• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED 2.2.5.378 Need help to check all instance's value for a certain variable

Gnome

Member
First time user of GMS 2 (started yesterday). I have no clue how instances and their functions work so I would not be surprised to use the wrong functions or even to use the right function in a wrong way.

I'm trying to have an instance check if all the instances in the room (including itself) have an "ini" score lower than the current "global.turn". If there is, then the "global.turn" should become the highest "ini" lower than the "global.turn". If there is no "ini" lower than the "global.turn" then it becomes the highest "ini" in the room.

The code I tried.

Code:
nextTurn=-1000;
maxTurn=0;

for(var i =0; i< instance_count;i++){
if(instance_find(all,i).ini < global.turn){
  nextTurn=max(nextTurn,instance_find(all,i).ini);
}else{
  maxTurn=max(maxTurn,instance_find(all,i).ini);
}

}
if(nextTurn = -1000){
turn=maxTurn;
}else{
turn=nextTurn;
}
Edit: Found the problem, "turn" doesn't exist, it was supposed to be "global.turn".
 
Last edited:

Binsk

Member
Welcome to the community!

I think there are two things you may find useful. First off, GameMaker has a unique loop called with that effectively will loop through a specified instance(s) and execute code on their behalf. Secondly, something like a ds_list combined with 'sort' would come in handy for sorting ini values.

So my idea would be this: Loop through all your objects, storing their ini values in a list. After this, sort the list. Loop through each value from the list, checking it against your global.turn, and, if you find one lower, set it to that and quit. If you don't find one lower then set it to the highest one and quit.

You'll have to adjust this to your game but something like this:
Code:
var _list = ds_list_create(); // Temporary variable. Scope is accessible within the with statement
with (all){ // Loop through every object in the room:
    ds_list_add(_list, ini); // Add our value to the list
}
ds_list_sort(_list, false); // Sort in descending order

// Loop through each value in the list:
for (var i = 0; i < ds_list_size(_list); ++i){
    if (_list[| i] < global.turn){ // The first one found is the highest one lower
        global.turn = _list[| i];
        break;
    }
}
// Make sure we check in case we didn't find a lower one and set to highest:
if (i >= ds_list_size(_list))
    global.turn = _list[| 0];

// Free the list from memory to prevent memory leak:
ds_list_destroy(_list);
That accomplishes your stated goal. One thing to note, however. Make sure that every object has the variable ini otherwise you will get an error when it tries accessing the variable in an instance that doesn't. If you only need to check instances of a certain object and not every instance then you can specify the object name instead of all with the with loop.
 
Top