Sites

Menu

ECS: World commands

Before reading this document, make sure you are familiar with:

World commands are built-in commands that are used to interact with the SpatialOS runtime to ask it to reserve entity ids, create or delete entities, or request information about entities.

The CommandSystem used for component commands can also be used for sending and receiving world commands.

Reserve an entity ID

You can use the ReserveEntityIds world command to reserve groups of entity IDs that you can use in entity creation.

Create a WorldCommands.ReserveEntityIds.Request and pass in the number of Entity IDs you wish to reserve. The TimeoutMillis field is optional.

To send the request, call the SendCommand method on the CommandSystem.

var numIdsToReserve = 10;
var reserveEntityIdsRequest = new WorldCommands.ReserveEntityIds.Request(numIdsToReserve);
commandSystem.SendCommand(reserveEntityIdsRequest);

You can get responses by calling GetResponses on the CommandSystem with the WorldCommands.ReserveEntityIds.ReceivedResponse type parameter.

commandSystem.GetResponses<WorldCommands.ReserveEntityIds.ReceivedResponse>();

Create an entity

The CreateEntity world command is used to request the creation of a new SpatialOS entity, specified using an entity template.

You must construct a WorldCommands.CreateEntity.Request struct and provide an entity template. The EntityId and TimeoutMillis fields are optional. If you do specify an EntityId, you need to get this from a ReserveEntityIds command.

To send the request, call the SendCommand method on the CommandSystem.

var entity = Creatures.CreatureEntityTemplate(new Coordinates(0, 0, 0));
var createEntityRequest = new WorldCommands.CreateEntity.Request(entity);
commandSystem.SendCommand(createEntityRequest);

You can get responses by calling GetResponses on the CommandSystem with the WorldCommands.CreateEntity.ReceivedResponse type parameter.

commandSystem.GetResponses<WorldCommands.CreateEntity.ReceivedResponse>();

Delete an entity

You can delete entities via the DeleteEntity world command. You need to know the SpatialOS entity ID of the entity you want to delete.

Create a WorldCommands.DeleteEntity.Request struct with the EntityId of the entity you want to delete. The TimeoutMillis field is optional.

To send the request, call the SendCommand method on the CommandSystem.

var deleteEntityRequest = new WorldCommands.DeleteEntity.Request(targetEntityId);
commandSystem.SendCommand(deleteEntityRequest);

You can get responses by calling GetResponses on the CommandSystem with the WorldCommands.DeleteEntity.ReceivedResponse type parameter.

commandSystem.GetResponses<WorldCommands.CreateEntity.ReceivedResponse>();

Entity queries

You can use entity queries to get information about entities in the world. For more information, see entity queries in the SpatialOS documentation.

Create a WorldCommands.EntityQuery.Request struct with the EntityQuery. The TimeoutMillis field is optional.

To send the request, call the SendCommand method on the CommandSystem.

var playerCreatorQuery = new EntityQuery
{
    Constraint = new ComponentConstraint(PlayerCreator.ComponentId),
    ResultType = new SnapshotResultType()
};
var entityQueryRequest = new WorldCommands.EntityQuery.Request(playerCreatorQuery);
commandSystem.SendCommand(entityQueryRequest);

You can get responses by calling GetResponses on the CommandSystem with the WorldCommands.EntityQuery.ReceivedResponse type parameter.

commandSystem.GetResponses<WorldCommands.EntityQuery.ReceivedResponse>();

Search results

Was this page helpful?

Thanks for letting us know!

Thanks for your feedback

Need more help? Ask on the forums