Skip to content

Commit

Permalink
Merge pull request #54 from asadm/RPC-fix
Browse files Browse the repository at this point in the history
fix: resolved issues with removing invoked RPC callbacks
  • Loading branch information
momintlh authored Apr 18, 2024
2 parents 591c229 + db30345 commit c288eb8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
11 changes: 9 additions & 2 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,8 @@ public static void RpcCall(string name, object data, Action callbackOnResponse)
[MonoPInvokeCallback(typeof(Action))]
private static void InvokeOnResponseCallback()
{
var namesToRemove = new List<string>();

foreach (var name in RpcEventNames)
{
try
Expand All @@ -1085,15 +1087,20 @@ private static void InvokeOnResponseCallback()
callback?.Invoke();
}

RpcEventNames.Remove(name);
OnResponseCallbacks.Remove(name);
namesToRemove.Add(name);
}
}
catch (Exception ex)
{
Debug.LogError($"C#: Error in Invoking callback for RPC event name: '{name}': {ex.Message}");
}
}

foreach (var name in namesToRemove)
{
RpcEventNames.Remove(name);
OnResponseCallbacks.Remove(name);
}
}


Expand Down
44 changes: 13 additions & 31 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ public class GameManager : MonoBehaviour

[SerializeField] private static bool playerJoined;

[SerializeField] private Text scoreText;

bool isMoving;

private void Awake()
{
// PlayroomKit.OnPlayerJoin(AddPlayer);

PlayroomKit.InsertCoin(new PlayroomKit.InitOptions()
{
maxPlayersPerRoom = 2,
matchmaking = true,
defaultPlayerStates = new() {
{"score", -500},
{"score", 0},
},

}, () =>
Expand Down Expand Up @@ -85,38 +84,31 @@ private void Update()
var myPlayer = PlayroomKit.MyPlayer();
var index = players.IndexOf(myPlayer);

Debug.Log(index);
playerGameObjects[index].GetComponent<PlayerController>().Move();
players[index].SetState("posX", playerGameObjects[index].GetComponent<Transform>().position.x);
players[index].SetState("posY", playerGameObjects[index].GetComponent<Transform>().position.y);


if (index != -1)
{
playerGameObjects[index].GetComponent<PlayerController>().Move();
players[index].SetState("posX", playerGameObjects[index].GetComponent<Transform>().position.x);
players[index].SetState("posY", playerGameObjects[index].GetComponent<Transform>().position.y);
}
if (Input.GetKeyDown(KeyCode.Space))
{
myPlayer.Kick(() =>
{
Destroy(playerGameObjects[index]);
});

Vector3 playerPosition = playerGameObjects[index].transform.position;
playerGameObjects[index].GetComponent<PlayerController>().ShootBullet(playerPosition, 50f);

score += 50;

// PlayroomKit.RpcCall("ShootBullet", score, () =>
// {
// Debug.Log("shooting bullet!");
// });
PlayroomKit.RpcCall("ShootBullet", score, () =>
{
Debug.Log("shooting bullet!");
});
}

if (Input.GetKeyDown(KeyCode.R) && PlayroomKit.IsHost())
{
PlayroomKit.ResetStates(null, () =>
{
var defscore = PlayroomKit.GetState<int>("score");
// scoreText.text = "Score: " + defscore.ToString();
scoreText.text = "Score: " + defscore.ToString();
});

}
Expand All @@ -140,14 +132,6 @@ private void Update()

}

void rpcRegisterCallback(string data, string sender)
{
Debug.Log("sender: " + sender);
var player = PlayroomKit.GetPlayer(sender);
Debug.Log("Name of sender: " + player.GetProfile().name);
}


public void AddPlayer(PlayroomKit.Player player)
{
GameObject playerObj = (GameObject)Instantiate(Resources.Load("Player"),
Expand All @@ -160,7 +144,7 @@ public void AddPlayer(PlayroomKit.Player player)
players.Add(player);
playerGameObjects.Add(playerObj);

Text scoreText = (players.Count == 1) ? scoreText1 : scoreText2;
scoreText = (players.Count == 1) ? scoreText1 : scoreText2;
playerObj.GetComponent<PlayerController>().scoreText = scoreText;


Expand All @@ -169,8 +153,6 @@ public void AddPlayer(PlayroomKit.Player player)
player.OnQuit(RemovePlayer);
}



[MonoPInvokeCallback(typeof(Action<string>))]
private static void RemovePlayer(string playerID)
{
Expand Down

0 comments on commit c288eb8

Please sign in to comment.