Skip to content

feat: indexed fields multi-add api #255

New issue

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

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

Already on GitHub? # to your account

Merged
merged 4 commits into from
Jun 27, 2024
Merged

feat: indexed fields multi-add api #255

merged 4 commits into from
Jun 27, 2024

Conversation

zaucy
Copy link
Member

@zaucy zaucy commented Jun 21, 2024

Indexed fields introduces "association by value" which means that when you add a component to an entity it no longer is associated by its type alone. Instead components are now associated by their indexed field values.

component A { i32 num; }
component AssociatedWithA { A.num hello; }

Give the above example we have a component called AssociatedWithA that has an indexed field called hello. An entity may add AssociatedWithA like normal, however, if the value of hello is different then the component is effectively a different component.

// Adding `AssociatedWithA` multiple times to the same entity is allowed with indexed fields
reg.add_component(entity, AssociatedWithA{.hello = 10});
reg.add_component(entity, AssociatedWithA{.hello = 42});

reg.add_component(entity, AssociatedWithA{.hello = 10}); // error, not allowed. already component with hello=10

This adds a wrinkle with remove_component, update_component, and has_component. We no longer have the ability to specify which AssociatedWithA component to update or remove. This PR introduces slight tweaks to our remove_component and update_component methods requiring you to pass in the indexed fields values.

reg.has_component<AssociatedWithA>(entity, 42); // returns true
reg.has_component<AssociatedWithA>(entity, 10); // returns true

// Remove the component with hello=10
reg.remove_component<AssociatedWithA>(entity, 10);

reg.has_component<AssociatedWithA>(entity, 42); // returns true
reg.has_component<AssociatedWithA>(entity, 10); // returns false

These of course translate to the context methods as well.

context.remove<AssociatedWithA>(10);
// etc.

You can also update an indexed field as well. Very similar to the remove example above.

reg.has_component<AssociatedWithA>(entity, 42); // returns true

reg.update_component(entity, AssociatedWithA{.hello = 55}, 42);

reg.has_component<AssociatedWithA>(entity, 42); // returns false
reg.has_component<AssociatedWithA>(entity, 55); // returns true

It's worth noting that you cannot call has, get, update, or remove without also giving the indexed fields. Doing so is considered user error, but our C++ (and other language) wrappers are designed to prevent these mistakes from happening.

zaucy added a commit to ecsact-dev/ecsact_rt_entt that referenced this pull request Jun 21, 2024
Copy link

cocogitto-bot bot commented Jun 27, 2024

✔️ 93e8315...aaa450a - Conventional commits check succeeded.

@zaucy zaucy enabled auto-merge June 27, 2024 23:05
@zaucy zaucy added this pull request to the merge queue Jun 27, 2024
Merged via the queue into main with commit 64b81d7 Jun 27, 2024
6 checks passed
@zaucy zaucy deleted the feat/indexed-fields-api branch June 27, 2024 23:13
@zaucy zaucy mentioned this pull request Jul 3, 2024
4 tasks
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant