Arch icon indicating copy to clipboard operation
Arch copied to clipboard

Add Query Unique

Open Difficulty-in-naming opened this issue 2 years ago • 5 comments

We need a method to query singleton components. We will use this method in many scenarios, such as Player Input, making the code look more concise and readable.

    private QueryDescription mDescription = new QueryDescription{Any = new ComponentType[]{typeof(C_PlayerInput)}};
    public void Query()
    {
        world.Query(mDescription, (ref C_PlayerInput input) =>
        {
            if (input.IsClicked)
            {
                //Query another entity....
            }
        });
    }
    public void QueryUnique()
    {
        var playerInput = world.QueryUnique<C_PlayerInput>();
        //Query another entity...
    }

Difficulty-in-naming avatar Jan 31 '23 10:01 Difficulty-in-naming

We need a method to query singleton components. We will use this method in many scenarios, such as Player Input, making the code look more concise and readable.

    private QueryDescription mDescription = new QueryDescription{Any = new ComponentType[]{typeof(C_PlayerInput)}};
    public void Query()
    {
        world.Query(mDescription, (ref C_PlayerInput input) =>
        {
            if (input.IsClicked)
            {
                //Query another entity....
            }
        });
    }
    public void QueryUnique()
    {
        var playerInput = world.QueryUnique<C_PlayerInput>();
        //Query another entity...
    }

Thanks and that's actually a great idea! :) Im gonna think about different ways of realizing this. I think combining this with the current Query-API is not possible since that would require checks every call which would result in a decreased performance. However the world.QueryUnique approach is probably fine, so we could set components directly on the world itself.

Im gonna take a look at how other ecs solve this problem, or if they actually solve it at all ^^

genaray avatar Jan 31 '23 16:01 genaray

We need a method to query singleton components. We will use this method in many scenarios, such as Player Input, making the code look more concise and readable.

    private QueryDescription mDescription = new QueryDescription{Any = new ComponentType[]{typeof(C_PlayerInput)}};
    public void Query()
    {
        world.Query(mDescription, (ref C_PlayerInput input) =>
        {
            if (input.IsClicked)
            {
                //Query another entity....
            }
        });
    }
    public void QueryUnique()
    {
        var playerInput = world.QueryUnique<C_PlayerInput>();
        //Query another entity...
    }

Thanks and that's actually a great idea! :) Im gonna think about different ways of realizing this. I think combining this with the current Query-API is not possible since that would require checks every call which would result in a decreased performance. However the world.QueryUnique approach is probably fine, so we could set components directly on the world itself.

Im gonna take a look at how other ecs solve this problem, or if they actually solve it at all ^^

The idea of attaching components to the world is an unusual concept. To my knowledge, both Unity ECS and Svelto ECS are attached to entities. Do you want to view the world as a root entity?

Difficulty-in-naming avatar Feb 01 '23 09:02 Difficulty-in-naming

Mounting components on the world may produce another way that is independent of the current architecture. Perhaps we need a dictionary with component type as the key and entity as the value. This way, when we call QueryUnique, we can also determine if the component type is unique (singleton) to ensure that user actions do not produce unexpected results.

Difficulty-in-naming avatar Feb 01 '23 09:02 Difficulty-in-naming

Mounting components on the world may produce another way that is independent of the current architecture. Perhaps we need a dictionary with component type as the key and entity as the value. This way, when we call QueryUnique, we can also determine if the component type is unique (singleton) to ensure that user actions do not produce unexpected results.

That could work aswell. This could also be a bit optimized by avoiding the dictionary and replacing it with an array for faster access. Dictionarys normally have a small overhead, which is noticeable for large amounts of acess.

genaray avatar Feb 06 '23 14:02 genaray

I found that the bevy engine use resource for singleton component. the resource stored at spareset.

ctxdegithub avatar Jun 09 '24 00:06 ctxdegithub