0

I'm developing a multiplayer game based on turns. So I have an script named gameController which is the one who has the global timer to alter the turns and also choose which players are attacking and which players are defending in the current turn. This script is a singleton attached to a gameObject with a network identity on it (This network identity has no checkboxes marked to be controller by the server). I tried to have this game object spawned to all the clients when the server connects and also to have the game object already in the scene (booth cases aren't working).

Well, the main problem is that in the gameController script I have a checker in the update function to check if any new player is connected. In case is connected, it should call a syncEvent named EventOnNewPlayerAddedDelegate (I tried to call it directly, also using [command] and using [ ClientRpc]) to let the players know that they have to call their function named "OnRegisterPlayer", which is a function in their own player script that calls a function on gameController script, passing the player object (I tried via command and rpc also), something like this: GameController.instance.RegisterPlayer(this);

So anyone knows how can I trigger this SyncEvent to register the player to a non-player object controlled by the server?

Thank you very much.

I attach here a brief of booth scripts to make it easier to understand:

GameController:

public class GameController : NetworkBehaviour
{

    public delegate void OnNewPlayerAddedDelegate();
    [SyncEvent]
    public event OnNewPlayerAddedDelegate EventOnNewPlayerAddedDelegate;

    List<GamePlayerManager> players = new List<GamePlayerManager>();

    public static GameController instance { get; private set; }

    float timePerTorn = 30f;
    bool isCountdown = false;
    [System.NonSerialized]
    public float countdownTime;
    int roundNumber = 0;

    int NumberOfPlayers;
    int NumberOfPlayersChanged;

    void Awake()
    {
        Debug.Log("Awaking ");
        if (instance != null)
        {
            Debug.Log("Destoring ");
            DestroyImmediate(this);
            return;
        }
        instance = this;
    }
    // Use this for initialization
    void Start()
    {
        if (!isServer)
        {
            return;
        }

        players = new List<GamePlayerManager>();
        StartCountdown(5f);//20
    }

    void Update()
    {
        if (isServer)
        {
            if (isCountdown)
            {
                countdownTime -= Time.deltaTime;
                if (countdownTime <= 0)
                {
                    AlterGlobalTurns();
                }
            }

        }
        NumberOfPlayers = NetworkManager.singleton.numPlayers;

        if (NumberOfPlayersChanged != NumberOfPlayers)

        {
            Debug.Log("num of players changed ---> " + NumberOfPlayers);
            NumberOfPlayersChanged = NumberOfPlayers;

            EventOnNewPlayerAddedDelegate();

            //RpcNewPlayerAdded();
            //CmdNewPlayerAdded();

        }
    }
    [ClientRpc]
    void RpcNewPlayerAdded()
    {
        Debug.Log("---------------- RpcNewPlayerAdded ------------");
        EventOnNewPlayerAddedDelegate();
    }
    [Command]
    void CmdNewPlayerAdded()
    {
        Debug.Log("---------------- CmdNewPlayerAdded ------------");
        EventOnNewPlayerAddedDelegate();
    }
    public void RegisterPlayer(GamePlayerManager player)
    {
        Debug.Log("player ---> " + player.name);
        if (players.Contains(player))
        {
            return;
        }
        Debug.Log("players ---> " + players);
        players.Add(player);
    }
}

PlayerScript:

public class GamePlayerManager : NetworkBehaviour
{

    [System.NonSerialized]
    public bool isPlayingOnTorn = true;

    void Awake()
    {
        GameController.instance.EventOnNewPlayerAddedDelegate += OnRegisterPlayer;   

    }
    private void Start()
    {

        if (!isLocalPlayer)
        {
            return;
        }
    }
    public override void OnStartServer()
    {
        GameObject gc = (GameObject)Instantiate(NetworkManager.singleton.spawnPrefabs[2], transform.position, transform.rotation);
        NetworkServer.Spawn(gc);

    }

    void OnRegisterPlayer(){

        if (isLocalPlayer)
        {
            //GameController.instance.RegisterPlayer(this);
            //RpcRegisterPlayer();
            CmdRegisterPlayer();
        }
    }

    [Command]
    void CmdRegisterPlayer(){
        Debug.Log("-------------Command Register player -------------");
        GameController.instance.RegisterPlayer(this);

    }
    [ClientRpc]
    void RpcRegisterPlayer()
    {
        Debug.Log("------------- RPC REgister Player -------------");
        GameController.instance.RegisterPlayer(this);
    }
}

1 Answer 1

0

I think I already saw the problem here.

The problem is that GameController is spawned by the playerScript(just if the player is also the host) in the function OnStartServer(). So the first problem is that the GameController doesn't detect the host player because it is not a new connections. And the second problem is when a second client is connected, the GameController detect the client connection and send the event signal faster than the client wakeup, so when the client is already working the signal is gone.

I solved the problem deleting this signal and checking directly in the playerScript if the GameController exists then check if the player is already registered.

My question now is that if there is anyway to instance and awake the server objects before the player(Host), which I understand that the answer is "no" because as the player is the host it needs to be running to have the server. And the second question is if there is anyway or any signal to know that a new player is connected, and wait until it is awaked.

Thank you very much. You can check the hole thread at Unity forums: https://forum.unity.com/threads/multiplayer-syncevent-problem-with-non-player-objects.589309/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.