Using SyncListStruct with arrays throws error in Runtime

Updated on January 7, 2016 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
27 on January 6, 2016

I really appreciate if anyone could help here , i stuck from last 9 days sync a array using UNet in unity , so i had asked couple of diff. types of question in unity but none get a answer . I really hope BrackeyForum helps me out .

According to this Documentation In Unity http://docs.unity3d.com/Manual/UNetStateSync.html , we can use arrays while using SyncListStruct , but unknowably  in Runtime when using SyncListStruct containing a array it throws a error.


InvalidProgramException: Invalid IL code in SendMeshToServer/TestMeshData:SerializeItem (UnityEngine.Networking.NetworkWriter,SendMeshToServer/MeshData): IL_001f: call 0x0a000016
UnityEngine.Networking.SyncList`1[SendMeshToServer+MeshData].SendMsg (Operation op, Int32 itemIndex, MeshData item)


Heres my script :


public struct MeshData {

public int point ;

public int[] points ;

};
public class TestMeshData : SyncListStruct<MeshData> {}
TestMeshData _testMeshData = new TestMeshData () ;
int[] tempPoints ;

MeshData _meshData ;
void Start() {

tempPoints = new int[5] ;

}
void Update() {
if (Input.GetKey(KeyCode.Q))

{

_meshData.point += 5 ;

_meshData.points = tempPoints ;
_testMeshData.Add(_meshData) ;

}

}

  • Liked by
Reply
1 on January 6, 2016

Ah, this problem might be simpler than you thought 🙂
Look at line 7 & 8:
 
7: I think you’re from a C background, or just made a mistake 🙂 After a struct, there shouldn’t be a semicolon.
8: You open the class, but immediately close it again.
 
I this fixes it 😀

Devoted
on January 6, 2016

Yes i know , i followed unity documentation . I tried again without semicolon but it is throwing same Invalid Exception error. 
I want to sync mesh in runtime . 
Thx in advance i really appreciate if you know any other way 🙂

Show more replies
  • Liked by
Reply
Cancel
8 on January 6, 2016

Ok, but at least keep those changes.
 
But I grow a little suspicious when I look some stuff up:
This is from you: Invalid IL code in SendMeshToServer/TestMeshData 
This is from the unity docs: State Synchronization is done from the Server to Remote Clients.
And this: Data is not synchronized from remote clients to the server. This is job of Commands.
 
So it seems to me that it’d be better to use Commands, right?

Devoted
on January 6, 2016

Off course i had already done trying with commands , but commands put me into limit upto 1400 bytes , mesh takes more than 1400 bytes , right now my mesh takes 18111 which more than i thought … 

Error when i tried that :

Failed to send big message of 18111 bytes. The maximum is 1400 bytes on this channel.
UnityEngine.Networking.NetworkBehaviour:SendRPCInternal(NetworkWriter, Int32, String)

Guru
on January 6, 2016

This means you’ll have to find a way to split up the commands.

Guru
on January 6, 2016
Devoted
on January 7, 2016

How can i serialize Vector3 array ?

Guru
on January 7, 2016

Well… I did just make a tutorial about it ?

Devoted
on January 7, 2016

I watched it again .. And how can i do same for Binary Formatter? You have use Protobuf .

Guru
on January 7, 2016

That’s because Vector3 alrady has that attribute 🙂
And for Protobuf, you don’t need it.

Devoted
on January 7, 2016

I seen that last part were you used Protobuf for serializing Vector3 , i want to achieve without using Protobuf  . Sry i am really taking this question too long , but i am really fed with trying new things …

Show more replies
  • Liked by
Reply
Cancel
10 on January 7, 2016

You could use the BinaryFormatter on the Mesh class.

Devoted
on January 7, 2016

And how will i serialize a complete Mesh?

Guru
on January 7, 2016
Devoted
on January 7, 2016

I dont Understand , how can i use mesh instead hastable ?

Guru
on January 7, 2016

Ok, MeshData is what your class is named

Devoted
on January 7, 2016

Yes , it contains Vector3[] , Vector2[] ,string and int[] .

Guru
on January 7, 2016

So why don’t you serialize that class?

Devoted
on January 7, 2016

Vector3 and Vector2 🙁 …..  

[Serializable()]
 public class MeshData {
 public int[] triangles ;
public Vector3[] myVector3;
public Vector2[] myVector2 ;
 public MeshData(int[] _triangles , Vector3[] _myVector2 , Vector3[] _myVector3 ) {
 triangles = _triangles ;
myVector3 = _myVector3 ;
myVector2 = _meVector2 ;
 }
 }

Guru
on January 7, 2016

Oh yeah… I forgot. One of the reasons why I don’t use the BF anymore.
You can add it though: http://forum.unity3d.com/threads/vector3-not-serializable.7766/#post-2098308

Devoted
on January 7, 2016

Can you explain whats going here … 🙁 …

Devoted
on January 7, 2016

I had already ended up with doing that … since the count number of vertices * 3 float turns to massive performance lost . 

Show more replies
  • Liked by
Reply
Cancel
4 on January 7, 2016

Hello , i am sry i had some break , i was going through the forum you just given and i found the Debug answer :  

Debug.Log(System.Attribute.IsDefined(typeof(Vector3), typeof(System.SerializableAttribute)));

Ultimately even in 5.3.0f4 unity it shows as true . At the moment i am confused .. if its already serialized struct vector in unity then why it throw that exception error … 🙁 !!!

Guru
on January 7, 2016

Because the BinaryFormatter is stupid 😉
The thing is that you need to serialize your data somehow. I’ve shown a way that works. Now if you don’t want to use that, fine. But then it’s up to you to get the alternative working…
 
So if you have any specific questions, then I’ll be happy to help.

Devoted
on January 7, 2016

Hey wait …it worked 😛 The second last post were he did used SurrogateSelector() .. OMG it was quiet relieving ! Was finding a better way from last 11 days ! You Really Helped me out by putting your precious time ! Thx alot ALOT Alot 🙂 Now i am working with the sending big data into chunks .. 😛 If i got any problem i will ask You .

Devoted
on January 7, 2016

Right now i wished i could send you some points like we can in Unity answers lol 😛 

Guru
on January 7, 2016

Haha, no problem. Glad I could help!

Show more replies
  • Liked by
Reply
Cancel