Skip to content

Commit

Permalink
Fix: fixed callback invoking in rpcRegister
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed May 10, 2024
1 parent 3925d37 commit 76811f8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
45 changes: 30 additions & 15 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1044,17 +1044,15 @@ public enum RpcMode
HOST
}

private static List<Action<string, string>> rpcRegisterCallbacks = new();
private static List<string> rpcRegisteredEvents = new();
private static Dictionary<string, Action<string, string>> rpcRegisterCallbacks = new();
private static List<string> rpcCalledEvents = new();

[DllImport("__Internal")]
private extern static void RpcRegisterInternal(string name, Action<string, string> rpcRegisterCallback, string onResponseReturn = null);

public static void RpcRegister(string name, Action<string, string> rpcRegisterCallback, string onResponseReturn = null)
{
rpcRegisterCallbacks.Add(rpcRegisterCallback);
rpcRegisteredEvents.Add(name);
rpcRegisterCallbacks.Add(name, rpcRegisterCallback);
RpcRegisterInternal(name, InvokeRpcRegisterCallBack, onResponseReturn);
}

Expand All @@ -1079,20 +1077,28 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson
}


// Getting the name of called Rpc event
string name = GetState<string>("rpcCalledEventName");
List<string> updatedRpcCalledEvents = new();
// This state is required to update the called rpc events list
string nameJson = GetState<string>("rpcCalledEventName");

if (!rpcCalledEvents.Contains(name))
JSONArray jsonArray = JSON.Parse(nameJson).AsArray;
foreach (JSONNode node in jsonArray)
{
rpcCalledEvents.Add(name);
string item = node.Value;
updatedRpcCalledEvents.Add(item);
}

Debug.Log("\n\nupdatedRpcCalledEvents: ");
for (int i = 0; i < updatedRpcCalledEvents.Count; i++)
{
Debug.Log(updatedRpcCalledEvents[i]);
}

for (int i = 0; i < Math.Min(rpcRegisteredEvents.Count, rpcCalledEvents.Count); i++)
foreach (string name in updatedRpcCalledEvents)
{
if (rpcRegisteredEvents[i] == rpcCalledEvents[i])
if (rpcRegisterCallbacks.TryGetValue(name, out Action<string, string> callback))
{
rpcRegisterCallbacks[i].Invoke(dataJson, senderJson);
callback?.Invoke(dataJson, senderJson);
}
}

Expand All @@ -1105,9 +1111,6 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson

public static void RpcCall(string name, object data, RpcMode mode, Action callbackOnResponse)
{
// setting to sync the event name with other players
SetState("rpcCalledEventName", name, reliable: true);


string jsonData = ConvertToJson(data);

Expand All @@ -1124,6 +1127,18 @@ public static void RpcCall(string name, object data, RpcMode mode, Action callba
}
}


JSONArray jsonArray = new JSONArray();
foreach (string item in rpcCalledEvents)
{
jsonArray.Add(item);
}
string jsonString = jsonArray.ToString();
/*
This is requrired to sync the rpc events between all players, without this players won't know which event has been called.
this is a temporary fix, RPC's need to be handled within JS for better control.*/
SetState("rpcCalledEventName", jsonString, reliable: true);

RpcCallInternal(name, jsonData, mode, InvokeOnResponseCallback);
}

Expand All @@ -1138,7 +1153,7 @@ private static void InvokeOnResponseCallback()
{
var namesToRemove = new List<string>();

foreach (var name in rpcCalledEvents)
foreach (string name in rpcCalledEvents)
{
try
{
Expand Down
9 changes: 6 additions & 3 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ void Start()
PlayroomKit.RpcRegister("Hello", Hello);
}

private void Hello(string arg1, string arg2)
private void Hello(string data, string caller)
{
print("helo");
var player = PlayroomKit.GetPlayer(caller);
Debug.Log($"Caller: {caller}, Player Name: {player?.GetProfile().name}, Data: {data}");
}

void HandleScoreUpdate(string data, string caller)
Expand Down Expand Up @@ -111,6 +112,7 @@ private void Update()
players[index].SetState("pos", playerGameObjects[index].GetComponent<Transform>().position);

ShootBullet(index);
SayHello();

if (Input.GetKeyDown(KeyCode.R) && PlayroomKit.IsHost())
{
Expand Down Expand Up @@ -159,7 +161,8 @@ private void SayHello()
{
if (Input.GetKeyDown(KeyCode.H))
{
PlayroomKit.RpcCall("Hello", -1, () => {
PlayroomKit.RpcCall("Hello", -1, () =>
{
Debug.Log("saying helo");
});
}
Expand Down

0 comments on commit 76811f8

Please sign in to comment.