Entitas icon indicating copy to clipboard operation
Entitas copied to clipboard

How to find an Entity by position

Open YsnSen opened this issue 8 years ago • 6 comments

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.

YsnSen avatar Feb 10 '18 16:02 YsnSen

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

Draugor avatar Feb 10 '18 17:02 Draugor

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;
}

YsnSen avatar Feb 10 '18 18:02 YsnSen

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));

ghost avatar Feb 10 '18 21:02 ghost

@T2RKUS Thank you. @Draugor Thank you.

YsnSen avatar Feb 10 '18 21:02 YsnSen

@T2RKUS does PrimaryEntityIndex work well with float and Vector3? I thought it was for int and enum mostly

c0ffeeartc avatar Feb 11 '18 07:02 c0ffeeartc

@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.

FNGgames avatar Feb 12 '18 11:02 FNGgames