Has the way of sending array of types changed?

Options
Hi,

I've upgraded my unity and photon to latest versions and now it seems that the array of int[] that I was sending thru stream is not working anymore... has the way of sending and receiving arrays changed?

Example:
   void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.isWriting)
        {
                //We own this player: send the others our data
                stream.SendNext((int[])characterEnums.states);
                stream.SendNext(transform.position);
                stream.SendNext(transform.rotation);
                stream.SendNext(weaponState.aimTarget.position);
                stream.SendNext((Vector3)controllerScript.directionVector);
        }
        else
        {
            	//Network player, receive data
			
                characterEnums.states = (int[])stream.ReceiveNext();
                correctPlayerPos = (Vector3)stream.ReceiveNext();
                correctPlayerRot = (Quaternion)stream.ReceiveNext();
                correctAimTarget = (Vector3)stream.ReceiveNext();
                controllerScript.directionVector = (Vector3)stream.ReceiveNext();
	}
		

    }



Thanks!

Comments

  • Tobias
    Options
    No, that should not be changed at all.
    PUN 1.16 and Photon 3 final?
    Do you get a error message on either client or server?
  • tutibueno
    Options
    PUN 1.16.2 (from asset store) and Photon Cloud. No errors at all. It simply stopped working.
  • Tobias
    Options
    I am using the cloud and int[] regularly. So that really shouldn't be an issue.

    What about:
    stream.SendNext((int[])characterEnums.states);

    characterEnums? That sounds like an enum. Is states really a int[] in that case?
    Can you post the definition of that?
  • tutibueno
    Options
    Yes, it sounds a little confusing indeed. I should have named the class as CharacterStates. But this is the class definition:
    using UnityEngine;
    using System;
    using System.Collections;
    using System.IO;
    using System.Text;
    
    public enum CharacterStateCustom
    {
        Idle = 0,
        Walking = 1,
        Trotting = 2,
        Running = 3,
        Jumping = 4,
    	Attacking = 5,
    	PointingGun = 6,
    	Firing = 7,
    	HolstingGun = 8,
    	Dead = 9,
    	Burning = 10
    
    }
    
    public class CharacterEnums : MonoBehaviour
    {
    
    	public CharacterStateCustom state;
    	int _statesArrayInt = 0;
    	public string MyName = "DefaultName";
    	public int[] states = new int[4];
    	public float myFloat;
    	
    
    	/*
    	 Bad: states[0]: basics states (CharacterState)
    	 		 [1]: 0 = not shooting, 1=shooting
    	 		 [2]: 0 = not using flashlights, 1 = using flashlights
    	 		 [3]: weapon slot;
    	 		 
    	  
    	 
    	
    	
    	 Pathfinder(the enemy)
    	 states[0] - 0 = waiting for player, 1 = attacking, 9 = dead, 10 = (and already exploded)
    	 states[1] - 1 = foldout turret, 0 = folding turret
    	 states[2] - 0 = not shooting, 1 = shooting
    	 
    	 */
    
    	// Use this for initialization
    	void Start ()
    	{
    		if(transform.tag == "Enemy")
    			transform.name = MyName;
    		
    	}
    
    	void Update ()
    	{
    		
    		//myInt = statesArrayInt;
    	}
    	
    	
    }
    

    Makes sense?
  • Tobias
    Options
    Yep, now! :)
    I will have to give it a try. Can this be reproduced in the editor or which platform do I have to compile for?
  • tutibueno
    Options
    Yes, you can reproduce it using the editor. No need to do on other platform.
  • Tobias
    Options
    I tried to but can't reproduce this. int[] should work as before and for me, it does. I can send it as first element or as last...

    Maybe something in your code changed?
    Are all scripts still attached as expected?
    You could possibly add debug out to print the stream you send and receive.
    I'm sorry I can't help better...
  • Tribio
    Options
    I still can't send an array, here is my code:
    using UnityEngine;
    using Photon.Pun;
    
    public class PUNTest : MonoBehaviour, IPunObservable
    {
    
        [SerializeField] int[] array;
        [SerializeField] float increaseTimer = 1f;
    
        private void Start()
        {
            array = new int[] { 0, 0, 0};
        }
    
        private void Update()
        {
            if (PhotonNetwork.IsMasterClient)
            {
                increaseTimer -= Time.deltaTime;
                if (increaseTimer < 0f)
                {
                    // increase all the int in the array
                    for(int i = 0; i < array.Length; i++)
                        array[i]++;
    
                    // restart countdown
                    increaseTimer = 1f;
                }
            }
        }
    
    
    
        public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
            if (stream.IsWriting)
            {
                stream.SendNext(array);
                stream.SendNext(array[0]); // if I remove this, stream.IsReading will be always false (why??)
            }
            else if (stream.IsReading)
            {
                array = (int[])stream.ReceiveNext();
    
                Debug.Log("-- Check start --");
                for (int i = 0; i < array.Length; i++)
                {
                    Debug.Log(array[i].ToString());
                }
                Debug.Log("-- Check ended --");
    
                Debug.Log("Extra int: " + ((int)stream.ReceiveNext()).ToString());
            }
    
        }
    

    In the client, you can see the logs, the Extra int is increasing, while the array isn't at all!
    What am I doing wrong?
  • Tobias
    Options
    Let us know which version of PUN 2 you use.
  • Tobias
    Options
    I think you can work around this issue by setting the PhotonView synchronization to Unreliable instead of using Unreliable on Change.
    Or: Don't always use the same array variable in SendNext.
  • Tribio
    Options
    I'm using v2.28.3 (03. March 2021).
    Tobias wrote: »
    I think you can work around this issue by setting the PhotonView synchronization to Unreliable instead of using Unreliable on Change.
    Or: Don't always use the same array variable in SendNext.

    I'm going to try this then...
  • Tribio
    Options
    Switched to "Unreliable" and it works!
    that's good enough for me, thanks!
  • Tribio
    Options
    I've double-checked: I was actually using "Reliable On Change", and it is actually what I need now, I have to be sure that the message is received, even with a bit of delay.
    Using "Unreliable on Change" it works as well, but the stream.IsReading is true twice, even if the array has been updated only once.
    I don't want to use the Unreliable one...
  • Tobias
    Options
    There is no "Reliable on Change".
    Unreliable means that updates normally arrive but if there is loss (maybe 1%), some updates may be missing. This is totally fine for moving objects.

    If objects rest, most of the time, then "Unreliable on change" is likely better but it does not support sending arrays of data.
  • Tribio
    Options
    I'm so sorry, I meant "Reliable Delta Compressed".
    I understand I can use Unreliable, but I really need to be sure that the data is received correctly, and any update happens rarely during the game.
  • Tobias
    Options
    The problem is that there is no proper support for arrays in the updates. PUN does not detect properly if the content of the arrays differs and thus doesn't know if it has to send an update or not.

    We can't add a feature like this without making it usable for all and some testing, so it will take a while.
    You could either add it yourself (have a look at the AlmostEquals code) or you can copy the data from the array (which is hopefully short, as it will be in a lot of updates, potentially) and write the values directly into the PhotonStream. That should work.
  • Tribio
    Options
    I see, I'll try something then, thanks for the help!