Getting an entity's information, when the information required is unknown before some logic #21988
Unanswered
christopherverch
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Related to #20176, I'm not sure what's a good way to make a function that will require queries that aren't known until the function is running. For example, say I'm mining something, and I need to do different things based on what I'm mining. Say there are 10 different entity types I could be mining.
Some options and why I don't think they work very well:
The ECS solution (I believe) would be to have a Mineable component, and have a component on each entity that says its type/data, like a Machine component that also has an Inventory component.
The problem with this is, unless I want to split my mining code into 10 different functions (when most of the logic will be similar), then the function parameters would have to look like Query<&mut Machine>, Query<&mut Resource>, Query<&mut Chest>, Query<&mut Objective> ... because the data from any one of those MIGHT be used and/or modified in the function, but I don't know until I check if the entity has one of those components.
Using events could work, but it has the same problem of most logic is shared so either every event handler has to call the same helper function or just repeat code. It also doesn't work well for things that should happen every frame.
A not-so-great solution of just stuffing the entity data into the Mineable component as an enum, so I could just do
match mineable_type { MineableType::Resource(resource_data) => {resource_data.quantity -= 1} ... }but obviously this is really not a great solution.
Pass &mut world and do the query we need from there. Bad for obvious reasons.
I've also run into this with input handlers, where I don't know what data I might want to modify every frame so I end up having to pass everything in, and if it's a value I need to modify every frame it doesn't seem like a good use case for events. I appreciate any help, I've been having problems with this since I first started using bevy and haven't found a good solution.
Beta Was this translation helpful? Give feedback.
All reactions