• 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!

decrease variable holding gamepad button

I

Ivankov

Guest
I have a global variable called global.fuel.

I want it so while holding down the game pad button gp_face1 it will decrease global.fuel by 1 every second.

At the moment I have the following and it works well at making my ship fly.
Code:
if gamepad_button_check_pressed(0, gp_face1)
{vspeed=-4
global.ongrav=0
}
 

obscene

Member
if you can decrease why can you not decrease global.fuel the same way? If your game runs at 60 FPS, you would reduce global.fuel by 1/60th every step.
 
W

Wraithious

Guest
You could use an alarm, a local variable and your global.fuel variable,
Create event:
Code:
can_drain=1;
Alarm event:
Code:
can_drain=1;
Then modify your code:
Code:
if gamepad_button_check_pressed(0, gp_face1)
{vspeed=-4;
global.ongrav=0;
if(can_drain=1)
    {
    global.fuel-=1;
    alarm[0]=room_speed;
    can_drain=0;
    }
}
 

Roderick

Member
I have a global variable called global.fuel.

I want it so while holding down the game pad button gp_face1 it will decrease global.fuel by 1 every second.

At the moment I have the following and it works well at making my ship fly.
Code:
if gamepad_button_check_pressed(0, gp_face1)
{vspeed=-4
global.ongrav=0
}
Code:
global.fuel -= 1/room_speed
If you're displaying global.fuel as a number rather than a health bar, use floor(global.fuel) to drop the fraction, or ceil(global.fuel) to round it up.
 
I

Ivankov

Guest
Thanks guys.
@Wraithious It works at reducing the fuel amount by one when I first press the gamepad button
gp_face1. However it wont continue to reduce it while I am holding the button. So no matter how long hold it , it just gets reduced by 1. Until I press it again.

@Roderick same issue. It reduces but only when I press it not while holding it down. Also floor(global.fuel) and ceil(global.fuel) gave me unexpected symbol at position 24. Which would in this case be the minus symbol
floor(global.fuel) -= 1/room_speed and same with ceil(global.fuel)


You could use an alarm, a local variable and your global.fuel variable,
Create event:
Code:
can_drain=1;
Alarm event:
Code:
can_drain=1;
Then modify your code:
Code:
if gamepad_button_check_pressed(0, gp_face1)
{vspeed=-4;
global.ongrav=0;
if(can_drain=1)
    {
    global.fuel-=1;
    alarm[0]=room_speed;
    can_drain=0;
    }
}
Code:
global.fuel -= 1/room_speed
If you're displaying global.fuel as a number rather than a health bar, use floor(global.fuel) to drop the fraction, or ceil(global.fuel) to round it up.
 

Roderick

Member
Also floor(global.fuel) and ceil(global.fuel) gave me unexpected symbol at position 24. Which would in this case be the minus symbol
floor(global.fuel) -= 1/room_speed and same with ceil(global.fuel)
That's because you're using floor and ceil in the wrong spots.

When you change the value, use
Code:
global.fuel -= 1/room_speed
When you draw the number on the screen, use something similar to
Code:
draw_text(0, 0, "Fuel remaining: " + string(floor(global.fuel)))
 
I

Ivankov

Guest
Your right thanks guys!

That's because you're using gamepad_button_check_pressed, which only triggers when you first press the button. If you want the code to trigger every step, as long as the button is held, use gamepad_button_check.
Use gamepad_button_check(device, button);
That's because you're using floor and ceil in the wrong spots.

When you change the value, use
Code:
global.fuel -= 1/room_speed
When you draw the number on the screen, use something similar to
Code:
draw_text(0, 0, "Fuel remaining: " + string(floor(global.fuel)))
 
Top