Skip to content
This repository was archived by the owner on Dec 5, 2018. It is now read-only.

Commit 3a1ed8b

Browse files
committed
upgraded to SpatialOS 12.0.0
1 parent e574118 commit 3a1ed8b

File tree

7 files changed

+43
-45
lines changed

7 files changed

+43
-45
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ If you run into problems, or want to give us feedback, please visit the [Spatial
2121

2222
## Running the project
2323

24-
To run the project locally, first build it by running `spatial worker build`, then start the server with `spatial local launch`. You can connect a client by opening the Unity project and pressing the play button, or by running `spatial local worker launch UnityClient default`. See the [documentation](https://spatialos.improbable.io/docs/reference/latest/developing/local/run) for more details.
24+
To run the project locally, first build it by running `spatial worker build`, then start the server with `spatial local start`. You can connect a client by opening the Unity project and pressing the play button, or by running `spatial local worker launch UnityClient default`. See the [documentation](https://spatialos.improbable.io/docs/reference/latest/developing/local/run) for more details.
2525

2626
To deploy the project to the cloud, first build it by running `spatial worker build -t=deployment`, then upload the assembly with `spatial cloud upload <assembly name>`, and finally deploy it with `spatial cloud launch <assembly name> <launch configuration file> <deployment name> --snapshot=<snapshot file>`. You can obtain and share links to connect to the deployment from the [console](http://console.improbable.io/projects). See the [documentation](https://docs.improbable.io/reference/latest/developing/deploy-cloud) for more details.

schema/improbable/core/PlayerCreation.schema

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package improbable.core;
22

33
type CreatePlayerRequest {}
4-
type CreatePlayerResponse {}
4+
type CreatePlayerResponse {
5+
int32 status_code = 1;
6+
}
57

68
component PlayerCreation {
79
id = 1001;

snapshots/default.snapshot

0 Bytes
Binary file not shown.

spatialos.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "your_project_name_here",
33
"project_version": "1.0.0",
4-
"sdk_version": "11.1.1",
4+
"sdk_version": "12.0.0",
55
"dependencies": [
6-
{"name": "standard_library", "version": "11.1.1"}
6+
{"name": "standard_library", "version": "12.0.0"}
77
]
88
}

workers/unity/Assets/Gamelogic/Core/Bootstrap.cs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
using Improbable.Unity.Core;
88
using Improbable.Unity.Core.EntityQueries;
99
using UnityEngine;
10+
using Improbable.Worker;
11+
1012

11-
// Placed on a GameObject in a Unity scene to execute SpatialOS connection logic on startup.
1213
namespace Assets.Gamelogic.Core
1314
{
15+
// Placed on a GameObject in a Unity scene to execute SpatialOS connection logic on startup.
1416
public class Bootstrap : MonoBehaviour
1517
{
1618
public WorkerConfigurationData Configuration = new WorkerConfigurationData();
@@ -40,19 +42,20 @@ public void Start()
4042
}
4143

4244
// Search for the PlayerCreator entity in the world in order to send a CreatePlayer command.
43-
public static void CreatePlayer()
45+
public void CreatePlayer()
4446
{
4547
var playerCreatorQuery = Query.HasComponent<PlayerCreation>().ReturnOnlyEntityIds();
4648
SpatialOS.WorkerCommands.SendQuery(playerCreatorQuery)
4749
.OnSuccess(OnSuccessfulPlayerCreatorQuery)
4850
.OnFailure(OnFailedPlayerCreatorQuery);
4951
}
5052

51-
private static void OnSuccessfulPlayerCreatorQuery(EntityQueryResult queryResult)
53+
private void OnSuccessfulPlayerCreatorQuery(EntityQueryResult queryResult)
5254
{
5355
if (queryResult.EntityCount < 1)
5456
{
5557
Debug.LogError("Failed to find PlayerCreator. SpatialOS probably hadn't finished loading the initial snapshot. Try again in a few seconds.");
58+
StartCoroutine(TimerUtils.WaitAndPerform(SimulationSettings.PlayerCreatorQueryRetrySecs, CreatePlayer));
5659
return;
5760
}
5861

@@ -61,24 +64,38 @@ private static void OnSuccessfulPlayerCreatorQuery(EntityQueryResult queryResult
6164
}
6265

6366
// Retry a failed search for the PlayerCreator entity after a short delay.
64-
private static void OnFailedPlayerCreatorQuery(ICommandErrorDetails _)
67+
private void OnFailedPlayerCreatorQuery(ICommandErrorDetails _)
6568
{
6669
Debug.LogError("PlayerCreator query failed. SpatialOS workers probably haven't started yet. Try again in a few seconds.");
67-
TimerUtils.WaitAndPerform(SimulationSettings.PlayerCreatorQueryRetrySecs, CreatePlayer);
70+
StartCoroutine(TimerUtils.WaitAndPerform(SimulationSettings.PlayerCreatorQueryRetrySecs, CreatePlayer));
6871
}
6972

7073
// Send a CreatePlayer command to the PLayerCreator entity requesting a Player entity be spawned.
71-
private static void RequestPlayerCreation(EntityId playerCreatorEntityId)
74+
private void RequestPlayerCreation(EntityId playerCreatorEntityId)
7275
{
7376
SpatialOS.WorkerCommands.SendCommand(PlayerCreation.Commands.CreatePlayer.Descriptor, new CreatePlayerRequest(), playerCreatorEntityId)
74-
.OnFailure(response => OnCreatePlayerFailure(response, playerCreatorEntityId));
77+
.OnSuccess(response => OnCreatePlayerCommandSuccess(response, playerCreatorEntityId))
78+
.OnFailure(response => OnCreatePlayerCommandFailure(response, playerCreatorEntityId));
7579
}
7680

77-
// Retry a failed creation of the Player entity after a short delay.
78-
private static void OnCreatePlayerFailure(ICommandErrorDetails details, EntityId playerCreatorEntityId)
81+
private void OnCreatePlayerCommandSuccess(CreatePlayerResponse response, EntityId playerCreatorEntityId)
7982
{
83+
var statusCode = (StatusCode) response.statusCode;
84+
if (statusCode != StatusCode.Success) {
85+
Debug.LogWarningFormat("PlayerCreator failed to create the player entity. Status code = {0}. Try again in a few seconds.", statusCode.ToString());
86+
RetryCreatePlayerCommand(playerCreatorEntityId);
87+
}
88+
}
89+
90+
private void OnCreatePlayerCommandFailure(ICommandErrorDetails details, EntityId playerCreatorEntityId){
8091
Debug.LogWarningFormat("CreatePlayer command failed. Status code = {0}. - you probably tried to connect too soon. Try again in a few seconds.", details.StatusCode.ToString());
81-
TimerUtils.WaitAndPerform(SimulationSettings.PlayerEntityCreationRetrySecs, () => RequestPlayerCreation(playerCreatorEntityId));
92+
RetryCreatePlayerCommand(playerCreatorEntityId);
93+
}
94+
95+
// Retry a failed creation of the Player entity after a short delay.
96+
private void RetryCreatePlayerCommand(EntityId playerCreatorEntityId)
97+
{
98+
StartCoroutine(TimerUtils.WaitAndPerform(SimulationSettings.PlayerEntityCreationRetrySecs, () => RequestPlayerCreation(playerCreatorEntityId)));
8299
}
83100
}
84101
}

workers/unity/Assets/Gamelogic/Core/PlayerCreatingBehaviour.cs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Improbable.Unity.Core;
77
using Improbable.Unity.Visualizer;
88
using UnityEngine;
9+
using Improbable.Worker;
910

1011
namespace Assets.Gamelogic.Core
1112
{
@@ -17,44 +18,21 @@ public class PlayerCreatingBehaviour : MonoBehaviour
1718

1819
private void OnEnable()
1920
{
20-
PlayerCreationWriter.CommandReceiver.OnCreatePlayer.RegisterResponse(OnCreatePlayer);
21+
PlayerCreationWriter.CommandReceiver.OnCreatePlayer.RegisterAsyncResponse(OnCreatePlayer);
2122
}
2223

2324
private void OnDisable()
2425
{
2526
PlayerCreationWriter.CommandReceiver.OnCreatePlayer.DeregisterResponse();
2627
}
2728

28-
private CreatePlayerResponse OnCreatePlayer(CreatePlayerRequest request, ICommandCallerInfo callerinfo)
29-
{
30-
CreatePlayerWithReservedId(callerinfo.CallerWorkerId);
31-
return new CreatePlayerResponse();
32-
}
33-
34-
private void CreatePlayerWithReservedId(string clientWorkerId)
35-
{
36-
SpatialOS.Commands.ReserveEntityId(PlayerCreationWriter)
37-
.OnSuccess(result => CreatePlayer(clientWorkerId, result.ReservedEntityId))
38-
.OnFailure(failure => OnFailedReservation(failure, clientWorkerId));
39-
}
40-
41-
private void OnFailedReservation(ICommandErrorDetails response, string clientWorkerId)
42-
{
43-
Debug.LogError("Failed to Reserve EntityId for Player: " + response.ErrorMessage + ". Retrying...");
44-
CreatePlayerWithReservedId(clientWorkerId);
45-
}
46-
47-
private void CreatePlayer(string clientWorkerId, EntityId entityId)
29+
private void OnCreatePlayer(ResponseHandle<PlayerCreation.Commands.CreatePlayer, CreatePlayerRequest, CreatePlayerResponse> responseHandle)
4830
{
31+
var clientWorkerId = responseHandle.CallerInfo.CallerWorkerId;
4932
var playerEntityTemplate = EntityTemplateFactory.CreatePlayerTemplate(clientWorkerId);
50-
SpatialOS.Commands.CreateEntity(PlayerCreationWriter, entityId, playerEntityTemplate)
51-
.OnFailure(failure => OnFailedPlayerCreation(failure, clientWorkerId, entityId));
52-
}
53-
54-
private void OnFailedPlayerCreation(ICommandErrorDetails response, string clientWorkerId, EntityId entityId)
55-
{
56-
Debug.LogError("Failed to Create Player Entity: " + response.ErrorMessage + ". Retrying...");
57-
CreatePlayer(clientWorkerId, entityId);
33+
SpatialOS.Commands.CreateEntity (PlayerCreationWriter, playerEntityTemplate)
34+
.OnSuccess (_ => responseHandle.Respond (new CreatePlayerResponse ((int) StatusCode.Success)))
35+
.OnFailure (failure => responseHandle.Respond (new CreatePlayerResponse ((int) failure.StatusCode)));
5836
}
5937
}
6038
}

workers/unity/Assets/Gamelogic/Core/TransformReceiver.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Improbable;
22
using Improbable.Core;
33
using Improbable.Unity.Visualizer;
4+
using Improbable.Worker;
45
using UnityEngine;
56

67
namespace Assets.Gamelogic.Core
@@ -27,7 +28,7 @@ void OnDisable()
2728

2829
void OnPositionUpdated(Position.Update update)
2930
{
30-
if (!PositionReader.HasAuthority)
31+
if (PositionReader.Authority == Authority.NotAuthoritative)
3132
{
3233
if (update.coords.HasValue)
3334
{
@@ -38,7 +39,7 @@ void OnPositionUpdated(Position.Update update)
3839

3940
void OnRotationUpdated(Rotation.Update update)
4041
{
41-
if (!RotationReader.HasAuthority)
42+
if (RotationReader.Authority == Authority.NotAuthoritative)
4243
{
4344
if (update.rotation.HasValue)
4445
{

0 commit comments

Comments
 (0)