GM:S 1.4 [HELP]Choosing Instance_ID for specific object

Discussion in 'Programming' started by Ryscale, Sep 10, 2019.

  1. Ryscale

    Ryscale Member

    Joined:
    Aug 20, 2019
    Posts:
    6
    Hello everyone

    Firstly I would like to introduce to you my problem, at first i was using a line of code which was for a object to go to point A and go to point B( point A and B are objects). There are multiple Point B's in the map near one another but the object only picks 1 the same one every time.

    Now that i added a picker which choose's the ID of a flower and does the exact thing but now its just broke, below is the code.

    >>>>>>>>>>>>>>I added this within my create event..

    flower1 = inst_4D092D21;
    flower2 = inst_536C30F0;
    picker = choose(flower1, flower2);

    >>>>>>>>>>>>>and this is the step event

    ///moving to flower

    if (land = false and start = true) {
    direction = point_direction(x,y,picker.x,picker.y);
    }

    ///if collide with flower move to hive

    if (land = false and place_meeting (x,y,picker)){
    land = true;
    start = false;
    }


    ///moving to beehive

    if (land = false and start = false){
    direction = point_direction(x,y,o_bee_col.x,o_bee_col.y);
    }


    ///if collide with hive move to flower

    if (land = false and place_meeting (x,y,o_bee_col)){
    land = true;
    start = true;
    }

    ///timer
    if (land = true){
    speed = 0;
    if (timer == 0){
    land = false;
    speed = 1;
    picker = choose(flower1, flower2);

    }
    }

    >>>>>>>>>>>>>>>>alarm 0 event

    alarm[0] = irandom_range(5,15);
    if (land = true){
    timer -= 1;
    }
     
  2. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    760
    Code:
    picker = instance_find(obj_flower, irandom(instance_number(obj_flower) - 1));
    this will select a random flower and return it.
    I'm not sure what you were doing with the inst_4D092D21 unless that's a really really weird variable, or object name that you came up with
    I know you were probobly trying to use the 'id' variable that comes when you print 'id' or when you look at the room editor but you can't directly use the id like that.

    EDIT:
    i guess i should elaborate.
    instance_find - returns the n'th instance of a specific object (so like the 3rd or 4th flower in your room)
    irandom - returns a random number from 0 to n.
    instance_number - returns how many instances of a specific object there are (in this case how many flowers are in the room)

    so we use irandom to choose a number from 0 to however-many-flowers-there-are
    then we use that to find that specific flower.

    So if there are 5 flowers it choose a number from 0-4
    if it chose, lets say 2, then it would find the 2nd flower (well actually 3rd because it starts at 0) using instance_find
     
    Last edited: Sep 10, 2019
  3. Ryscale

    Ryscale Member

    Joined:
    Aug 20, 2019
    Posts:
    6
    Firstly thank you for replying and sorry for being complete noob where about's do i put that line of code you sent me? Create event?

    EDIT: ive added it to my create event and (im not sure if i need to refresh it everytime) my timer event. But still the bee's wont leave the hive to return to the flowers.
     
    Last edited: Sep 10, 2019
  4. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    760
    right now you have:
    Code:
    picker = choose(flower1, flower2);
    in order to select a random flower.
    Code:
    picker = instance_find(obj_flower, irandom(instance_number(obj_flower) - 1));
    this is just how you should be picking a flower,
     
  5. Ryscale

    Ryscale Member

    Joined:
    Aug 20, 2019
    Posts:
    6
    Again thank you for replying, i was able to achieve the random objects through the code you suggested, but the bees still won't go back to a flower after visiting the hive. I checked on the debugger the timer works perfectly. But the timer will not work once it gets to the hive. (it worked perfectly before when picker was just the object)
     
  6. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    760
    you have 2 places that you do:
    Code:
    picker = choose(flower1, flower2);
    once in the create and once in the end of the step. Did you make sure to replace both?
     
  7. Ryscale

    Ryscale Member

    Joined:
    Aug 20, 2019
    Posts:
    6
    Yes! I replaced them both, but for some reason the timer doesnt work since i changed the object to picker.
     

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