Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TryGet to entities code generator to get components #1046

Open
pereviader-popcore opened this issue Nov 9, 2022 · 3 comments
Open

Add TryGet to entities code generator to get components #1046

pereviader-popcore opened this issue Nov 9, 2022 · 3 comments

Comments

@pereviader-popcore
Copy link

Is your feature request related to a problem? Please describe.
Internally the entity.potato calls entity.hasPotato , so this would make it a tad faster by avoiding some branching and getting the component directly.

Describe the solution you'd like
From:

if(entity.hasPotato)
{
   var potato = entity.potato;
   ...
}

To:

if(entity.TryGetPotato(out var potato))
{
    ...
}
@pereviader-popcore pereviader-popcore changed the title Add TryGet to entities to get components Add TryGet to entities code generator to get components Nov 9, 2022
@sschmid
Copy link
Owner

sschmid commented Jul 6, 2023

Hi! I'm thinking of this:

namespace MyFeature
{
    [Context(typeof(MainContext))]
    public sealed class PositionComponent : IComponent
    {
        public int X;
        public int Y;
    }
}

Will result in:

namespace MyFeature
{
    public static class MyAppMainPositionEntityExtension
    {
        public static Entity SetPosition(this Entity entity, int x, int y)
        {
            var index = Index.Value;
            var componentPool = entity.GetComponentPool(index);
            var component = componentPool.Count > 0
                ? (PositionComponent)componentPool.Pop()
                : new PositionComponent();
            component.X = x;
            component.Y = y;
            entity.ReplaceComponent(index, component);
            return entity;
        }

        public static Entity UnsetPosition(this Entity entity)
        {
            if (entity.HasComponent(Index.Value))
                entity.RemoveComponent(Index.Value);

            return entity;
        }

        public static PositionComponent? GetPosition(this Entity entity)
        {
            return entity.HasComponent(Index.Value)
                ? (PositionComponent)entity.GetComponent(Index.Value)
                : null;
        }
    }
}

Checking if an entity has a component could look like this:

if (entity.GetPosition() is { X: 10, Y: > 0 })
{
    // ...
}

@sschmid
Copy link
Owner

sschmid commented Jul 6, 2023

The first step would be just changes in the generated code which doesn't require any api changes in Entitas. Next step would be to move Entitas itself to nullables, which will get rid of the HasComponent check in GetPosition()

@sschmid
Copy link
Owner

sschmid commented Jul 7, 2023

New C# syntax might look weird, but it's essentially a TryGet :D

if (entity.GetPosition() is { } position)
{
    // position.X ... 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: New
Development

No branches or pull requests

2 participants