Skip to content

fix: Presence of NetworkVariable<T>[] in a class causes an exception #2584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2225,9 +2225,9 @@ private void GenerateVariableInitialization(TypeDefinition type)
}
field = new FieldReference(fieldDefinition.Name, fieldDefinition.FieldType, genericType);
}
if (field.FieldType.IsSubclassOf(m_NetworkVariableBase_TypeRef))
if (!field.FieldType.IsArray && !field.FieldType.Resolve().IsArray && field.FieldType.IsSubclassOf(m_NetworkVariableBase_TypeRef))
{
// if({variable} != null) {
// if({variable} == null) {
processor.Emit(OpCodes.Ldarg_0);
processor.Emit(OpCodes.Ldfld, field);
processor.Emit(OpCodes.Ldnull);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ public class NetworkVariableSubclass<TSubclassName> : NetworkVariableMiddleclass

}

public class NetworkBehaviourWithNetVarArray : NetworkBehaviour
{
public NetworkVariable<int> Int0 = new NetworkVariable<int>();
public NetworkVariable<int> Int1 = new NetworkVariable<int>();
public NetworkVariable<int> Int2 = new NetworkVariable<int>();
public NetworkVariable<int> Int3 = new NetworkVariable<int>();
public NetworkVariable<int> Int4 = new NetworkVariable<int>();
public NetworkVariable<int>[] AllInts = new NetworkVariable<int>[5];

public int InitializedFieldCount => NetworkVariableFields.Count;


private void Awake()
{
AllInts[0] = Int0;
AllInts[1] = Int1;
AllInts[2] = Int2;
AllInts[3] = Int3;
AllInts[4] = Int4;
}
}

public struct TemplatedValueOnlyReferencedByNetworkVariableSubclass<T> : INetworkSerializeByMemcpy
where T : unmanaged
{
Expand Down Expand Up @@ -1435,6 +1457,40 @@ public void TestUnsupportedUnmanagedTypesWithUserSerializationDoNotThrowExceptio
UserNetworkVariableSerialization<Guid>.DuplicateValue = null;
}
}
[Test]
public void WhenCreatingAnArrayOfNetVars_InitializingVariablesDoesNotThrowAnException()
{
var testObjPrefab = CreateNetworkObjectPrefab($"NetVarArrayPrefab");
var testComp = testObjPrefab.AddComponent<NetworkBehaviourWithNetVarArray>();
testComp.InitializeVariables();

// Verify all variables were initialized
Assert.AreEqual(testComp.InitializedFieldCount, 5);

Assert.NotNull(testComp.Int0.GetBehaviour());
Assert.NotNull(testComp.Int1.GetBehaviour());
Assert.NotNull(testComp.Int2.GetBehaviour());
Assert.NotNull(testComp.Int3.GetBehaviour());
Assert.NotNull(testComp.Int4.GetBehaviour());

Assert.NotNull(testComp.Int0.Name);
Assert.NotNull(testComp.Int1.Name);
Assert.NotNull(testComp.Int2.Name);
Assert.NotNull(testComp.Int3.Name);
Assert.NotNull(testComp.Int4.Name);

Assert.AreNotEqual("", testComp.Int0.Name);
Assert.AreNotEqual("", testComp.Int1.Name);
Assert.AreNotEqual("", testComp.Int2.Name);
Assert.AreNotEqual("", testComp.Int3.Name);
Assert.AreNotEqual("", testComp.Int4.Name);

Assert.AreSame(testComp.AllInts[0], testComp.Int0);
Assert.AreSame(testComp.AllInts[1], testComp.Int1);
Assert.AreSame(testComp.AllInts[2], testComp.Int2);
Assert.AreSame(testComp.AllInts[3], testComp.Int3);
Assert.AreSame(testComp.AllInts[4], testComp.Int4);
}

private void TestValueType<T>(T testValue, T changedValue) where T : unmanaged
{
Expand Down Expand Up @@ -1562,7 +1618,6 @@ private void TestValueTypeNativeList<T>(NativeList<T> testValue, NativeList<T> c
clientVariable.Dispose();
}
#endif

[Test]
public void WhenSerializingAndDeserializingValueTypeNetworkVariables_ValuesAreSerializedCorrectly(

Expand Down