Skip to content

Commit

Permalink
simplify parent change because Childs is now synchronized
Browse files Browse the repository at this point in the history
  • Loading branch information
RevenantX committed Nov 26, 2024
1 parent 2a64b0d commit 4df5a39
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions LiteEntitySystem/EntityLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public T AddPredictedEntity<T>(Action<T> initMethod = null) where T : EntityLogi
entity._predictedId.Value = _localPredictedIdCounter.Value++;
entity._parentId.Value = new EntitySharedReference(this);
entity.InternalOwnerId.Value = InternalOwnerId.Value;
entity.OnParentChange(EntitySharedReference.Empty);
Childs.Add(this);
entity.OnOwnerChange(EntityManager.InternalPlayerId);
return entity;
}
Expand Down Expand Up @@ -196,7 +196,7 @@ public void AddPredictedEntity<T>(ref SyncVar<EntitySharedReference> targetRefer
entity = ClientManager.AddLocalEntity(initMethod);
entity._parentId.Value = new EntitySharedReference(this);
entity.InternalOwnerId.Value = InternalOwnerId.Value;
entity.OnParentChange(EntitySharedReference.Empty);
Childs.Add(this);
entity.OnOwnerChange(EntityManager.InternalPlayerId);
targetReference.Value = entity;
}
Expand All @@ -214,9 +214,9 @@ public void SetParent(EntityLogic parentEntity)
if (id == _parentId.Value)
return;

EntitySharedReference oldId = _parentId;
EntityManager.GetEntityById<EntityLogic>(_parentId)?.Childs.Remove(this);
EntityManager.GetEntityById<EntityLogic>(id)?.Childs.Add(this);
_parentId.Value = id;
OnParentChange(oldId);

var newParent = EntityManager.GetEntityById<EntityLogic>(_parentId)?.InternalOwnerId ?? EntityManager.ServerPlayerId;
if (InternalOwnerId.Value != newParent)
Expand Down Expand Up @@ -314,7 +314,6 @@ internal static void SetOwner(EntityLogic entity, byte ownerId)
protected override void RegisterRPC(ref RPCRegistrator r)
{
base.RegisterRPC(ref r);
r.BindOnChange(this, ref _parentId, OnParentChange);
r.BindOnChange(this, ref InternalOwnerId, OnOwnerChange);
}

Expand Down

0 comments on commit 4df5a39

Please sign in to comment.