Remove initializer data once the block is placed

This commit is contained in:
Norbi Peti 2020-07-19 01:42:32 +02:00 committed by NGnius (Graham)
parent 5264d98ce7
commit b53dff5d12

View file

@ -82,6 +82,7 @@ namespace GamecraftModdingAPI
position, uscale, scale, player, rotation, out var initializer); position, uscale, scale, player, rotation, out var initializer);
var bl = New<T>(egid.entityID, egid.groupID); var bl = New<T>(egid.entityID, egid.groupID);
bl.InitData.Group = BlockEngine.InitGroup(initializer); bl.InitData.Group = BlockEngine.InitGroup(initializer);
Placed += bl.OnPlacedInit;
return bl; return bl;
} }
@ -352,6 +353,13 @@ namespace GamecraftModdingAPI
(GridConnectionsEntityStruct st) => new SimBody(st.machineRigidBodyId)); (GridConnectionsEntityStruct st) => new SimBody(st.machineRigidBodyId));
} }
private void OnPlacedInit(object sender, BlockPlacedRemovedEventArgs e)
{ //Member method instead of lambda to avoid constantly creating delegates
if (e.ID != Id) return;
Placed -= OnPlacedInit; //And we can reference it
InitData = default; //Remove initializer as it's no longer valid - if the block gets removed it shouldn't be used again
}
public override string ToString() public override string ToString()
{ {
return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Type)}: {Type}, {nameof(Color)}: {Color}, {nameof(Exists)}: {Exists}"; return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Type)}: {Type}, {nameof(Color)}: {Color}, {nameof(Exists)}: {Exists}";