Skip to content

Help: How to set property by string? #1248

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

Open
jptang2 opened this issue Apr 24, 2025 · 3 comments
Open

Help: How to set property by string? #1248

jptang2 opened this issue Apr 24, 2025 · 3 comments
Assignees
Labels
question open question

Comments

@jptang2
Copy link

jptang2 commented Apr 24, 2025

As an EnTT beginner, I want to dynamically set component properties using strings. For example, how can I set registry.get<velocity>(entity).dx = x; when I only have:

The entity
Component name as "velocity"
Property name as "dx"
The target value of dx

Please help, thanks.

@skypjack skypjack self-assigned this Apr 24, 2025
@skypjack skypjack added the question open question label Apr 24, 2025
@skypjack
Copy link
Owner

You should use a reflection system for that. EnTT offers meta. The web offers many others. 🙂

@jptang2
Copy link
Author

jptang2 commented Apr 25, 2025

@skypjack Thank you for your quick response, I would like to try entt meta, but I noticed the wiki doesn’t clearly explain how to access components for a specific entity. Currently, I can set component properties using code like this:

registry.emplace<velocity>(entity, 0);
entt::meta_factory<velocity>{}.type("velocity"_hs).data<&velocity::dx>("dx"_hs);

auto type = entt::resolve("velocity"_hs);           // replaced by string
auto prop = type.data("dx"_hs);                        // replaced by string
auto velocityComponent = registry.get<velocity>(entity); // how to replace by "velocity"
prop.set(velocityComponent, propertyValue);                  // set dx to a given value

How to get velocityComponent from a given entity by string? I’d greatly appreciate your help.

@skypjack
Copy link
Owner

Ironically, I'm working on something similar in EnTT. It can probably help you.
See here for details. In particular, look at these lines:

const auto range = registry.storage();
internal::present_entity(ctx, entt, range.begin(), range.end());

Where a range of storage is provided to the present_entity function. If you check its body, it uses meta to get the job done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question open question
Projects
None yet
Development

No branches or pull requests

2 participants