Add EnsureComponent(ref Entity<T?>) (#4516)

This commit is contained in:
Leon Friedrich
2023-10-24 17:19:38 +11:00
committed by GitHub
parent ad134d9e4e
commit ad0cb05dd6
3 changed files with 42 additions and 0 deletions

View File

@@ -699,6 +699,26 @@ namespace Robust.Shared.GameObjects
return AddComponent<T>(uid);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool EnsureComponent<T>(ref Entity<T?> entity) where T : IComponent, new()
{
if (entity.Comp != null)
{
// Check for deferred component removal.
if (entity.Comp.LifeStage <= ComponentLifeStage.Running)
{
DebugTools.AssertOwner(entity, entity.Comp);
return true;
}
RemoveComponent(entity, entity.Comp);
}
entity.Comp = AddComponent<T>(entity);
return false;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool EnsureComponent<T>(EntityUid entity, out T component) where T : IComponent, new()