#Calculating a query against itself
24 messages · Page 1 of 1 (latest)
iterate the query once, collecting needed data into a Vec. Iterate again and mutate based on values in the Vec
but I have to do that every frame, surely that'd hurt performance a lot
collecting into a vec once?
maybe if you have a billion things it'd be a problem
probably not even then
There's also https://docs.rs/bevy_ecs/latest/bevy_ecs/system/struct.Query.html#method.iter_combinations_mut if you have to compare everything to everything
It's O(n^k) though so only use that if you need to and don't have a massive amount of entities
Otherwise collecting to a vec seems fine, if you're worried about performance you can also keep the Vec allocation across frames by sticking it in a Local
actually yea it might be faster to collect into a vec since I'm only getting the data once, I assume ``iter_cominbations_mut()` has to get the data every time its run which'd probably be quite a bit slower
keep the Vec allocation?
If you do query.iter().collect() it will allocate a new vec every frame. You can instead add a Local<Vec<T>> system parameter and use extend and clear instead of collect so that once the vec is allocated you can just reuse that allocation
Local is a bevy concept that means that data is local to a system instead of being stored in a resource
iter.collect is roughly equal to let collection = Vec::new(); collection.extend(iter);. So if you reuse the Vec, clear will remove the items but keep the capacity from before
ohh ok
so if I put it into a local it'll just be stored locally in my system
and bevy will just reuse the already allocated memory
(resource is just a global variable, right?)
I cannot figure out how to use iter.collect with my queries
You can't store the references the query returns. Try .iter().cloned().collect()
it just keeps telling me the value of type cannot be build from std iter??