1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GM:S 1.4 Returning Instance ID's/Choosing random Instance

Discussion in 'Programming' started by Bogan666, May 28, 2019.

  1. Bogan666

    Bogan666 Member

    Joined:
    Jul 9, 2018
    Posts:
    32
    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.
     
  2. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    874
    @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: May 28, 2019
  3. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,612
    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));
    
     
  4. Bogan666

    Bogan666 Member

    Joined:
    Jul 9, 2018
    Posts:
    32
    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.
     
  5. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,612
    Learn the difference between objects and instances. instance_find() returns an instance ID, you can reference coordinates off that.
    Code:
    var inst_dirt = instance_find(obj_dirt, irandom(instance_number(obj_dirt)-1));
    instance_create(inst_dirt.x, inst_dirt.y, obj_new);
    
     
  6. Bogan666

    Bogan666 Member

    Joined:
    Jul 9, 2018
    Posts:
    32
    A
    Ahh yep, ty ty. I forgot the ".x and .y", got it working now, thank you.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice