Legacy GM Returning Instance ID's/Choosing random Instance

B

Bogan666

Guest
I have currently got setup a randomly generated world with different types of instances being spawned, the main one which is obj_dirt.
I'm wanting to, after the world has finished generating to choose from a random instance of obj_dirt.
Is there a way to list all the occurrences of this object and randomly choose one from that list?

Or perhaps a different way to randomly select one of the desired objects, thanks.
 

CloseRange

Member
@Bogan666
The most straight forward way is to create a pool and choose an object from that pool.
Just make an empty array and fill it with all the dirt objects, then choose from that array.
Code:
var length = 0;
list[0] = noone;
with(obj_dirt) {
     other.list[length++] = id;
}
var chosen_one = list[irandom(length)];
Of course this is going to suck because you have to remake this list for every single dirt object so it might be a bit slow (if you have 100 dirt objects this would take 100^100 iterations to complete)

A better way is to take some core object (perhaps you could do it on the object that created the objects in the first place) and make this list,
then in every dirt object choose randomly:
Code:
dirt_length = 0;
dirt_list[0] = noone;
with(obj_dirt) {
     other.dirt_list[other.dirt_length++] = id;
}
with(obj_dirt) {
     var i = irandom(other.dirt_length);
     chosen_one = other.dirt_list[i];
}
now if there are 100 dirt objects it would only take 100+100 time
 
Last edited:

FrostyCat

Redemption Seeker
There's no need to make a list first when picking a single random instance of a given object, unless you're concerned about duplicates with previous picks.
Code:
var inst_dirt = instance_find(obj_dirt, irandom(instance_number(obj_dirt)-1));
 
B

Bogan666

Guest
There's no need to make a list first when picking a single random instance of a given object, unless you're concerned about duplicates with previous picks.
Code:
var inst_dirt = instance_find(obj_dirt, irandom(instance_number(obj_dirt)-1));
say I used this, how would I return the value of the chosen object?
So I would want to use this to spawn an object at the chosen obj_dirt.
 
Top