SYNC INSTANCIATE ENEMY (ZOMBIE) with Photon PUN 2

elyes
elyes
hi to the whole community i hope you are doing well!

I'm trying to create a multiplayer FPS game with Pun 2 photon at this moment I can instantiate the players as needed, shoot them, take damage etc ...

I want to add enemies (zombie) in the game.

Until then I managed to instantiate the zombies by attaching a script to game scene and using photonNetwork.InstantiateRoomObject (Combine.Path ...), I can observe them with all clients correctly.

the problem is that when I want to change the names of the zombies (to store them in a GameManager dictionary (ZombieName, Scriptzombie)), it changes only with the master client!

I tried instantiation with RPCs but still nothing, the other clients will receive zoombie.clone in the hierarchy, I have tried everything I have read all the forums but still nothing, that someone can help me it's been several days and night that I try to solve this problem.
Credibly.

Comments

  • anyone can help please ?
  • ThomasKao_XRSpace
    edited December 2020
    for the information at the time of Instantiation you can use
    PhotonNetwork.Instantiate(string prefabName, Vector3 position, Quaternion rotation, byte group=0, object[] data=null);
    

    and then you can read the data from photonView.InstantiationData in either Start() or
    //IPunInstantiateMagicCallback 
    public void OnPhotonInstantiate(PhotonMessageInfo info)
    {
        object[] instantiationData = info.photonView.InstantiationData;
        // ...
    }
    

    After the Instantiation stage, you might encounter sync problem because the ownership.
    As you Instantiate those as Room/Scene object, you migt want to give ownership to specific player just after creation.
  • thank you very much for answering me,

    your trick works like magic, I can now sync a single zombie for all clients and shoot them, kill them etc ...
    but the public void function OnPhotonInstantiate (PhotonMessageInfo info) does not work if I instantiate two zombies, I cannot rename each by itself, if I change name both zombies will have the same !!

    to better understand me:
    in gameManager I instantiate the two zombies as follows:

    for (int i = 0; i <2; i ++)
    {
    nameZomby = i.ToString ();
    zombieInstantiated = PhotonNetwork.InstantiateRoomObject (Path.Combine ("PhotonPrefabs", "zombie"), new Vector3 (i, 0, i), Quaternion.identity); //. transform.name = i.ToString ();
    }

    PS: if I change the name by instantiating them, it will only concern the master client !!

    and on the zombie script:
    public void OnPhotonInstantiate (PhotonMessageInfo info)
    {
    object [] instantiationData = info.photonView.InstantiationData;

    transform.name = GameManager.Instance.nameZomby;

    // GameManager.RegisterZombie (transform.name, GetComponent <ZombieScript> ());

    / * foreach (GameObject go in GameRoomManager.Instance.zombieInstantiated)
    {
    transform.name = go.name;
    } * /
    }

    it also works only on the master client!
  • As I mentioned, put the zombie name inside Instatiate data
    InstantiateRoomObject (prefabName,  position, rotation, 0, new object[]{"zombieName"})
    
    and then
    var zName = (string)photonView.InstantiationData[0];
    
  • it works perfectly again thank you very much your constant help, you saved my little project i hope to make you a faver one day ThomasKao_XRSpace