IndexOutOfRangeException

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

IndexOutOfRangeException: Array index is out of range.

Jesse_0722
2015-01-19 14:43:32

Hello all, I overrided the LeaveRoom function of vikingdemo in PUN to test my server, but when i click LeaveRoom button in the game ,a error show up as the title. Here is my code:

//the code in GameManagerVik.cs
       var playerStatus_table=new Dictionary<byte,object>{{(byte)ParameterCode.PlayerProperties,playerStatus}};
	PhotonNetwork.LeaveRoom(playerStatus_table);
     //   PhotonNetwork.LeaveRoom();

//***  this function is in PhotonNetwork.cs,just add a parameter
public static bool LeaveRoom(Dictionary<byte,object> playerStatus)  

//***  this function is in NetworkPeer.cs,just add a parameter and send to OpCustom
public virtual bool OpLeave(Dictionary<byte,object> playerStatus)
	{
		if (this.State != global::PeerState.Joined)
		{
			Debug.LogWarning("Not sending leave operation. State is not 'Joined': " + this.State);
			return false;
		}
		
		return this.OpCustom((byte)OperationCode.Leave, playerStatus, true, 0);
	}

the details of error:

IndexOutOfRangeException: Array index is out of range.
ExitGames.Client.Photon.Protocol.SerializeDictionaryHeader (System.IO.MemoryStream writer, System.Object dict, System.Boolean& setKeyType, System.Boolean& setValueType)
ExitGames.Client.Photon.Protocol.SerializeDictionary (System.IO.MemoryStream dout, IDictionary serObject, Boolean setType)
ExitGames.Client.Photon.Protocol.Serialize (System.IO.MemoryStream dout, System.Object serObject, Boolean setType)
ExitGames.Client.Photon.Protocol.SerializeParameterTable (System.IO.MemoryStream memStream, System.Collections.Generic.Dictionary`2 parameters)
ExitGames.Client.Photon.Protocol.SerializeOperationRequest (System.IO.MemoryStream memStream, Byte operationCode, System.Collections.Generic.Dictionary`2 parameters, Boolean setType)
ExitGames.Client.Photon.EnetPeer.SerializeOperationToMessage (Byte opc, System.Collections.Generic.Dictionary`2 parameters, EgMessageType messageType, Boolean encrypt)
ExitGames.Client.Photon.EnetPeer.EnqueueOperation (System.Collections.Generic.Dictionary`2 parameters, Byte opCode, Boolean sendReliable, Byte channelId, Boolean encrypt, EgMessageType messageType)
ExitGames.Client.Photon.PeerBase.EnqueueOperation (System.Collections.Generic.Dictionary`2 parameters, Byte opCode, Boolean sendReliable, Byte channelId, Boolean encrypted)
ExitGames.Client.Photon.PhotonPeer.OpCustom (Byte customOpCode, System.Collections.Generic.Dictionary`2 customOpParameters, Boolean sendReliable, Byte channelId)
NetworkingPeer.OpLeave (System.Collections.Generic.Dictionary`2 playerStatus) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:988)
PhotonNetwork.LeaveRoom (System.Collections.Generic.Dictionary`2 playerStatus) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:1828)
GameManagerVik.OnGUI () (at Assets/DemoVikings/Scripts/GameManagerVik.cs:67)

At the beginning,i thought the data is overflow,but i just store a string in dictionary still have the same problem. I dont have any idea to fix this. :( Please help me ,thxs. :)

Comments

Tobias
2015-01-19 15:04:04

I can't check this at the moment but will try this week. I think it's a bug that's fixed in PUN 1.50.3 (the main asset in the Asset Store). The Viking Demo is a bit older and maybe has a bug with serialization (which you see). Please try this in the latest PUN package, too.

In general, OpLeave is not really built to include state or anything custom. I don't think your dictionary will arrive at the other clients.

Jesse_0722
2015-01-19 15:42:55

Thank you for your quick reply ,Tobias.You are right,the dictionary can't even arrive my sever. So,as you said "In general, OpLeave is not really built to include state or anything custom." How can I handle leave Operation to save the state of player,use a OpCustom by self or something other?

Tobias
2015-01-20 09:01:27

In Turnbased, we do the following: Anything that's relevant for state has to be a buffered event or a Custom Property. Those things are in the server anyways and when everyone left, we store this as state. When this game gets re-joined (by name, by a player who was in that game before), then we can either find the game in memory or load it.

If you put the state in OpLeave you will loose it when a client crashed or loses connection suddenly without calling OpLeave.

Jesse_0722
2015-01-21 05:01:58

Thank you Tobias,I will do what you said.BTW,I downloaded the last PUN and fixed the problem.

Tobias
2015-01-23 16:46:25

Super. Thanks for letting us know!

Back to top