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

Windows Need help with lists

T

TheBlindG

Guest
Hello!

I am not sure how lists work fully and hope some1 can help me. I have few actors which can be available for targeting by the enemy. If the player target is a valid target, I was tihking of adding all valid targets to a list, and i.e check which of those targets has the currently lowest HP and attack that target. Is this possible with lists? And if so, any examples would be helpful! :)

Thanks in advance
 

Yal

šŸ§ *penguin noises*
GMC Elder
Definitely. There's also a function called ds_list_shuffle() that shuffles a list... if you want to select a random thing from a list, shuffle it and take the first or last item in it. Perfect for enemy AI and random drops and things like that.
 

jo-thijs

Member
So, you have a list that contains a bunch of instance ids and you want to get the id with the lowest hp from it?
That's certainly possible:
Code:
var result = noone;

for(var i = ds_list_size(list) - 1; i >= 0; i--)
    if result == noone || list[|i].hp < result.hp
        result = list[|i];
At the end of this code, result will contain the instance with the lowest hp.
 
B

bojack29

Guest
A priority would really work best here.
You can add values to a priority to find, in your case, the lowest hp target and the priority would be sorted automatically.
Code:
p = ds_priority_create();
with(obj_targets){
     ds_priority_add(other.p, id, hp);
}
myTarget = ds_priority_find_min(p);
ds_priority_destroy(p);
 
T

TheBlindG

Guest
Thanks a bunch for the good comments! :) You guys helped a lot!
 
T

TheBlindG

Guest
Is this correct use of list?

Code:
var targets = ds_list_create();

for(ii = 1; ii <= 5; ii++){
  if(spot[ii].actor != noone){
  if(spot[ii].actor.valid_target){
  ds_list_add(targets, spot[ii].actor);
  }
  }
}

ds_list_shuffle(targets);

var target = ds_list_find_index(targets, 0)
 
Top