I have a bunch of parameters (int, bool, string, float, vectors, etc.) and also arrays and lists that I want to pass through a struct.
I was checking the documentation here and it seems pretty straightforward when it comes to single parameters:
As far as I understand this, if I have, let’s say, 30 public parameters in the struct, then on the NetworkSerialize, for each of them, I have to make a reference with the serializer. For instance:
serializer.SerializeValue(ref param1);
serializer.SerializeValue(ref param2);
serializer.SerializeValue(ref param3);
serializer.SerializeValue(ref param4);
serializer.SerializeValue(ref param5);
serializer.SerializeValue(ref param6);
serializer.SerializeValue(ref param7);
// etc.
serializer.SerializeValue(ref param30);
Now, when it comes to using arrays on a struct, they have this example:
But I don’t understand it perfectly. If the array contains both single parameters and also arrays, does the NetworkSerialize need to look like this? (containing both):
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
serializer.SerializeValue(ref param1);
serializer.SerializeValue(ref param2);
serializer.SerializeValue(ref param3);
serializer.SerializeValue(ref param4);
serializer.SerializeValue(ref param5);
serializer.SerializeValue(ref param6);
serializer.SerializeValue(ref param7);
// etc.
serializer.SerializeValue(ref param30);
// +
// Length
int length = 0;
if (!serializer.IsReader)
{
length = Array.Length;
}
serializer.SerializeValue(ref length);
// Array
if (serializer.IsReader)
{
Array = new int[length];
}
for (int n = 0; n < length; ++n)
{
serializer.SerializeValue(ref Array[n]);
}
}
Also, I don’t completely understand the role of the public int[ ] Array; here. It’s supposed to be your array. What if you have 10+ arrays of different types, do you have to replicate the
// Length
int length = 0;
if (!serializer.IsReader)
{
length = Array.Length;
}
serializer.SerializeValue(ref length);
// Array
if (serializer.IsReader)
{
Array = new int[length];
}
for (int n = 0; n < length; ++n)
{
serializer.SerializeValue(ref Array[n]);
}
for each array? It seems counterintuitive to make it like that, so I’m almost sure I’m misunderstanding this.
Lastly, I’m not completely sure how to update the information inside the struct. On the docs, they show
MyServerRpc(
new MyComplexStruct
{
Position = transform.position,
Rotation = transform.rotation
}); // Client -> Server
but I imagine it can get real complicated having to update 30+ parameters and arrays like this
MyServerRpc(
new MyComplexStruct
{
param1 = value1,
param2 = value2,
param3 = value3,
param4 = value4,
param5 = value5,
param6 = value6,
// etc.
param30 = value30,
// then arrays
for (int i = 0; i < myArray1.Length; i++)
{
myArray1[i] = valueArray1[i];
}
for (int i = 0; i < myArray2.Length; i++)
{
myArray2[i] = valueArray2[i];
}
// etc.
}); // Client -> Server
Thank you

