You will need to create a new Entity Representation Mapping asset in your project. This asset describes a mapping between an entity type and a resolver. The resolver describes how a worker chooses a GameObject for an entity.
How do I set the 'entity type' for my SpatialOS entities?
You can set the
entity_type field in the
Metadata component when declaring your EntityTemplate.
The following entity resolver types are provided:
A basic 1-to-1 mapping of an entity type to a prefab.
An authority based resolver which picks one of two prefabs based on the
OwningWorker component provided by the Player Lifecycle module.
To create an Entity Representation Mapping asset, right-click anywhere in your Project window and select
Create > SpatialOS > Entity Representation Mapping.
Note: We recommend that you name the asset based on the worker type you will be using it for.
Select the newly created asset to view it in the Inspector window, then for each entity type in your project:
- Select the
New Entity Typebutton at the bottom of the Inspector.
- Choose the resolver type that you want to use from the dropdown.
- Configure the resolver with the 'Entity Type' and the prefab(s) that it requires.
An example of what your Entity Representation Mapping could look like.
You will need to add a reference to your worker's prefab mapping in the worker.
[SerializeField] private EntityRepresentationMapping entityRepresentationMapping = default;
You then need to add the underlying systems to your worker. Open your
WorkerConnector implementation and add the following line to the
Note: You may need to override the
HandleWorkerConnectionEstablished method in your
WorkerConnector implementation if you haven't already.
This adds the
GameObjectInitializationSystem with an instance of the
GameObjectCreatorFromMetadata class to your worker, and pass along the selected prefab mapping for that worker type.
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, entityRepresentationMapping, workerPrefabInstance);
The other component of the GameObject Creation Feature Module is the GameObject Creators. These describe how to spawn a prefab in the world.
GameObjectCreatorFromMetadata class is the default implementation for spawning GameObjects for your SpatialOS entities.
Updated about a year ago