elm-ecs icon indicating copy to clipboard operation
elm-ecs copied to clipboard

Composable selectors?

Open peacememories opened this issue 5 years ago • 5 comments

I think there's a case to be made for selectors to be composable (in my case, I would want to create a special selector that transforms two components, and would want to be able to use that in other selectors.

Some kind of selectWith : Selector comparable ecs a -> ComponentSpec comparable ecs a although I'm pretty sure it wouldn't work that way^^'

In my case, I want to be able to wrap another component in an Owned type if it has the component Owned attached

peacememories avatar Feb 25 '19 15:02 peacememories

For now the solution for my usecase is

selectOwned : Selector NeedId Ecs a -> Ecs -> List ( OwnedId, a )
selectOwned selector ecs =
    let
        isOwned =
            Ecs.Select.andHas components.owned
    in
    Ecs.selectList (isOwned selector) ecs
        |> List.map
            (\( id, value ) ->
                ( OwnedId id, value )
            )

which is not terribly reusable, but probably works for now

peacememories avatar Feb 25 '19 16:02 peacememories

I did make the mapping functions to make selectors composable but they are currently not exposed. My initial though was to keep selectors a simple as possible. But your use case might be a good reason to expose them anyway. 🤔

harmboschloo avatar Feb 25 '19 18:02 harmboschloo

Thanks for responding so quickly!

I took a look at the mapping functions, and they seem to only select a single entity?

What I would really love to see is some json-decode-pipeline-esque pipelining capability. Though I have to say that I am probably not the core target audience for this library, so you should probably not expose things for my sake alone ;) For games I think the current selectors are fine. I should probably workshop some implementations for our data storage and see where I end up, and if where I end up is close to the api of this library :)

peacememories avatar Feb 26 '19 10:02 peacememories

I took a look at the mapping functions, and they seem to only select a single entity?

A selector (including the mapping functions) can do both a single entity and a list.

What I would really love to see is some json-decode-pipeline-esque pipelining capability

I like that idea. I'll think about it. That would be nice for setting up the specs as well, but I'm not sure if that's feasible. For the selectors it shouldn't really be a problem. The only downside would be that it's a bit less performant I think.

harmboschloo avatar Feb 26 '19 19:02 harmboschloo

I published a small package to do dictionary intersections. It's basically what the selectors do under the hood. Maybe it's interesting if you want a bit more flexibility.

https://package.elm-lang.org/packages/harmboschloo/elm-dict-intersect/latest/

harmboschloo avatar Mar 02 '19 10:03 harmboschloo