Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

PlayerLifecycle integration with the EntityReservationSystem #1315

Merged
merged 5 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- Schema components in ECS no longer have a `ComponentId` property. [#1308](https://github.com/spatialos/gdk-for-unity/pull/1308)
- You should use `ComponentDatabase.GetComponentId<T>()` instead.
- `CustomSpatialOSSendSystem` is no longer available. [#1308](https://github.com/spatialos/gdk-for-unity/pull/1308)
- The PlayerLifecycle feature module now provides an `EntityId` in it's CreatePlayerEntityTemplate callback. [#1315](https://github.com/spatialos/gdk-for-unity/pull/1315)
paulbalaji marked this conversation as resolved.
Show resolved Hide resolved
- You will have to change your callback from `(string clientWorkerId, byte[] serializedArguments)` to `(EntityId entityId, string clientWorkerId, byte[] serializedArguments)`.

### Added

Expand Down
29 changes: 29 additions & 0 deletions UPGRADE_GUIDE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
# Upgrade Guide

## From `0.3.3` to `0.3.4`

### PlayerLifecycle feature module now provides an EntityId

The callback used for creating a player `EntityTemplate` has changed to provide an `EntityId` up front.
This will be the `EntityId` the player's Entity will have after it is successfully spawned, and can be usefull for QBI.
zeroZshadow marked this conversation as resolved.
Show resolved Hide resolved
paulbalaji marked this conversation as resolved.
Show resolved Hide resolved

```csharp
// Previously
public static EntityTemplate Player(string workerId, byte[] args)
{
var template = new EntityTemplate();
// ...
return template;
}

PlayerLifecycleConfig.CreatePlayerEntityTemplate = FpsEntityTemplates.Player;
zeroZshadow marked this conversation as resolved.
Show resolved Hide resolved

// Now
public static EntityTemplate Player(EntityId entityId, string workerId, byte[] args)
{
var template = new EntityTemplate();
// ...
return template;
}

PlayerLifecycleConfig.CreatePlayerEntityTemplate = FpsEntityTemplates.Player;
```
zeroZshadow marked this conversation as resolved.
Show resolved Hide resolved

## From `0.3.2` to `0.3.3`

### Building for Android now requires the NDK
Expand Down
2 changes: 1 addition & 1 deletion workers/unity/Assets/Playground/Config/PlayerTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Playground
{
public static class PlayerTemplate
{
public static EntityTemplate CreatePlayerEntityTemplate(string clientWorkerId, byte[] playerCreationArguments)
public static EntityTemplate CreatePlayerEntityTemplate(EntityId entityId, string clientWorkerId, byte[] playerCreationArguments)
{
var clientAttribute = EntityTemplate.GetWorkerAccessAttribute(clientWorkerId);

Expand Down
1 change: 0 additions & 1 deletion workers/unity/Assets/Playground/Config/WorkerUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public static void AddGameLogicSystems(World world)
PlayerLifecycleHelper.AddServerSystems(world);
GameObjectCreationHelper.EnableStandardGameObjectCreation(world);

world.GetOrCreateSystem<EntityReservationSystem>();
world.GetOrCreateSystem<TriggerColorChangeSystem>();
world.GetOrCreateSystem<ProcessLaunchCommandSystem>();
world.GetOrCreateSystem<ProcessRechargeSystem>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Improbable.Gdk.PlayerLifecycle
/// An <see cref="EntityTemplate"/> to create a SpatialOS player entity from.
/// </returns>
public delegate EntityTemplate GetPlayerEntityTemplateDelegate(
EntityId entityId,
string clientWorkerId,
byte[] serializedArguments);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public static void AddClientSystems(World world, bool autoRequestPlayerCreation
/// <param name="world">A world that belongs to a server-worker.</param>
public static void AddServerSystems(World world)
{
world.GetOrCreateSystem<EntityReservationSystem>();
zeroZshadow marked this conversation as resolved.
Show resolved Hide resolved
world.GetOrCreateSystem<HandleCreatePlayerRequestSystem>();
world.GetOrCreateSystem<PlayerHeartbeatInitializationSystem>();
world.GetOrCreateSystem<SendPlayerHeartbeatRequestSystem>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ namespace Improbable.Gdk.PlayerLifecycle
public class HandleCreatePlayerRequestSystem : ComponentSystem
{
private CommandSystem commandSystem;
private EntityReservationSystem entityReservationSystem;

protected override void OnCreate()
{
base.OnCreate();
commandSystem = World.GetExistingSystem<CommandSystem>();
entityReservationSystem = World.GetOrCreateSystem<EntityReservationSystem>();
paulbalaji marked this conversation as resolved.
Show resolved Hide resolved
}

private class PlayerCreationRequestContext
Expand All @@ -40,24 +42,34 @@ private void HandlePlayerCreateRequests()
for (var i = 0; i < requests.Count; ++i)
{
ref readonly var request = ref requests[i];
var playerEntityTemplate = PlayerLifecycleConfig.CreatePlayerEntityTemplate(
request.CallerWorkerId,
request.Payload.SerializedArguments
);

var entityRequest = new WorldCommands.CreateEntity.Request
(
playerEntityTemplate,
context: new PlayerCreationRequestContext
{
createPlayerRequest = request
}
);

commandSystem.SendCommand(entityRequest);
SpawnPlayerEntity(request);
}
}

private async void SpawnPlayerEntity(PlayerCreator.CreatePlayer.ReceivedRequest receivedRequest)
{
var entityId = await entityReservationSystem.GetAsync();

var playerEntityTemplate = PlayerLifecycleConfig.CreatePlayerEntityTemplate(
entityId,
receivedRequest.CallerWorkerId,
receivedRequest.Payload.SerializedArguments
);

var entityRequest = new WorldCommands.CreateEntity.Request
(
playerEntityTemplate,
entityId,
context: new PlayerCreationRequestContext
{
createPlayerRequest = receivedRequest
}
);

commandSystem.SendCommand(entityRequest);
}

private void HandlePlayerCreateEntityResponses()
{
var responses = commandSystem.GetResponses<WorldCommands.CreateEntity.ReceivedResponse>();
Expand Down