How to find an Entity by position
Hello people. First of all I thank you to the developer of this system.
I wonder if there is any method to find and entity by position.
I want to check a Movable or Impassable flag, my target position is known, but I was not able to check the flag by position.
there is a chapter in the Entitas cookbook about indexing entities, it even uses position as an example. That should help you. https://github.com/mzaks/EntitasCookBook/blob/master/chapters/1_ingredients/106_index.md
I already have IdComponent for my project and I'm not so sure how to implement this.
using Entitas;
using Entitas.CodeGeneration.Attributes;
[Game, Input]
public class IdComponent : IComponent
{
[PrimaryEntityIndex]
public int value;
}
This might help if you want more complex lookups. https://github.com/sschmid/Entitas-CSharp/issues/351
But if you just want to lookup position only (e.g. not taking into account anything else) this is enough.
using Entitas;
using Entitas.CodeGeneration.Attributes;
[Game, Input]
public class PositionComponent : IComponent
{
[PrimaryEntityIndex]
public UnityEngine.Vector3 value;
}
Just replace UnityEngine.Vector3 with some other struct/class containing XYZ etc.
so you can use the generated extension method:
var Entity = Contexts.game.GetEntityWithPosition(new Vector3(0,10,0));
@T2RKUS Thank you. @Draugor Thank you.
@T2RKUS does PrimaryEntityIndex work well with float and Vector3? I thought it was for int and enum mostly
@c0ffeeartc usual caveats apply with floats - fp precision errors for equality checks. I personally never use them. I would prefer to round the floats down to ints have a separate GridPosition component with an IntVector2 - then use this for lookups, but depends on what you're trying to achceive.