3. Services Component

  05. Services No Comments

Services Component

The Services component can be used to register multiple asset and scene based services accessible to select clients.

Registering Services Using The Services Component

You can attach the Services component to any GameObject using the Add Component menu.

Assigning Services

  1. Add a new item to the Provides Services list.
  2. Drag-and-drop the Object you want to register as a Service into the Object reference field.
  3. Select the defining type of the service using the dropdown menu on its right side.

You can define as many services as you want using a single Services component.

You can only assign components and scriptable objects to the Services Component directly. If you want to register a plain old C# object as a service using a Services component, you can create a wrapper or a value provider, and drag-and-drop that into the Object reference field.

Configuring Availability

Use the For Clients field to specify which clients should have access to the services registered using the component:

  1. In GameObject – Only clients that are attached to the same GameObject as the Services component can receive its services from it.
  2. In Children – Only clients that are attached to the same GameObject as the Sevices component, or any of its children (including nested children), can receive services from it.
  3. In Parents – Only clients that are attached to the same GameObject as the Services component, or any of its parents (including nested parents), can receive services from it.
  4. In Hierarchy Root Children – Only clients that are attached to the GameObject which is at the root of the Services component’s parent hierarchy, or any of the children of the root (including nested children), can receive services from it.
  5. In Scene – Only clients belonging to the same scene as the Services component can receive services from it.
  6. Everywhere – All clients can receive services from the Services component, regardless of which scene they belong to, or if they belong to any scene at all.

Use Cases

Both Services components and Service Tags can be used to register local services. Since they have a lot of overlapping use cases, which one you should use by default is mostly just a matter of personal preference. That being said, here are some situations where the Services component could be a particularly good choice:

  1. ScriptableObject Services – You can register scriptable object assets as services using the Services components.
  2. Value Providers – Services components are also a good choice for registering local services resolved using value providers.
  3. Complex Prefabs – You can attach a single Services component to the root of a prefab asset, and register all services from all its children using it. This lets you visualize all services found within the entire prefab hierarchy in a single neat list, and makes it easy to beam all those services only downwards into the children of the prefab’s root object by setting For Clients to In Children.

Best Practices

Limiting the availability of a local service to only the clients found in the hierarchy of the scene or prefab to which the service is attached to has some benefits:

  1. Synchronous Client Initialization – Having local services and their clients exist as part of the same scene or prefab hierarchy guarantees that the services will always be available when the clients are being loaded – regardless of the order in which you load your scenes and instantiate your prefabs. While Init(args) can automatically delay the initialization of components until all the services they depend on have become available, it still keeps things simpler when clients can simply be initialized synchronously.
  2. Accurate Null Argument Guard – The Init section can more accurately inform you about missing services when local services and their clients are not part of different scenes and prefabs. If the scene or prefab containing a local service is not loaded in Edit Mode, then the Null Argument Guard won’t know anything about it. While it’s possible to work around this by disabling the Null Argument Guard in Edit Mode, or by attaching an Initializer to the client and assigning Wait For Service as the Init argument field, your life will be simpler if you can skip all that.
  3. Better Inspector Integration – when the local services of a client are loaded, you’ll be able to easily ping them or select them using the Service icons found in the client’s Init section. Similarly, when all the clients of a local service are loaded, you’ll be able to easily locate all of them by selecting the Find Clients In Scenes item from the context menu of the Service icon drawn in a service component’s header.

Leave a Reply

Your email address will not be published. Required fields are marked *