does InstantiateSceneObject always create a fresh instance

Options
Hi there,

at the start of my game, the master client instantiates a bunch of scene objects that then sync data use OnPhotonSerializeView. This mostly works fine but occasionally, after a few battles, I get some really strange behaviour. For example, on one object I am syncing its health. I am outputting its health and can see that it is 1000 and immediately after the OnPhotonSerializeView is setting the health to 1.177621E-38.

This only seems to happen after I have completed a few battles, each in their own room, so I was wondering if it was possible with way Photon pools and instantiates objects for OnPhotonSerializeView calls to somehow get carried from one room to the next?

Thanks

Comments

  • BigGameCo
    Options
    bump
  • BigGameCo
    Options
    I could really use some help with this. Been stuck on it for a few days and am close to tears.

    I found something that may or may not be relevant. When the bug I outlined above occurs, I also noticed some strange outputs from my code that tries to start the battle at the same time. When I detect that all the scene objects have been instantiated, the master client calls
    photonView.RPC("ReadyToStartBattle", RpcTarget.All, PhotonNetwork.Time + 1f);
    

    which calls
    [PunRPC]
        void ReadyToStartBattle(double timeToInit)
        {
            timeToInitBattle = timeToInit;
            InvokeRepeating("StartBattleAtSameTime", 0f, .05f);
        }
    
        void StartBattleAtSameTime()
        {
            PhotonNetwork.AutomaticallySyncScene = false;
            Debug.Log("StartBattleAtSameTime PhotonNetwork.Time "+ PhotonNetwork.Time + " and 
         timeToInitBattle "+ timeToInitBattle);
            if (PhotonNetwork.Time > timeToInitBattle)
            {
                
                CancelInvoke("StartBattleAtSameTime");
                //start the battle
            }
        }
    


    Now most of the time my debug line outputs something like
    StartBattleAtSameTime PhotonNetwork.Time 3421259.411 and timeToInitBattle 3421260.01
    

    but when the bug above occurred, I got
    StartBattleAtSameTime PhotonNetwork.Time 3422212.812 and timeToInitBattle -4.31149270387473E-264
    

    What could cause PhotonNetwork.Time + 1f to = -4.31149270387473E-264?

    I feel like these issues might be related, but I don't think the issue I raised in the first post is caused by this time issue because I can see in the logs that health is being set to strange values before the start battle code is even run.

    It's also worth noting that all this only ever happens on the non-master client. So for example, in this code
    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
            if (!boolISetWhenInitIsComplete) return;
            if (stream.IsWriting)
            {
                float currentHealth = -1000f;
                if (myHealthScript != null)
                {
                    float health = myHealthScript.GetCurrentHealth();
                    if (!float.IsNaN(health))
                    {
                        currentHealth = health;
                    }
                }
                Debug.Log("sending health of " + currentHealth );
                stream.SendNext(currentHealth);
            }
            else
            {
                float currentHealth = (float) stream.ReceiveNext();
                Debug.Log("received health of " + currentHealth );
                if (myHealthScript != null && (int)currentHealth != -1000)
                {
                    myHealthScript.SetKeepHealth(currentHealth);
                }
                else
                {
                    Debug.Log("myHealthScript is null");
                }
            }
        }
    

    Please excuse all the extra checks I've added while trying to debug this. The main point I want to make is when the bug occurs, the send Debug outputs the correct values (eg 3000) while the receiving debug outputs stranges values that jump around even when the sending value hasn't changed.
    received health of 2165.25
    received health of 2165.25
    received health of 691.8351
    received health of 259.2783
    received health of 169.2783
    received health of 169.2783
    received health of 2105.25
    received health of 2105.25
    received health of 0
    received health of 0
    received health of 1.177621E-38
    received health of 1.177621E-38
    received health of 1.177621E-38
    received health of 1.177621E-38
    

    Does anyone know what the problem might be?

    Thanks
  • S_Oliver
    S_Oliver ✭✭✭
    Options
    Hi if you take a closer look at
    PhotonNetwork.Time
    
    /// <summary>
    /// Photon network time, synched with the server.
    /// </summary>
    /// <remarks>
    /// v1.55<br/>
    /// This time value depends on the server's Environment.TickCount. It is different per server
    /// but inside a Room, all clients should have the same value (Rooms are on one server only).<br/>
    /// This is not a DateTime!<br/>
    ///
    /// Use this value with care: <br/>
    /// It can start with any positive value.<br/>
    /// It will "wrap around" from 4294967.295 to 0!
    /// </remarks>
    Means the behaviour you are encountering is the correct behaviour.
    Maybe a better choice is using
    PhotonNetwork.ServerTimestamp
    
    .
    ServerTimestamp is made for stuff you wanna do.

    But this not the reason why your health values act so strange.
    If you can post you health code i could check it.
    To give you a simple example, take a look at this https://github.com/SradnickDev/Photon-Misc/blob/master/Assets/Player/Health/Health.cs



  • BigGameCo
    BigGameCo
    edited April 2020
    Options
    Thanks for the reply. I did read the documentation but I still couldn't work out how my value was becoming negative, but maybe it was caused by going beyond the max float size or something. Anyway, I will change it to use ServerTimestamp - thanks.

    Regarding the other issue, the mana code might be a little cleaner than the health code, so I will post it below. Both objects bug out at the same time.

    So when the battle starts, the master client makes 2 scene instances of the ManaManager object - one for themselves and another for the other player. The starting master client has a player number of 1, the other player is player 2. I set this in OnCreatedRoom. That mana object has this code on it.
    public void OnPhotonInstantiate(PhotonMessageInfo info)
        {
    	    object[] instantiationData = info.photonView.InstantiationData;
    	    int ownerPlayerNum = (int)instantiationData[0];
    	    Debug.Log("mana OnPhotonInstantiate with view ID "+info.photonView.ViewID + " and ownerPlayerNum  of "+ ownerPlayerNum);
    	    ownerPlayerNumber = ownerPlayerNum;
    	    if (ownerPlayerNumber == TurnManager.Instance.GetPlayerNumber(true))
    	    {
    		    Debug.Log("it was our mana generator");
    		    //set up some references to this script	 
    //set up the event listener
    		    this.gameObject.name = "OurManaManager";
    	    }
    	    else
    	    {
    		    //set up some references to this script
    		    this.gameObject.name = "EnemyManaManager";
    	    }
    	    currentMana = 6f;
    //regen rate is set when the mana generator is initialised
    	    regenRate = 0f;
    	    maxMana = 10f;
    	    photonView = PhotonView.Get(this);
    	   
    	   //run check to see if the construction of all the objects is finished
        }
    
        
        public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
    	    if (!initialised) return;
    	    if (stream.IsWriting)
    	    {
    		    Debug.Log("mana sending " + currentMana + " ownerPlayerNumber is " +ownerPlayerNumber+ " and photon view ID is " +photonView.ViewID);
    		    stream.SendNext(currentMana);
    	    }
    	    else
    	    {
    		    Debug.Log("mana received and count was " + stream.Count+ " ownerPlayerNumber is " +ownerPlayerNumber+ " and photon view ID is " +photonView.ViewID);
    		    if (allowManaSync)
    		    {
    			    currentMana = (float) stream.ReceiveNext();
    			    Debug.Log("mana received value was " + currentMana+ " ownerPlayerNumber is " +ownerPlayerNumber+ " and photon view ID is " +photonView.ViewID);
    		    }
    		    
    	    }
        }
    


    I wait to see if all the required objects have been created
    public void CheckIfConstructionIsFinished()
        {
          
            if (catapultShoot.manaIncreaser == null || catapultShoot.enemyManaIncreaser == null)
            {
                Debug.Log("manaIncreaser not ready");
                return;
            }
            Debug.Log("manaIncreaser ready");
         
            BothPlayersFinishedConstruction();
        }
    


    And when everything is ready I initialise the mana generators
    manaIncreaser.Init();
     enemyManaIncreaser.Init();
    

    And the Init code looks like this
    public void Init(){
    		
            int numberOfSections = LastBattleData.Instance.numberOfCastleSections;
            float amountPerSectionPerSecond = .1f;
            regenRate = (amountPerSectionPerSecond * numberOfSections) * manaIncreaseInterval;
            originalRegenRate = regenRate;
            Debug.Log("mana generator initialised with number of section = "+ numberOfSections + " and regen rate of "+ regenRate);
            StartCoroutine(IncreaseMana());
            initialised = true;
    	}
    	IEnumerator IncreaseMana(){
    		while(GameStateManager.Instance.state != GameStateManager.State.GameOver){
    			if(currentMana + regenRate  < maxMana){
    				currentMana += regenRate;
    			}else{
    				currentMana = maxMana;
    			}
    			NotifyListeners();
    			yield return Yielders.Get(manaIncreaseInterval);
    		}
    	}
    


    When the bug occurs, I get the sender saying
    mana sending 6
    mana sending 6.03
    mana sending 6.045
    mana sending 6.074999
    

    and the receiver saying
    mana received -122.8755
    mana received -1.305135E-17
    mana received 6.810797E+22
    mana received 28897.13
    mana received 1.298574E-33
    

    I'm getting the exact same output for both mana generators.

    Thanks for the link to the example - that code looks nice and clean. I would prefer not to recode everything at this point because it is working most of the time but if I can't get to the bottom of it I guess I will have to start from scratch.

    Thanks again
  • BigGameCo
    Options
    another strange thing - when my mana value is received, it says the stream.Count is 4 but I am only sending 1 value. It says this even if the bug above hasn't occurred.
  • S_Oliver
    S_Oliver ✭✭✭
    Options
    Can you provide a minimal project to check it by my own ?
  • BigGameCo
    Options
    no sorry the project is way too big to be able to just pull out the relevant parts into its own project.
  • S_Oliver
    S_Oliver ✭✭✭
    Options
    Did you tried a simple test in a new script with just sending and receiving 1 float or something ?
  • BigGameCo
    Options
    to check the stream count? I'm getting the same result with another object that sends 1 float - when receiving, the stream count is 4. That's not my main issue though, I just thought it was strange. Main issue is the mana going crazy sometimes.
  • BigGameCo
    Options
    can anyone help with this? I know it's a strange one but I'm not sure what else I can try to fix this, so I could really use some suggestions.
  • S_Oliver
    S_Oliver ✭✭✭
    Options

    Hit me on discord Daegit#4499 or link to the whole mana / health script.
    Maybe there is something.

  • BigGameCo
    Options
    ok thanks. Friend request sent (btrikojus). I'll do some more testing and let you know if I find anything significant.