Wait function

N

NANI?

Guest
Hello, In a game I'm developing, you have agun. I already have the gun firing properly, but I need to make it semi auto. With my current code, it fires fully auto as long as the lef tmouse is held down. I was thinking maybe if I did something like

if (mouse_check_button(mb_left)){
instance_create_layer(x,y,layer,Bullet)
wait 1 second
repeat
}

or

Repeat{
if (mouse_check_button(mb_left))
instance_create_layer(x,y,layer,Bullet)
wait 1 second
}

(not sure which would work better), I oculd achieve it so that it fires once every second key is held down, instead of firing indefinitely. However, there is no wait command. How would I achieve something like this?
 
A

AttaBoy

Guest
What you want to do is have a counter that goes up as long as the button is held, and when it goes above a certain point (after a second) a bullet is fired. The pseudo code would roughly be
Code:
if (left click held down) {
    counter++;
    if (counter >= 60) {
        fire a bullet
        reset counter
    }
}
This assumes that you run at 60 frames per second. I would also set the counter to 60 when the mouse is clicked so there is a bullet fired immediately.
 
N

NANI?

Guest
What you want to do is have a counter that goes up as long as the button is held, and when it goes above a certain point (after a second) a bullet is fired. The pseudo code would roughly be
Code:
if (left click held down) {
    counter++;
    if (counter >= 60) {
        fire a bullet
        reset counter
    }
}
This assumes that you run at 60 frames per second. I would also set the counter to 60 when the mouse is clicked so there is a bullet fired immediately.
how do Iget a counter? it isn't recognized as a command
 
W

WolfHybrid23

Guest
You create the counter variable in the create event and increment the variable
Also to what AttaBoy said here is a modification that checks for if a value is equal to a second in steps that changes with your game:
Code:
if(counter >= room_speed)
 
E

Ephemeral

Guest
I was thinking maybe if I did something like
You can't do that. The reason you can't do that, is because if you could do that, the only effect would be to cause your entire game to hang for one second. The fundamental concept you need to be aware of, is that any given piece of code you're working with will execute, in its entirety, in the same instant. Waiting within a piece of code would just lock up your game for that much time. If you want time to pass, you have to track how many times a piece of code runs.

But, you know a Step Event will happen 60 times each second, because you set that number when you created your project, presumably.

So,

Create Event:
Code:
gun_delay = 60; // how long you want to wait for the first shot
Step Event:
Code:
if (gun_delay > 0)
{
    gun_delay -= 1; // count down to the next shot by subtracting 1 from gun_delay
}
else
{
    if (mouse_check_button(mb_left))
    {
        instance_create_layer(x, y, layer, Bullet); // fire the bullet
        gun_delay = 60; // reset the time to wait until the next shot
    }
}
 

jackquake

Member
One more method, just for fun, uses the alarm function...

Create Event
Code:
canFire = true;
Step Event
Code:
if (mouse_check_button(mb_left) and canFire)
   {
       instance_create_layer(x, y, layer, Bullet); // fire the bullet
       canFire = false;
       alarm[0] = 60; // reset the time to wait until the next shot
   }
}
Alarm 0 Event
Code:
canFire = true;
 
E

Ephemeral

Guest
Or, if you want to do it the maximally robust, idiot-proof way:

Create Event:
Code:
firing_delay = 0.5 // number of seconds between shots
gun_wait = 0; // No delay on first shot
game_speed = game_get_speed(gamespeed_fps); // Get the game Steps Per Second from the project settings.
Step Event:
Code:
if (gun_wait > 0)
{
    gun_wait -= 1; // count down to the next shot by subtracting 1 each frame
}
else
{
    if (mouse_check_button(mb_left))
    {
        var bul;
        bul = instance_create_layer(x, y, layer, Bullet); // fire the bullet and store its instance_id in a temporary variable.
        // set any bullet variables that are necessary, such as  direction  maybe, using  bul.<variable>
        gun_wait = firing_delay * game_speed; // multiply the number of seconds by the fps to get wait time in frames
    }
}
 
Top