Improbable Icon

SpatialOS Forums

Runtime SpatialOS entity spawning in Unity GDK

Hello, Im trying to spawn a player camera when the player entity spawns. I cant seem to find any up to date information on this topic. I tried my luck browsing through the api to no avail.

https://github.com/spatialos/sdk-for-unity/blob/master/docs/tutorials/recipes/runtime-entity-creation.md

In the link provided above I found a snippet of code in the sdk-for-unity spawn cube recipe. Which im looking for the equivalent. If anyone has information on this it would be much appreciated.

Code snippet below

    private void CreateCube(Spawn args)
    {
        var cubeTemplate = EntityTemplateFactory.CreateCubeTemplate(args.initialPosition);
        SpatialOS.Commands.CreateEntity(PositionWriter, cubeTemplate);
    }

I found the solution in

All I needed was

    [Require] private WorldCommandSender worldCommandRequestSender;

Alright, I’m still kind of lost on how to accomplish what I want… I can see the entity in the inspector but I don’t really understand how to get it into the workers view. The Game Object Creation documentation only explains how to spawn the player and under the Workflow section the Create and Delete Entities only explains how to add it to the server. I tried to manually create a EntityGameObjectLinker and link the newly created entity to an Instantiated GameObject. It prooduced the following error.

Can not add GameObjet components to entity 5. Entity not in view
Improbable.Gdk.Subscriptions.EntityGameObjectLinker.AddComponentsToEntity

Code from project

private void OnEntityCreated(WorldCommands.CreateEntity.ReceivedResponse response)
{
    if (response.StatusCode == StatusCode.Success)
    {
        entityGameObjectLinker.LinkGameObjectToSpatialOSEntity(response.EntityId.Value, new GameObject()); // <---- This Procduces the error
    }
}
private void OnSpawnCameraEvent(Spawn obj)
{
    var clientAttribute = EntityTemplate.GetWorkerAccessAttribute(UnityClientConnector.WorkerType);
    var serverAttribute = EntityTemplate.GetWorkerAccessAttribute(UnityGameLogicConnector.WorkerType);

    var template = new EntityTemplate();
    template.AddComponent(new Position.Snapshot(), serverAttribute);
    template.AddComponent(new Rotation.Snapshot(), serverAttribute);
    template.AddComponent(new Metadata.Snapshot("PlayerCamera"), serverAttribute);

    template.SetReadAccess(clientAttribute, serverAttribute);
    template.SetComponentWriteAccess(EntityAcl.ComponentId, serverAttribute);

    worldCommandSender.SendCreateEntityCommand(
        new WorldCommands.CreateEntity.Request(template), OnEntityCreated);
}

Hi @MetaCitizen

Can I ask what you’re trying to achieve with your player camera? Is there a reason it needs to be integrated with SpatialOS? In most standard implementations the camera doesn’t need to be replicated as other players will never see it, so it can be handled entirely on the local client.

Additionally, you mention the Game Object Creation documentation, but I think the Player Lifecycle documentation may be more useful to you here. Have you reviewed these docs as well?

Well I don’t want to give to much away but each player has a targeting system and it interacts with other players. Its not something I want to trust with the client because it would allow for hacking. If someone could rotate or move the camera on client side it could break the whole thing. The plan for my game is the only thing the client sends to the server is player input.

I did take a look at the Player Lifecycle but it only accepts one entity template. So I tried to bypass it with basic entity spawning. When the player gets created that’s when I call the code I’ve written above.