[Solved]creating a list of colors & removing one when chosen

K

Kamon145

Guest
Good evening, I am trying to figure out how to go about creating a choose statement that will choose a color, than remove it from said list so that it is not chosen twice, example:
Code:
enemy_1_color = choose(c_blue,c_green,c_red)
//if red is chosen, remove it from list somehow
enemy_2_color = choose(c_blue,c_green)
//if blue is chosen, remove that leaving only green
enemy_3_color = c_green
So thats what Im wanting to do basically, just not sure how to go about doing it, there is 9 colors total so i don't want to do anything that will cause a long chain of "if" checks
 

Slyddar

Member
Populate a ds_list with the colours, choose a position in the list with an irandom value up to the ds_list size-1, assign that position to a variable, delete that position from the list. Repeat until size-1 equal to 0.
 

Perseus

Not Medusa
Forum Staff
Moderator
You could use lists.
Code:
var c_list = ds_list_create();
ds_list_add(c_list, c_red, c_green, c_blue, ...);
ds_list_shuffle(c_list);
for (var n = 0; n < 9; n++) {
    variable_instance_set(id, "enemy_" + string(n+1) + "_color", c_list[| n]);
}
ds_list_destroy(c_list);
 
K

Kamon145

Guest
Populate a ds_list with the colours, choose a position in the list with an irandom value up to the ds_list size-1, assign that position to a variable, delete that position from the list. Repeat until size-1 equal to 0.
I haven't used ds lists before so I'm having a bit of trouble putting things together, so far i have
Code:
list = ds_list_create();
ds_list_add(list, c_red);
ds_list_add(list, c_blue);
ds_list_add(list, c_green);
color_1 = irandom(ds_list_size(list)-1)
but than I'm not sure how to remove that one from the list, also, at the end of the game or when it restarts i have to use ds_list_destroy to avoid a memory leak, correct? Just checking because again I haven't used these before but know how helpful they can be so I am eager to give it a try
 

Slyddar

Member
Perseus has given the answer, but well done for giving it a try yourself, which is always better when learning new things.
Lists are nothing to fear, they are actually really useful. As you say, just ensure you destroy them after using them, either in the same step if a local variable, or in a cleanup event if you need them as instance/global variables.
 
Top