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

Commit

Permalink
UTY-2504: Nullable command response (#1428)
Browse files Browse the repository at this point in the history
* Change messagespan to nullable

* Added simple validation in player command system

* todo changelist

* Update CHANGELOG.md

* add preprocessor directives

* send command
  • Loading branch information
BryanJY-Wong authored Jul 21, 2020
1 parent a3f59d1 commit e935704
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- See the [upgrade guide](UPGRADE_GUIDE.md) for detailed upgrade instructions for this breaking change.
- `IEntityGameObjectCreator.PopulateEntityTypeExpectations` now only needs to specify what SpatialOS components it needs for spawning an entity, such as `Position`.
- `GameObjectCreationHelper.EnableStandardGameObjectCreation` now requires a non-null `EntityRepresentationMapping` to be passed in.
- The `GetResponse<T>(CommandRequestId)` method in the `IDiffCommandResponseStorage` and `CommandSystem` now returns a `T?` instead of `MessageSpan<T>` [#1428](https://github.com/spatialos/gdk-for-unity/pull/1428)

### Added

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Improbable;
using Improbable.Gdk.Core;
using Improbable.Gdk.Core.Commands;
using Playground.Scripts.UI;
using Unity.Collections;
using Unity.Entities;
Expand All @@ -26,6 +27,9 @@ private enum PlayerCommand
private CommandSystem commandSystem;
private EntityQuery launchGroup;

#if UNITY_EDITOR
private CommandRequestId? lastId;
#endif
protected override void OnCreate()
{
base.OnCreate();
Expand All @@ -39,6 +43,22 @@ protected override void OnCreate()

protected override void OnUpdate()
{
#if UNITY_EDITOR
if (lastId.HasValue)
{
var response = commandSystem.GetResponse<Launcher.LaunchEntity.ReceivedResponse>(lastId.Value);
if (response.HasValue)
{
Debug.Log($"Launch {response.Value.RequestId.Raw} successful, with response {response.Value}");
lastId = null;
}
else
{
Debug.Log($"Could not find response for Launch {lastId.Value.Raw}");
}
}
#endif

using (var entities = launchGroup.ToEntityArray(Allocator.TempJob))
using (var spatialIdData = launchGroup.ToComponentDataArray<SpatialEntityId>(Allocator.TempJob))
{
Expand Down Expand Up @@ -86,8 +106,12 @@ protected override void OnUpdate()
command == PlayerCommand.LaunchLarge ? LargeEnergy : SmallEnergy,
playerId
));

#if UNITY_EDITOR
lastId = commandSystem.SendCommand(request, entities[0]);
Debug.Log($"Launching {lastId.Value.Raw}");
#else
commandSystem.SendCommand(request, entities[0]);
#endif
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public MessagesSpan<TResponse> GetResponses()
return responseStorage.Slice();
}

public MessagesSpan<TResponse> GetResponse(CommandRequestId requestId)
public TResponse? GetResponse(CommandRequestId requestId)
{
if (!responsesSorted)
{
Expand All @@ -78,9 +78,7 @@ public MessagesSpan<TResponse> GetResponse(CommandRequestId requestId)
}

var responseIndex = responseStorage.GetResponseIndex(requestId);
return responseIndex.HasValue
? responseStorage.Slice(responseIndex.Value, 1)
: MessagesSpan<TResponse>.Empty();
return responseIndex.HasValue ? responseStorage[responseIndex.Value] : (TResponse?) null;
}

private sealed class RequestComparer<T> : IComparer<T> where T : struct, IReceivedCommandRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public MessagesSpan<T> GetResponses<T>() where T : struct, IReceivedCommandRespo
return manager.GetResponses();
}

public MessagesSpan<T> GetResponse<T>(CommandRequestId requestId) where T : struct, IReceivedCommandResponse
public T? GetResponse<T>(CommandRequestId requestId) where T : struct, IReceivedCommandResponse
{
var manager = (IDiffCommandResponseStorage<T>) worker.Diff.GetCommandDiffStorage(typeof(T));
return manager.GetResponse(requestId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ public interface IDiffCommandResponseStorage<T> : ICommandDiffStorage
{
void AddResponse(T response);
MessagesSpan<T> GetResponses();
MessagesSpan<T> GetResponse(CommandRequestId requestId);
T? GetResponse(CommandRequestId requestId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public void AddResponse(WorldCommands.EntityQuery.ReceivedResponse response)
return entityQueryResponses.Slice();
}

MessagesSpan<WorldCommands.CreateEntity.ReceivedResponse>
IDiffCommandResponseStorage<WorldCommands.CreateEntity.ReceivedResponse>.GetResponse(CommandRequestId requestId)
WorldCommands.CreateEntity.ReceivedResponse? IDiffCommandResponseStorage<WorldCommands.CreateEntity.ReceivedResponse>.GetResponse(CommandRequestId requestId)
{
if (!createEntitySorted)
{
Expand All @@ -95,11 +94,11 @@ public void AddResponse(WorldCommands.EntityQuery.ReceivedResponse response)

var responseIndex = createEntityResponses.GetResponseIndex(requestId);
return responseIndex.HasValue
? createEntityResponses.Slice(responseIndex.Value, 1)
: MessagesSpan<WorldCommands.CreateEntity.ReceivedResponse>.Empty();
? createEntityResponses[responseIndex.Value]
: (WorldCommands.CreateEntity.ReceivedResponse?) null;
}

MessagesSpan<WorldCommands.DeleteEntity.ReceivedResponse>
WorldCommands.DeleteEntity.ReceivedResponse?
IDiffCommandResponseStorage<WorldCommands.DeleteEntity.ReceivedResponse>.GetResponse(CommandRequestId requestId)
{
if (!deleteEntitySorted)
Expand All @@ -110,11 +109,11 @@ public void AddResponse(WorldCommands.EntityQuery.ReceivedResponse response)

var responseIndex = deleteEntityResponses.GetResponseIndex(requestId);
return responseIndex.HasValue
? deleteEntityResponses.Slice(responseIndex.Value, 1)
: MessagesSpan<WorldCommands.DeleteEntity.ReceivedResponse>.Empty();
? deleteEntityResponses[responseIndex.Value]
: (WorldCommands.DeleteEntity.ReceivedResponse?) null;
}

MessagesSpan<WorldCommands.ReserveEntityIds.ReceivedResponse>
WorldCommands.ReserveEntityIds.ReceivedResponse?
IDiffCommandResponseStorage<WorldCommands.ReserveEntityIds.ReceivedResponse>.GetResponse(CommandRequestId requestId)
{
if (!reserveEntityIdsSorted)
Expand All @@ -125,11 +124,11 @@ public void AddResponse(WorldCommands.EntityQuery.ReceivedResponse response)

var responseIndex = reserveEntityIdsResponses.GetResponseIndex(requestId);
return responseIndex.HasValue
? reserveEntityIdsResponses.Slice(responseIndex.Value, 1)
: MessagesSpan<WorldCommands.ReserveEntityIds.ReceivedResponse>.Empty();
? reserveEntityIdsResponses[responseIndex.Value]
: (WorldCommands.ReserveEntityIds.ReceivedResponse?) null;
}

MessagesSpan<WorldCommands.EntityQuery.ReceivedResponse>
WorldCommands.EntityQuery.ReceivedResponse?
IDiffCommandResponseStorage<WorldCommands.EntityQuery.ReceivedResponse>.GetResponse(CommandRequestId requestId)
{
if (!entityQueriesSorted)
Expand All @@ -140,8 +139,8 @@ public void AddResponse(WorldCommands.EntityQuery.ReceivedResponse response)

var responseIndex = entityQueryResponses.GetResponseIndex(requestId);
return responseIndex.HasValue
? entityQueryResponses.Slice(responseIndex.Value, 1)
: MessagesSpan<WorldCommands.EntityQuery.ReceivedResponse>.Empty();
? entityQueryResponses[responseIndex.Value]
: (WorldCommands.EntityQuery.ReceivedResponse?) null;
}

private class Comparer : IComparer<WorldCommands.CreateEntity.ReceivedResponse>,
Expand Down

0 comments on commit e935704

Please sign in to comment.