asadm / playroom-unity

9 stars 1 forks source link

OnPlayerJoin is not called #34

Closed asadm closed 5 months ago

asadm commented 5 months ago

From discord:

after updating the 0.0.10 package on Unity, my AddPlayer method with PlayroomKit.Player argument is no longer called when I do PlayroomKit.OnPlayerJoin(AddPlayer);
Even when I do a Debug.Log in the AddPlayer method, I get nothing, but before doing Playroom.Kit.OnPlayerJoin(AddPlayer), when I do a Debug.Log(PlayroomKit.MyPlayer().GetProfile().name), I get the person's nickname, which means it's been initialized.

So I'm wondering how to solve this problem, whether it's the type of argument that's changed, or there's a bug in the latest version or it's just my code that's not working.

Thanks for your help.
SaadBazaz commented 5 months ago

Exact Discord chat: https://discord.com/channels/997752993598419044/1128151935669764096/1198739652274819144

Huskago commented 5 months ago

Hello, I'm the one who reported the problem, so here are the various code extracts that are directly linked to Playroom.

First of all, here's a method that's called when I click on the Multiplayer button. When I click on it, it opens a scene in which the characters are supposed to appear:

public void Multiplayer()
    {
        PlayroomKit.InsertCoin(() =>
        {
            SceneManager.LoadScene("TestScene");
        }, new PlayroomKit.InitOptions()
        {
            maxPlayersPerRoom = 2
        });
    }

And on the second scene, there's a GameManager, in this GameManager, it contains all the multiplayer functionality, character appearance and synchronization:

private void Awake()
    {
        // if single player, create player game object
        if (PlayroomKit.MyPlayer() == null)
        {
            // create player game object
            GameObject playerObj = Instantiate(playerPrefab, new Vector2(Random.Range(-1.5f, 1.2f), Random.Range(0, -1)), Quaternion.identity);
            // set camera follow player
            cinemachineVirtualCamera.Follow = playerObj.transform;
            // dont destroy player game object when load new scene
            DontDestroyOnLoad(playerObj);
        }
        else
        {
            Debug.Log("new player : " + PlayroomKit.MyPlayer().GetProfile().name);
            // if multiplayer, add player to the game;
            PlayroomKit.OnPlayerJoin(AddPlayer);
        }
    }

    private void AddPlayer(PlayroomKit.Player player)
    {
        // create player game object
        GameObject playerObj = (GameObject)Instantiate(playerPrefab, new Vector2(Random.Range(-1.5f, 1.2f), Random.Range(0, -1)), Quaternion.identity);
        // dont destroy player game object when load new scene
        DontDestroyOnLoad(playerObj);

        // set player color
        playerObj.GetComponentInChildren<SpriteRenderer>().color = player.GetProfile().color;
        Debug.Log(player.GetProfile().name + " joined the game!" + " id: " + player.id);

        // add player to player list
        PlayerDict.Add(player.id, playerObj);
        players.Add(player);
        playerGameObjects.Add(playerObj);

        playerJoined = true;

        // remove player from player list when player quit
        player.OnQuit(RemovePlayer);

        // set camera follow player
        if (player.Equals(PlayroomKit.MyPlayer()))
        {
            cinemachineVirtualCamera.Follow = playerObj.transform;
        }
    }

and of course, before doing the last update, everything works, but I hope that what I have sent you is enough for you to help me.

SaadBazaz commented 5 months ago

@Huskago Reproducing this. In the meanwhile, can you test on the latest version of PlayroomKit from the one you currently have? There could possibly be an update.

Huskago commented 5 months ago

But my problem arose when I upgraded to the latest version of Playroom, v0.0.10, because before doing so, everything was working fine. Before upgrading, I was at v0.0.9.

SaadBazaz commented 5 months ago

Until we resolve this, you can rollback to 0.0.9. There may be less features, but apparently it's stable.

momintlh commented 5 months ago

The issue was with how we were handling the callback with OnPlayerJoin(), basically they are not being invoked after added to the list of callbacks. The real issue is how this was working in previous releases as this was never changed since the first release lol. I have tested it with v0.0.10 and have the fix, will open a PR =)

momintlh commented 5 months ago

hey @Huskago can you test this https://mmntlh.itch.io/playroom-demo.