Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update EntityManager to use a read-only wrapper over entity groups #10

Merged
merged 2 commits into from
Mar 2, 2022

Conversation

hak33m16
Copy link
Owner

@hak33m16 hak33m16 commented Mar 1, 2022

TODO: Look into if there's an interface to implement toArray() and forEach() just so we can be consistent with other iterable objects

Makes it a lot nicer to work with the EntityManager for queries now

const group: EntityGroup = entityManager.queryComponents(Position)
for (const [id, entity] of group) {
  // dostuffs
}

becomes:

const group: EntityGroup = entityManager.queryComponents(Position)
for (const entity of group) {
  // dostuffs
}

Also, because the query doesn't directly pass back the map anymore, this group is basically a read-only copy. The only remaining gotcha now is that if users store this group, they could be surprised that it changes over time as entities are modified since the .get() method is really accessing a group reference underneath

To combat that issue, users can create their own copies of the contents of the query/group:

const myCopy = Array.from(entityManager.queryComponents(Position))

(since Array.from() will work on anything that implements Iterable)

Also adds in a .toArray() and .forEach() function

@hak33m16 hak33m16 added the enhancement New feature or request label Mar 1, 2022
@hak33m16 hak33m16 self-assigned this Mar 1, 2022
@hak33m16 hak33m16 linked an issue Mar 2, 2022 that may be closed by this pull request
@hak33m16 hak33m16 merged commit 0a17002 into master Mar 2, 2022
@hak33m16 hak33m16 deleted the feature/change-component-query-type branch March 2, 2022 04:12
@hak33m16 hak33m16 added reviewable and removed enhancement New feature or request labels Mar 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change component query return type/handle
1 participant