evaera / matter

A modern ECS library for Roblox.
https://eryn.io/matter/
MIT License
144 stars 34 forks source link

Snapshot should return a "View" Handle #79

Open Ukendio opened 8 months ago

Ukendio commented 8 months ago

queryResult:snapshot() performs all of the iterator task at once and provides you the results of the query at the moment it is called. Hecs has a similar feature with query().views(), which is meant for repeated random access. It provides you a handle to discern whether it contains a specific entity and to access its data easily at an amortized cost via Views.get() and Views.contains() respectively.

I think providing similar methods will make :snapshot a more meaningful arm to QueryResult. We could even naively implement this by changing the list into a dictionary that maps the entity id to its packed data, and returning it, allowing people to just index it. But this wouldn't preserve the same order of traversal of entities as the query.

Considerations

This is a breaking change as it would most likely change the return type. Alternatively we can look to make a separate method called view.

Side note

Currently, you can't follow the :without() arm with :snapshot(). We may want to make a separate change to address this. #85

For tracking Partial implementation: #80

Ukendio commented 8 months ago

@LastTalon Extending the API instead

But I think we should extend the API rather than change it. I think adding a view method would be better. Bevy has a lot of thought into these aspects of usage. Since future changes we may want to make down the road and/or as roblox makes things possible will be affected by the API we make now. What I definitely don't want to do at this moment is break people's current use of snapshot. We're allowed to break things like that in v0, but I don't see a good reason to with this. There's no reason the current snapshot is bad. It just doesn't do these other things we also want.