1. Hey! Guest! The 32nd GMC Jam will take place between Feb 22nd, 12:00 UTC (Friday noon) and Feb 25th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

On keypress wait random time and execute code

Discussion in 'Programming' started by alib0ng0, Jan 18, 2019.

  1. alib0ng0

    alib0ng0 Member

    Joined:
    Nov 28, 2018
    Posts:
    13
    Hi there, I'm fairly new to Gamemaker 2, so go easy on me.

    I have a game where a stick of dynamite is placed on keypress Q - i want to wait for a random interval between 3-6 seconds and then execute my destroy_instance() code.

    I have tried using alarms without much luck.

    Any help would be appreciated, thanks
     
  2. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    158
    Alarms would be the way I'd do it.
    When keyboard_check_pressed(ord("Q")) Set alarm0 to room_speed*(random_range(3,6));
    And then instance_destroy(); in alarm0 event.
    What's the alarm code you tried?
     
  3. alib0ng0

    alib0ng0 Member

    Joined:
    Nov 28, 2018
    Posts:
    13
    This is what I have...

    On keypress Q...

    Code:
    if (point_in_circle(oPlayer.x,oPlayer.y,x,y,64)){
    instance_create_layer(oDestroy.x+10,oDestroy.y+40,"Dynamite",oDynamite);
    //generate random time and execute
    alarm[0];
    instance_destroy();
    }
    Do i have to create the alarm as an event and then call it? alarm[0];
     
  4. alib0ng0

    alib0ng0 Member

    Joined:
    Nov 28, 2018
    Posts:
    13
    I've got it - create an Alarm 0 event and put all the executable code in there - then on the key press event (Q) - place this code: alarm[0] = room_speed*(random_range(3,6)); - after 3-6 seconds it will execute the code!

    Thanks!!!
     
    Taddio likes this.
  5. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    158
    Yeah, exactly! You need an alarm event!
    Glad you got it to work!
     

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