• 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!

Adding more to 'Max'

I am currently aware that the max function simply compares all the values within the argument and produces the highest value.

What I'm wanting to accomplish is a function that checks the threat level of each enemy in the room and finds the highest threat level. The number of enemies (or entries) into the max function can be anything depending upon how many there are.

For example we have three enemies with threat levels of 93, 75, and 42 in one wave, then another enemy appears with a threat level of 88.

I want to know what I'd need to write to add or subtract the number of values within the max function.
 

Relic

Member
Don’t use the max function (well not the way you are trying to explain it), instead loop through all enemies and compare their values, always keeping the highest you have found.

Code:
var threat_max=0
//cycle through all enemies, keeping the highest my_threat value found among them
with obj_enemy {
threat_max=max(threat_max,my_threat)
}
Do this in a step event of some controller or in response to a new enemy appearing/being destroyed.
 
Haha, yeah I know my explanation would likely present some level of ambiguity.

I will say that's an interesting angle on the issue. What I interpreted from your suggestion is that the player object can store the highest threat level in the room and with each new arrival, their threat level can be compared to the highest threat currently detected by the player object. That way it reduces the number of objects total that need to be calculated.

I certainly appreciate the response! Thank you!
 

samspade

Member
Haha, yeah I know my explanation would likely present some level of ambiguity.

I will say that's an interesting angle on the issue. What I interpreted from your suggestion is that the player object can store the highest threat level in the room and with each new arrival, their threat level can be compared to the highest threat currently detected by the player object. That way it reduces the number of objects total that need to be calculated.

I certainly appreciate the response! Thank you!
Relic is right, using max is not really the way to do what you want. You should read through this tutorial on with statements as it is the easiest way to accomplish what you're looking for:

https://forum.yoyogames.com/index.php?threads/with-block-recipe-cards.25326/

Specifically this one:

Code:
var val, highest, highest_val;
highest = noone;
highest_val = -1;
with (object_or_all) {
  val = property;
  if (highest == noone || val > highest_val) {
   highest = id;
   highest_val = val;
  }
}
You can put it in a scrip like this:

Code:
/// @description Return instance with highest number/highest number
/// @param object

var val, highest, highest_val;
highest = noone;
highest_val = -1;
with (argument0) {
   val = threat;
   if (highest == noone) || (val > highest_val) {
        highest = id;
        highest_val = val;
    }
}

///Then either
//return highest; //return the instance id or noone if none
//return highest_val; //return the highest number or -1 if none
 
D

dannyjenn

Guest
Another option would be to keep track of the enemies and their corresponding threat levels in a ds_priority (the enemy instance's id would be the value; the enemy instance's threat level would be the priority). If you do that, you'd use ds_priority_add() in the enemy's create event, ds_priority_delete_value() in the enemy's destroy event, and ds_priority_find_max() whenever you wanted to get the max.

Yet another option would be to keep track of all the enemy threat levels in a ds_list, and then call ds_list_sort() every time you needed to get the max. (Again, add to the list upon enemy creation and remove from the list upon enemy destruction.)
 
Top