using JetBrains.Annotations; using Robust.Shared.Interfaces.GameObjects; namespace Robust.Shared.GameObjects { [PublicAPI] public static class ComponentExt { /// /// Convenience wrapper to implement "create component if it does not already exist". /// Always gives you back a component, and creates it if it does not exist yet. /// /// The entity to fetch or create the component on. /// The existing component, or the new component if none existed yet. /// The type of the component to fetch or create. /// True if the component already existed, false if it had to be created. public static bool EnsureComponent(this IEntity entity, out T component) where T : Component, new() { if (entity.TryGetComponent(out component)) { return true; } component = entity.AddComponent(); return false; } /// /// Convenience wrapper to implement "create component if it does not already exist". /// Always gives you back a component, and creates it if it does not exist yet. /// /// The entity to fetch or create the component on. /// The type of the component to fetch or create. /// The existing component, or the new component if none existed yet. public static T EnsureComponent(this IEntity entity) where T : Component, new() { if (entity.TryGetComponent(out T component)) { return component; } return entity.AddComponent(); } } }