#Calculating a query against itself

24 messages · Page 1 of 1 (latest)

static marten
#

I need to do a thing where I iterate through a mutable query and compare it against data from itself, but rust won't let me have a mutable query AND use data from it too, what can I do about that? Do I have to clone the query to compare it? How to clone a query?

torn schooner
#

iterate the query once, collecting needed data into a Vec. Iterate again and mutate based on values in the Vec

static marten
#

but I have to do that every frame, surely that'd hurt performance a lot

junior garden
#

collecting into a vec once?

#

maybe if you have a billion things it'd be a problem

#

probably not even then

unkempt gate
#

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

static marten
#

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

unkempt gate
# static marten 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

static marten
#

ooo

#

(sorry I'm a bit new with rust, how does that work?)

unkempt gate
#

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

static marten
#

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?)

static marten
#

I cannot figure out how to use iter.collect with my queries

steel star
static marten