• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

On keypress wait random time and execute code

A

alib0ng0

Guest
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
 
T

Taddio

Guest
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?
 
A

alib0ng0

Guest
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];
 
A

alib0ng0

Guest
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!!!
 
T

Taddio

Guest
Yeah, exactly! You need an alarm event!
Glad you got it to work!
 
Top