• 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] Modifying random child object

M

mrbloomfield

Guest
Hello,

I have a parent_object. I would like to create a button which - after clicking on it - randomly selects one of the "children" and change it into different instance. How can I code that?
 

samspade

Member
It depends on a couple things. If it can be any child of the object then it wouldn't be the most efficient but the easiest would be:

Code:
///select_random_child(object)

var _list = ds_list_create();
with (argument0) {
    ds_list_add(_list, id);
}
ds_list_shuffle(_list);
var _random_child_id = ds_list_find_value(_list, 0);
ds_list_destroy(_list);
return _random_child_id;
 

TailBit

Member
If I remember correct:
Code:
target = instance_find(obj, irandom(instance_number(obj)));

with(target){
    instance_create(x,y,different_object);
    instance_destroy();
}
EDIT: I forgot the last part to switch object
 
Top