Choosing a random y coordinate from an array [SOLVED]

Good afternoon,

I wanted to create three different instances (obj_chicken,obj_chicken_1,obj_chicken_2) at different predefined y coordinates stored in a list. The x is always the same (384), but I want the y to differ and when one y value is used then I don't want it to be used again.

From previous help received I have made a list which stores the three y coordinates and pulls them out randomly (the comments are for me and if they are wrong, please let me know):
Code:
coord_list = ds_list_create();
ds_list_add(coord_list,[32]);//stores the y coord
ds_list_add(coord_list,[192]);//stores the y coord
ds_list_add(coord_list,[352]);//stores the y coord
var rand = irandom(ds_list_size(coord_list)-1);//randomly assigns a Y coord a number
var pos = coord_list[| rand];//stores the y coord number in pos
var yy = pos[0];stores the y coord number in pos in var yy

instance_create_layer(384,yy,"instances",obj_chicken)
But how would I make the next instance in a different Y position? If I just created another instance with the same instance create code right after the first, would it automatically use a different Y value? That is to ask, if the first instance is created at y(32), is there a chance the next instance could also be created at y(32)?

Or do I have to destroy the list and then create it again without storing y(32)?

Any help would be appreciated. Thanks.
 
Last edited:

chamaeleon

Member
Good afternoon,

I wanted to create three instances of obj_chicken at different predefined Y coordinates stored in a list. The x is always the same (384), but I want the y to differ and when one y value is used then I don't want it to be used again.

From previous help received I have made a list which stores the three y coordinates and pulls them out randomly (the comments are for me and if they are wrong, please let me know):
Code:
coord_list = ds_list_create();
ds_list_add(coord_list,[32]);//stores the y coord
ds_list_add(coord_list,[192]);//stores the y coord
ds_list_add(coord_list,[352]);//stores the y coord
var rand = irandom(ds_list_size(coord_list)-1);//randomly assigns a Y coord a number
var pos = coord_list[| rand];//stores the y coord number in pos
var yy = pos[0];stores the y coord number in pos in var yy

instance_create_layer(384,yy,"instances",obj_chicken)
But how would I make the instances in each different Y position? If I just created another instance with the same instance create code right after the first, would it automatically use a different Y value? That is to ask, if the first instance is created at y(32), is there a chance the next instance could also be created at y(32)?

Or do I have to destroy the list and then create it again without storing y(32)?

Any help would be appreciated. Thanks.
Since you have a ds_list already, just ds_shuffle() it and use the three first entries. That way you have random ordering along with not having to worry about picking the same coordinate more than once.

Edit: picking the three coordinates should be done from the same list. From your code it looks like you're creating the list for every instance creation.
 
Since you have a ds_list already, just ds_shuffle() it and use the three first entries. That way you have random ordering along with not having to worry about picking the same coordinate more than once.

Edit: picking the three coordinates should be done from the same list. From your code it looks like you're creating the list for every instance creation.
So is this correct:
Code:
coord_list = ds_list_create();
ds_list_add(coord_list,[32]);//stores the y coord
ds_list_add(coord_list,[192]);//stores the y coord
ds_list_add(coord_list,[352]);//stores the y coord
var rand = irandom(ds_list_size(coord_list)-1);//randomly assigns a Y coord a number
var pos = coord_list[| rand];//stores the y coord number in pos
var yy = pos[0];stores the y coord number in pos in var yy
ds_list_shuffle(coord_list)

instance_create_layer(384,yy,"instances",obj_chicken)
instance_create_layer(384,yy,"instances",obj_chicken_1)
instance_create_layer(384,yy,"instances",obj_chicken_2)
 

chamaeleon

Member
So is this correct:
Code:
coord_list = ds_list_create();
ds_list_add(coord_list,[32]);//stores the y coord
ds_list_add(coord_list,[192]);//stores the y coord
ds_list_add(coord_list,[352]);//stores the y coord
var rand = irandom(ds_list_size(coord_list)-1);//randomly assigns a Y coord a number
var pos = coord_list[| rand];//stores the y coord number in pos
var yy = pos[0];stores the y coord number in pos in var yy
ds_list_shuffle(coord_list)

instance_create_layer(384,yy,"instances",obj_chicken)
instance_create_layer(384,yy,"instances",obj_chicken_1)
instance_create_layer(384,yy,"instances",obj_chicken_2)
The storing of an array in the list is not needed for this use case.
Code:
coord_list = ds_list_create();
ds_list_add(coord_list,32);//stores the y coord
ds_list_add(coord_list,192);//stores the y coord
ds_list_add(coord_list,352);//stores the y coord
ds_list_shuffle(coord_list)

instance_create_layer(384,coord_list[| 0],"instances",obj_chicken)
instance_create_layer(384,coord_list[| 1],"instances",obj_chicken_1)
instance_create_layer(384,coord_list[| 2],"instances",obj_chicken_2)

ds_list_destroy(coord_list)
(Sorry for not getting the ds_list_shuffle() name correct, but at least you found it anyway)
 
The storing of an array in the list is not needed for this use case.
Code:
coord_list = ds_list_create();
ds_list_add(coord_list,32);//stores the y coord
ds_list_add(coord_list,192);//stores the y coord
ds_list_add(coord_list,352);//stores the y coord
ds_list_shuffle(coord_list)

instance_create_layer(384,coord_list[| 0],"instances",obj_chicken)
instance_create_layer(384,coord_list[| 1],"instances",obj_chicken_1)
instance_create_layer(384,coord_list[| 2],"instances",obj_chicken_2)

ds_list_destroy(coord_list)
(Sorry for not getting the ds_list_shuffle() name correct, but at least you found it anyway)
Okay, that makes sense. However I get this error code:
instance_create_layer argument 2 incorrect type (array) expecting a Number (YYGF)

This means its expecting a number and getting a string instead(?) although I don't know why.
 
Last edited:
Top