• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

[SOLVED] Help with ds_list to select random path without duplication

I'm trying to work out how to place one enemy on each of the paths I have.

I used a switch to select a path which works fine, but I can't figure out the ds_list to select 4 random, but different, numbers.

Eventually I'll have more paths than instances.

Very new to this, so any guidance would be great!


oSpawner (Create)
Code:
minpath=0;
maxpath=3;
buzzpath=0;
buzzspeed = random_range(.2,.5);

// Pick a random path
ds = ds_list_create();
var index = 0;
repeat(maxpath)
   {
   ds[| index++] = irandom(maxpath);
   }
  
//get path value
minpath =0;
if (!minpath > maxpath)
{
buzzpath = ds[| minpath];

//switch
       switch (buzzpath)
       {
           case 0: with instance_create_layer(x, y, "Buzz", oBuzz)
           {
               buzzspeed = random_range(.2,.5);
               path_start(path0,buzzspeed,path_action_reverse,1);
           }
           break;
  
           case 1: with instance_create_layer(x, y, "Buzz", oBuzz)
           {
               buzzspeed = random_range(.2,.5);
               path_start(path1,buzzspeed,path_action_reverse,1);
           }
           break;
  
           case 2: with instance_create_layer(x, y, "Buzz", oBuzz)
           {
               buzzspeed = random_range(.2,.5);
               path_start(path2,buzzspeed,path_action_reverse,1);
           }
           break;
  
           case 3: with instance_create_layer(x, y, "Buzz", oBuzz)
           {
               buzzspeed = random_range(.2,.5);
               path_start(path3,buzzspeed,path_action_reverse,1);
           }
           break;
       }

minpath +=1
}
 

chamaeleon

Member
I used a switch to select a path which works fine, but I can't figure out the ds_list to select 4 random, but different, numbers.
Possibly the easiest way to solve problems of this nature is to stuff the number 0-3 or 1-4 (depending on the use, looks like you want 0-3 as you are using indices into a ds_list) into a ds_list and call ds_list_shuffle() on it to get a random order, Then, instead of picking a random element in that list to use as index into your data, you go through it linearly, and you end up using all indices, but in random order.
Code:
var ds = ds_list_create();
for (var i = 0; i < maxpath; i++) {
  ds[| i] = i;
}
ds_list_shuffle(ds);
Now you have your 0 .. maxpath-1 indices shuffled, and depending on your exact needs of this shuffled order, you can either loop over it to make use of all the paths in the same step to initialize several things at once while having ensured they are not utilized in the same order every time, or keep some instance or global variable for the purpose of keeping track of which index should be used next, if you want to space out the use of them over time (until you increment the index to become equal to maxpath at which point you're faced with creating new paths and a new shuffled list, or just starting over from 0, depending on what behavior you're after).

Side-note: I don't see any difference between your different case statements except with path to use, so if that is really what you want, you might want to stuff your paths into an array or ds_list instead, and use something like
Code:
path_start(my_paths[| ds[| current_path]], ...);
where current_path is the 0 .. maxpath-1 index you increment as you step through the set of paths and my_paths is a ds_list that you have initialized to contain path1 through path4. And to be safe, make sure to rely a lot on ds_list_size() instead of hard coding values for maxpath, etc.
 
Last edited:
Thanks chamaeleon,
That's a great help. I worked out how to get one of 4 different enemies on each path by modifying it to this:

Code:
minpath=0;
maxpath=4;
buzzpath=0;
enemycount=2;
randomize();

// Pick a random speed
buzzspeed = random_range(.2,2);

// set ds list and shuffle
var ds = ds_list_create();
ds_list_clear (ds);

//loop for number of paths
for (var i = 0; i < maxpath; i++)
   {
   ds[| i] = i;
   }
   ds_list_shuffle(ds);
      
for (var p = minpath; p <enemycount; p++)
   {
   buzzpath = ds_list_find_value(ds, p);

//switch....
I'll take a look at implementing your paths idea too - might be back for some more guidance.
Thanks again for helping a newcomer, it's really appreciated.
 
Top