Fixed issue with Target SyncedVar when running as host

This commit is contained in:
Albin Corén 2018-04-18 20:03:33 +02:00
parent e8d58d292b
commit f45ac572ea

View File

@ -453,88 +453,91 @@ namespace MLAPI.MonoBehaviours.Core
} }
else else
{ {
//It's sync time. This is the target receivers packet. if (!(isHost && new NetId(ownerClientId).IsHost()))
using (BitWriter writer = new BitWriter())
{ {
//Write all indexes //It's sync time. This is the target receivers packet.
writer.WriteByte(totalDirtyCount); using (BitWriter writer = new BitWriter())
writer.WriteUInt(networkId); //NetId
writer.WriteUShort(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
for (byte i = 0; i < syncedVarFields.Count; i++)
{ {
//Writes all the indexes of the dirty syncvars. //Write all indexes
if (syncedVarFields[i].Dirty == true) writer.WriteByte(totalDirtyCount);
writer.WriteUInt(networkId); //NetId
writer.WriteUShort(networkedObject.GetOrderIndex(this)); //Behaviour OrderIndex
for (byte i = 0; i < syncedVarFields.Count; i++)
{ {
writer.WriteByte(i); //FieldIndex //Writes all the indexes of the dirty syncvars.
switch (syncedVarFields[i].FieldType) if (syncedVarFields[i].Dirty == true)
{ {
case FieldType.Bool: writer.WriteByte(i); //FieldIndex
writer.WriteBool((bool)syncedVarFields[i].FieldInfo.GetValue(this)); switch (syncedVarFields[i].FieldType)
break; {
case FieldType.Byte: case FieldType.Bool:
writer.WriteByte((byte)syncedVarFields[i].FieldInfo.GetValue(this)); writer.WriteBool((bool)syncedVarFields[i].FieldInfo.GetValue(this));
break; break;
case FieldType.Double: case FieldType.Byte:
writer.WriteDouble((double)syncedVarFields[i].FieldInfo.GetValue(this)); writer.WriteByte((byte)syncedVarFields[i].FieldInfo.GetValue(this));
break; break;
case FieldType.Single: case FieldType.Double:
writer.WriteFloat((float)syncedVarFields[i].FieldInfo.GetValue(this)); writer.WriteDouble((double)syncedVarFields[i].FieldInfo.GetValue(this));
break; break;
case FieldType.Int: case FieldType.Single:
writer.WriteInt((int)syncedVarFields[i].FieldInfo.GetValue(this)); writer.WriteFloat((float)syncedVarFields[i].FieldInfo.GetValue(this));
break; break;
case FieldType.Long: case FieldType.Int:
writer.WriteLong((long)syncedVarFields[i].FieldInfo.GetValue(this)); writer.WriteInt((int)syncedVarFields[i].FieldInfo.GetValue(this));
break; break;
case FieldType.SByte: case FieldType.Long:
writer.WriteSByte((sbyte)syncedVarFields[i].FieldInfo.GetValue(this)); writer.WriteLong((long)syncedVarFields[i].FieldInfo.GetValue(this));
break; break;
case FieldType.Short: case FieldType.SByte:
writer.WriteShort((short)syncedVarFields[i].FieldInfo.GetValue(this)); writer.WriteSByte((sbyte)syncedVarFields[i].FieldInfo.GetValue(this));
break; break;
case FieldType.UInt: case FieldType.Short:
writer.WriteUInt((uint)syncedVarFields[i].FieldInfo.GetValue(this)); writer.WriteShort((short)syncedVarFields[i].FieldInfo.GetValue(this));
break; break;
case FieldType.ULong: case FieldType.UInt:
writer.WriteULong((ulong)syncedVarFields[i].FieldInfo.GetValue(this)); writer.WriteUInt((uint)syncedVarFields[i].FieldInfo.GetValue(this));
break; break;
case FieldType.UShort: case FieldType.ULong:
writer.WriteUShort((ushort)syncedVarFields[i].FieldInfo.GetValue(this)); writer.WriteULong((ulong)syncedVarFields[i].FieldInfo.GetValue(this));
break; break;
case FieldType.String: case FieldType.UShort:
writer.WriteString((string)syncedVarFields[i].FieldInfo.GetValue(this)); writer.WriteUShort((ushort)syncedVarFields[i].FieldInfo.GetValue(this));
break; break;
case FieldType.Vector3: case FieldType.String:
Vector3 vector3 = (Vector3)syncedVarFields[i].FieldInfo.GetValue(this); writer.WriteString((string)syncedVarFields[i].FieldInfo.GetValue(this));
writer.WriteFloat(vector3.x); break;
writer.WriteFloat(vector3.y); case FieldType.Vector3:
writer.WriteFloat(vector3.z); Vector3 vector3 = (Vector3)syncedVarFields[i].FieldInfo.GetValue(this);
break; writer.WriteFloat(vector3.x);
case FieldType.Vector2: writer.WriteFloat(vector3.y);
Vector2 vector2 = (Vector2)syncedVarFields[i].FieldInfo.GetValue(this); writer.WriteFloat(vector3.z);
writer.WriteFloat(vector2.x); break;
writer.WriteFloat(vector2.y); case FieldType.Vector2:
break; Vector2 vector2 = (Vector2)syncedVarFields[i].FieldInfo.GetValue(this);
case FieldType.Quaternion: writer.WriteFloat(vector2.x);
Vector3 euler = ((Quaternion)syncedVarFields[i].FieldInfo.GetValue(this)).eulerAngles; writer.WriteFloat(vector2.y);
writer.WriteFloat(euler.x); break;
writer.WriteFloat(euler.y); case FieldType.Quaternion:
writer.WriteFloat(euler.z); Vector3 euler = ((Quaternion)syncedVarFields[i].FieldInfo.GetValue(this)).eulerAngles;
break; writer.WriteFloat(euler.x);
case FieldType.ByteArray: writer.WriteFloat(euler.y);
writer.WriteByteArray((byte[])syncedVarFields[i].FieldInfo.GetValue(this)); writer.WriteFloat(euler.z);
break; break;
} case FieldType.ByteArray:
writer.WriteByteArray((byte[])syncedVarFields[i].FieldInfo.GetValue(this));
break;
}
if (nonTargetDirtyCount == 0) if (nonTargetDirtyCount == 0)
{ {
//Only targeted SyncedVars were changed. Thus we need to set them as non dirty here since it wont be done by the next loop. //Only targeted SyncedVars were changed. Thus we need to set them as non dirty here since it wont be done by the next loop.
syncedVarFields[i].FieldValue = syncedVarFields[i].FieldInfo.GetValue(this); syncedVarFields[i].FieldValue = syncedVarFields[i].FieldInfo.GetValue(this);
syncedVarFields[i].Dirty = false; syncedVarFields[i].Dirty = false;
}
} }
} }
InternalMessageHandler.Send(ownerClientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize()); //Send only to target
} }
InternalMessageHandler.Send(ownerClientId, "MLAPI_SYNC_VAR_UPDATE", "MLAPI_INTERNAL", writer.Finalize()); //Send only to target
} }
if (nonTargetDirtyCount == 0) if (nonTargetDirtyCount == 0)