PhotonNetwork.Instantiate with parameters works for MasterClient not for Client

Options
Hi guys,

English is not my native language, please forgive my mistakes :)

I am really new to Unity Gamedevelopment and PUN. Currently I am working on my first project, which is a two Player Game with MOBA elements involved.

For two days I am stuck on a problem I was not able to solve with the help of googel, so I want to ask you for help.

When I test my current state of the game, there are several spawn points on the map. There are part of the scene. Every Spawnpoint has a "WaveSpawn" script with the SpawnCreep() function.

The spawned Creeps have waypoints and use a NavMesh. They also have other parameters, which are assigned directly after spawning (see code). The initial values for these are set in the Inspector.
    void SpawnCreep()
    {
        if (PhotonNetwork.IsMasterClient) {
            Debug.Log("MasterClient =" + PhotonNetwork.IsMasterClient);
            GameObject newAgent = PhotonNetwork.Instantiate("CreepBlue", transform.position, Quaternion.identity);
            newAgent.GetComponent<CreepMovement>().goal = goTo.transform;
            newAgent.GetComponent<CreepMovement>().goal2 = goTo2.transform;
            newAgent.GetComponent<CreepMovement>().startHealth = startHealth;
            newAgent.GetComponent<CreepMovement>().aggroRange = aggroRange;
            newAgent.GetComponent<CreepMovement>().attackRange = attackRange;
            newAgent.GetComponent<CreepMovement>().fireRate = fireRate;
            newAgent.GetComponent<CreepMovement>().redTag = targetColor;
}
For Player1 / MasterClient/ Blue everything works correct. Creeps are spawned and the have their waypoints and other parameters set.
For Player2 / Client / Red the spawn works, but the "CreepBlue" Prefab is spawned without any parameters. No waypoints whatsoever.
The Prefab has a PhotonView Component.

Any ideas what is wrong with my setup.

PS: I am pretty sure it is not best practice to call GetComponent so often. Feel free to show me a better way.

Thanks
Simvanlee

Comments

  • OneManArmy
    Options
    Hi, you can send RPC (buffered - if other clients can join after instantiation) to configure AI.
  • JohnTube
    JohnTube ✭✭✭✭✭
    edited September 2019
    Options
    Hi @SimVanLee,

    Thank you for choosing Photon!

    As @OneManArmy suggested you need to send the custom data manually.
    It is not done for you out-of-the-box.

    You could do this using different ways:

    1- Instantiation data
    2- RPC (what @OneManArmy mentioned)
    3- RaiseEvent
    4- Room properties

    I think option 1 is fine.

    And I recommend you get the CreepMovement once and reuse it to gain some performance (in case you missed this tip):
        void SpawnCreep()
        {
            if (PhotonNetwork.IsMasterClient) {
                Debug.Log("MasterClient =" + PhotonNetwork.IsMasterClient);
                GameObject newAgent = PhotonNetwork.Instantiate("CreepBlue", transform.position, Quaternion.identity);
                CreepMovement creepMovement = newAgent.GetComponent<CreepMovement>()
                creepMovement.goal = goTo.transform;
                creepMovement.goal2 = goTo2.transform;
                creepMovement.startHealth = startHealth;
                creepMovement.aggroRange = aggroRange;
                creepMovement.attackRange = attackRange;
                creepMovement.fireRate = fireRate;
                creepMovement.redTag = targetColor;
    }
  • Hi OneManArmy and JohnTube,

    thank you so much for the quick help !
    I will post an Update here, when I tested and solved my issue.
  • I finally SOLVED it. Thanks to you tipps :smile: so Thanks again.

    What I used:
    Added an RPC directly after my Instantiate in the "SpawnCreep" method.
    
                PhotonView PV = newAgent.GetComponent<PhotonView>();
                PV.RPC("UpdateCreepStats", RpcTarget.All, new object[] { startHealth, fireRate, aggroRange, attackRange, targetColor, goTo , goTo2 });
    and because this "new Agent" will have the "CreepMovement" Script I added this in the CreepMovement:
    
        [PunRPC]
        private void UpdateCreepStats( float startHealth,  float fireRate, float aggroRange, float attackRange, string targetColor, Vector3 goTo, Vector3 goTo2)
        {
            this.goal = goTo;
            this.goal2 = goTo2;
            this.startHealth = startHealth;
            this.aggroRange = aggroRange;
            this.attackRange = attackRange;
            this.fireRate = fireRate;
            this.redTag = targetColor;
        }
    
    Just had to change my waypoint GameObjects to Vector3 because I did not have the patience to learn customtype serialisation.

    <3