-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Comments
pereviader-popcore
changed the title
Add TryGet to entities to get components
Add TryGet to entities code generator to get components
Nov 9, 2022
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 })
{
// ...
} |
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 |
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
Is your feature request related to a problem? Please describe.
Internally the
entity.potato
callsentity.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:
To:
The text was updated successfully, but these errors were encountered: