1

I'm making a multiplayer bomberman game using Unet. I'm trying to create a ready check to start the game when everyone else is ready.

I tried using a sync var,Command and Rpc calls, but nothing seems to work properly.

When using a Cmd call this way (called when a player pushes the ready button)

[Command]
private void CmdIncreaseReady()
{
        IncreaseReady();
        RpcIncreaseReady();

}
[ClientRpc]
private void RpcIncreaseReady()
{
    IncreaseReady();
}
private void IncreaseReady() {

    playersReady++;
    //ChechAllReady();
}

When pressed on the client, everyone's counter is 0 (it should be one), and when pressed on the server, the server's counter is 2 and the client 1.

I've tried to call Cmd when !isServer and Rpc when it is, and the result is that stills doesn't update when pressed on the client, but it is updated correctly (the counter is 1 on both), when pressed on the server.

if (!isServer)
    {
        CmdIncreaseReady();
    }
    else
    {
        RpcIncreaseReady();
    }

If I delete the IncreaseReady() call on the Cmd, the result is the same than above.

I've tried too to use a [SyncVar] to the counter, with and without a hook, passing 1 on the hook (to increment the counter that amount) and passing the already counter incremented and set variable to that number, nothing seems to work.

I really don't know how to make it work anymore. Could someone help me? I'm really desperate with this. I've searched everywhere.

The script is the game manager, when every client has his version, it has a Netework Identity and has Local player Authority.

I've tried another approach, passing the increased number on the Cmd and Rpc, and inside the functions, just set the playersReady = i. Even that doesn't work.

[UPDATE]:

So, I've found the specific problem that I want to solve, and it's to Sync non-player objects properties. I've tried to spawn the Game Manager with Client Authority, and now it seems to sync correctly only on the server, the client stills doesn't call the Cmd propety.

Looking at the Debug mode in the inspector, I've been able to see that on the server, hasAutority is true, but is false on the client.

This is a fragment of the code on the player script where I spawn the game manager:

 void Update()
{
    if (!sceneLoaded)
    {
        if(isServer & SceneManager.GetActiveScene().name == "Main Scene")
        //if (SceneManager.GetActiveScene().name == "Main Scene")
        {
            if (connectionToClient.isReady)
            {
                var go = (GameObject)Instantiate(gameManager);
                NetworkServer.SpawnWithClientAuthority(go, connectionToClient);
                go.transform.SetParent(null);
                //go.GetComponent<NetworkIdentity>().AssignClientAuthority(connectionToClient);
                globalManager = go.GetComponent<FSM>();

                //SetFSM();

                sceneLoaded = true;
            }

        }
    }
  • A few things, although see my Answer below. "It could be that the script must be attached to the player spawned?" - by default that's true, unless you assign authority - see heading "Client Authority for Non-Player Objects" https://docs.unity3d.com/Manual/UNetConcepts.html . It was done this way to try to encourage keeping Networking code together instead of noodled all over the place. – Jethro Apr 10 '18 at 13:28
  • YOur SYncVar likely wasn't working because when you add a Hook, you have to manually set the local variable. This is done so you can look at the old state incase you need that. https://answers.unity.com/questions/1159005/unet-syncvar-with-hook-not-syncing-on-clients.html – Jethro Apr 10 '18 at 13:29
  • Don't use !isServer - it's misleading. use isClient and isServer. When things aren't spawned yet, you may be confused to see your client evaluate isClient == false – Jethro Apr 10 '18 at 13:31
  • Yes, indeed it seems like what I'm trying to do is set Client Authority for Non-Player Objects. I've tried to spawn the game manager with authority, and now it works, but only for the server, not for the client. – Zoroastiran Apr 11 '18 at 07:51
  • ok cool glad you're one step closer. I think you'll have to update the Question to match the exact problem you're having now to get any further help, as I for one am lost as to where your code is. (: keep it specific if you can! – Jethro Apr 11 '18 at 09:48

1 Answers1

1

UNET has a Lobby system which does exactly what I think you want. It has a per-player ready state, so that the game starts when all players are ready.

I would recommend using that, as it saves you having to reimplement things like dropping out, rejoining etc...

Jethro
  • 3,029
  • 3
  • 27
  • 56