Serialize floats in 2D vs Position/Rotation

Options
Hello, first off, I'm enjoying using the PUN+ plugin, kudos guys! Now a couple quick questions :)

I'm new to network programming, so these are probably novice questions, but I couldn't find the answers through the forums/google, so I figured I'd ask them here.

1. I'm working on a top-down 2D multiplayer game for mobile iOS/Android and want to send position/rotation info so I plugged in the character's transform into the PhotonView component in the inspector, set it to Unreliable, and selected Position and Rotation. That's sending a Vector3(x,y,z) and a Quaternion(x,y,z,w) I presume [i.e. 7 floats]. In an effort to reduce data transfer, I was thinking it would be better to plug the character's script into the PhotonView instead, set it to Unreliable, and then serialize the 3 floats we actually need to send:
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
		if (stream.isWriting){ //local character, send the others our data
			stream.SendNext(transform.position.x);
			stream.SendNext(transform.position.y);
			stream.SendNext(transform.eulerAngles.z);
		} else { //network character, receive data
			float tempX = (float)stream.ReceiveNext();
			float tempY = (float)stream.ReceiveNext();
			float tempZ = (float)stream.ReceiveNext();
			transform.position = new Vector3(tempX, tempY, 0);
			transform.eulerAngles = new Vector3(0, 0, tempZ);
		}
	}
Is that a better way to send the position and rotation? I'd think the client impact would be insignificant, but the data transfer would be more than halved. Is that correct?

2. Follow up question to #1, assuming that the 3 floats method is better/preferred, the character script is then set to Unreliable, so if I send RPC calls from that same character script, will they be Unreliable too or will they be sent as Reliable Data Compressed anyway since they're RPC calls?

Thanks for the awesome plugin and help!

Comments

  • Tobias
    Options
    1. As you skip some values, the traffic would be reduced. You can check this by keeping an eye on the "Stats GUI". Search the PUN project for "gui" and you will find it.
    See: http://doc.exitgames.com/en/pun/current ... -stats-gui

    2. OnPhotonSerializeView is never treated as RPC. You can use Unreliable or "Unreliable on Change".
    See: http://doc.exitgames.com/en/pun/current ... e-overview "Observe Options", too.
  • Ok cool, so for #1 that is a savings, thanks!

    For #2, I didn't make the question clear enough and I'm not 100% sure from the docs, or I'm missing something, so I'll try again :D
    So, in the inspector, on a game object with a PhotonView component that has "myScript" as the observed object and it's set to Unreliable. In myScript [inherits from Photon.Monobehaviour] it has both OnPhotonSerializeView() and then outside of that it has SomeMethod() that calls an RPC method MyRPCmethod() like:
    void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
          //send and receive code for pos/rot
    }
    ...further down the same script...
    void SomeMethod(){
         photonView.RPC("MyRPCmethod", PhotonTargets.All);
    }
    
    [RPC] public void MyRPCmethod(){
         //code to do stuff on each client
    }
    
    Do those RPC calls get sent as Unreliable (since that's what set in the PhotonView component in the inspector) or are they sent as Reliable (since they're RPC calls)?

    Thanks!
  • vadim
    Options
    Hi,

    RPC's are always reliable.
    OnPhotonSerializeView is reliable or not depending on settings in inspector.
    They work independently.
  • Perfect, thanks!