Set up custom spawning

Set up custom spawning

The Game Object Creation Feature Module offers you a simple way to customize how GameObjects are created. You must:

  1. Implement a class which inherits from the IEntityGameObjectCreator interface.
  2. Tell the GDK to use this implementation.

Implement the IEntityGameObjectCreator interface

A good reference implementation of this interface can be found in the GameObjectCreatorFromMetadata.

PopulateEntityTypeExpectations

This method is called by the constructor of the GameObjectInitializationSystem to register the components required by the IEntityGameObjectCreator for creating GameObjects of specific entity types.

In the example below it is specified that the IEntityGameObjectCreator must wait for "Player" entities to obtain a ServerMovement component. In addition, the IEntityGameObjectCreator will delay its creation of other entity types until the entity contains a Position component.

public void PopulateEntityTypeExpectations(EntityTypeExpectations entityTypeExpectations)
{
    entityTypeExpectations.RegisterDefault(new[]
    {
        typeof(Position.Component)
    });

    entityTypeExpectations.RegisterEntityType("Player", new[]
    {
        typeof(ServerMovement.Component)
    });
}

OnEntityCreated

This method is called by the GameObjectInitializationSystem whenever a SpatialOS entity within your view has read access to both a Metadata component and a minimum set of components as defined inside by the Entity Resolver for a given entity type.

The entity type is read from the entity's Metadata component, and is provided inside the SpatialOSEntityInfo as the first argument by the GameObjectInitializationSystem.

Note: This means that your entities must have the Metadata component to use the GameObject Creation Feature Module. In addition, the worker running the GameObject Creation systems must have read access to this component in order to trigger this method on your IEntityGameObjectCreator.

The SpatialOSEntityInfo struct also gives you an instance of a SpatialOSEntity class to represent the entity and its SpatialOS entity ID.

The GameObjectInitializationSystem also resolves the prefab that the entity should map to, and passes that in as the second argument. In addition, the EntityManager and an instance of the EntityGameObjectLinker class are provided as parameters.

If you wish to create a GameObject to represent the SpatialOS entity, you must instantiate the GameObject yourself and call EntityGameObjectLinker.LinkGameObjectToSpatialOSEntity with the entity ID and that GameObject.

The LinkGameObjectToSpatialOS call has a third parameter: params Type[] componentTypesToAdd. These are the types of MonoBehaviour components that you want the linker to copy references onto the underlying ECS entity.

Note: If you also want to use the Transform Synchronization module, you must copy the UnityEngine.Transform and the UnityEngine.Rigidbody MonoBehaviour components.

OnEntityRemoved

This method is called by the GameObjectInitializationSystem whenever a SpatialOS entity leaves a view. It is called after any linked GameObjects have been unlinked.

The system gives you the entity ID of the SpatialOS entity that has left your view as a parameter.

It is your responsibility to destroy the GameObjects that have been linked to that SpatialOS entity.

Use your custom IEntityGameObjectCreator implementation

Open your WorkerConnector implementation and add the following line to the HandleWorkerConnectionEstablished method.

Note: The worker connector must be provided with an EntityRepresentationMapping asset.

    GameObjectCreationHelper.EnableStandardGameObjectCreation(Worker.World, new MyGameObjectCreator(), entityRepresentationMapping);

Note: You may need to override the HandleWorkerConnectionEstablished method in your WorkerConnector implementation if you haven't already.

(Optional) Link a worker prefab to your worker instance

If you have a worker prefab that you wish to link to your worker instance, you should replace the line noted above with:

  GameObjectCreationHelper.EnableStandardGameObjectCreation(Worker.World, new MyGameObjectCreator(), entityRepresentationMapping, workerPrefabInstance);

Updated 12 months ago


Set up custom spawning


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.