Skip to content

Commit

Permalink
Fixed multiple issues with creation/destruction of entities. Clients …
Browse files Browse the repository at this point in the history
…could generate duplicate entity IDs. Now only the server can assign IDs.
  • Loading branch information
profexorgeek committed Jan 1, 2019
1 parent 66e81e7 commit c8215f7
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public void RequestCreateEntity(object initialState)
}
else
{
SendDataMessage(GetUniqueEntityId(), NetworkId, initialState, NetworkMessageType.Create);
SendDataMessage(-1, NetworkId, initialState, NetworkMessageType.Create);
}
}

Expand All @@ -262,7 +262,7 @@ public void RequestDestroyEntity(INetworkEntity entity)
{
if(Role == NetworkRole.Server)
{
DestroyEntity(entity.EntityId, entity.OwnerId);
DestroyEntity(entity.EntityId);
}
else
{
Expand Down Expand Up @@ -380,8 +380,7 @@ private void ProcessDataMessage(NetIncomingMessage message)
CreateEntity(netMsg.OwnerId, netMsg.EntityId, netMsg.Payload, netMsg.MessageSentTime);
break;
case NetworkMessageType.Destroy :
// TODO: do something with time here?
DestroyEntity(netMsg.EntityId, netMsg.OwnerId);
DestroyEntity(netMsg.EntityId);
break;
case NetworkMessageType.Update :
case NetworkMessageType.Reckoning :
Expand All @@ -402,16 +401,33 @@ private void ProcessDataMessage(NetIncomingMessage message)
/// <param name="time">The time the message was sent, used for projecting the state to current time.</param>
private void CreateEntity(long ownerId, long entityId, object payload, double time)
{
// check if already exists.
INetworkEntity targetEntity = mEntities.Where(e => e.EntityId == entityId && e.OwnerId == ownerId).SingleOrDefault();
// this is a brand new entity, get a new ID
if(entityId == -1)
{
if(Role == NetworkRole.Server)
{
entityId = GetUniqueEntityId();
}
else
{
var msg = "Something went wrong, client received bad entity ID.";
mLog.Error(msg);
throw new RedGrinException(msg);
}
}

// check entity with ID already exists
INetworkEntity targetEntity = mEntities.Where(e => e.EntityId == entityId).SingleOrDefault();

if(targetEntity == null)
{
targetEntity = GameArena.RequestCreateEntity(ownerId, entityId, payload);
}
else
{
mLog.Warning("Attempted to create entity for ID that already exists: " + entityId);
var msg = "Attempted to create entity for ID that already exists: " + entityId;
mLog.Error(msg);
throw new RedGrinException(msg);
}

targetEntity.OwnerId = ownerId;
Expand All @@ -426,15 +442,14 @@ private void CreateEntity(long ownerId, long entityId, object payload, double ti
/// Called when a Destroy message has arrived, destroys an entity.
/// </summary>
/// <param name="entityId">The unique ID of the entity to be destroyed</param>
private void DestroyEntity(long entityId, long ownerId)
private void DestroyEntity(long entityId)
{
BroadcastIfServer(entityId, ownerId, null, NetworkMessageType.Destroy);

INetworkEntity targetEntity = mEntities.Where(e => e.EntityId == entityId && e.OwnerId == ownerId).SingleOrDefault();
if(targetEntity != null)
INetworkEntity target = mEntities.Where(e => e.EntityId == entityId).SingleOrDefault();
if (target != null)
{
mEntities.Remove(targetEntity);
GameArena.RequestDestroyEntity(targetEntity);
BroadcastIfServer(target.EntityId, target.OwnerId, null, NetworkMessageType.Destroy);
mEntities.Remove(target);
GameArena.RequestDestroyEntity(target);
}
else
{
Expand Down Expand Up @@ -564,7 +579,7 @@ private void DestroyAllOwnedById(long ownerId)
SendDataMessage(entity, NetworkMessageType.Destroy);
if(Role == NetworkRole.Server)
{
DestroyEntity(entity.EntityId, ownerId);
DestroyEntity(entity.EntityId);
}
}
}
Expand Down

0 comments on commit c8215f7

Please sign in to comment.