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

UTY-2504: Nullable command response #1428

Merged
merged 8 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to add this to playground?
This might be rather spammy in a cloud build

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only logs when you left/right-click, but if its still too spammy I can remove it. @jamiebrynes7 @paulbalaji what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be useful locally, but don't think it's worth keeping for cloud builds

{
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]);
BryanJY-Wong marked this conversation as resolved.
Show resolved Hide resolved
Debug.Log($"Launching {lastId.Value.Raw}");
#else
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking if you tested this works? I think this now means that it won't send commands in the editor

Copy link
Contributor Author

@BryanJY-Wong BryanJY-Wong Jul 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😕 if editor send command, store id and log else just send command, and yes I did test it

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