Search code examples
game-maker

GameMaker Studio: How to ignore global left button when dealing with virtual keys?


I have the following problem:

When I press the pause button my characters jumps at the same time, the pause button being a VIRTUAL KEY and the jump button is keyboard_mouse_button_check(mb_left) .

I am trying to:

be able to press the pause button without making the character jump at the same time.

Obj_pause

Create Event

global.pause = 0;

Step Event

if keyboard_check_pressed(vk_space) and !global.death and !global.stop
    global.pause = !global.pause

Obj_player

Create Event

// Initialize Variables
global.stop = 0;
previously_paused = false;
global.bigscore = 0;
global.col = true;
global.cartof = 0;
grav = 0.2;
hsp = 0;
vsp = 0;
jumpspeed = 4;
movespeed = 4;
antigrav = true;
jumps = 0;

jumpsmax = 2;

delay = 3;
alarm[3] = delay;

Step Event

if (global.pause) or (global.death) exit;   // Pause

// Get the player's input

key_jump = mouse_check_button(mb_left);

// React to inputs
hsp = 0;
if vsp < 10
    vsp += grav;

if place_meeting(x, y + 1, obj_wall)
    jumps = jumpsmax;

// Double jump
if (key_jump) && (jumps > 0) && (!previously_paused)
{
    jumps -= 1;
    vsp = -jumpspeed;
    previously_paused = global.pause;
}
// Fly
else if (jumps == 0)
{
    key_jump = mouse_check_button(mb_left);
    if key_jump
        vsp -= 0.4
}

if global.col
{
    // Horizontal Collision
    if (place_meeting(x+hsp, y, obj_wall))
    {
        while (!place_meeting(x + sign(hsp), y, obj_wall))
        {
            x += sign(hsp);
        }
        hsp = 0;     
    }
    x += hsp;
}

// Vertical Collision
if (place_meeting(x, y + vsp, obj_wall))
{
    while (!place_meeting(x, y + sign(vsp), obj_wall))
    {
        y += sign(vsp);
    }
    vsp = 0;     
}
y += vsp;   

// Exit the game or restart
if keyboard_check(ord("P"))
    game_restart();

if keyboard_check(vk_space)
    alarm[1] = room_speed;

// Score
if background_hspeed[0] < 0
    global.cartof += 10;

I have tried:

Check if I press the pause button don't do the jump To change key_jump to be equal 0 when I click on the pause button. When mouse is over the pause button to dezactivate the mouse ArchbishopDave Method

I am using:

GM:S 1.4

GameMaker Language (GML) or Drag and Drop (DnD)


Solution

  • Did you read event order? Step events will be runned before Keyboard and Mouse events (and virtual buttons too). So if you checked jump button in Step event, and only after it will be pressed virtual key, then your character always will jump. You need change order of checks. For example, you can manually check, is pressed virtual key or not, before jump (check, is mouse inside coords of virtual key or not), something like this:

    key_jump = mouse_check_button(mb_left);
    if point_in_rectangle(device_mouse_x_to_gui(0), device_mouse_y_to_gui(0), virt_left, virt_top, virt_right, virt_bottom)
        key_jump = false;
    

    where virt_left, virt_top, virt_right, virt_bottom is coords of your virtual button.

    There are many ways for improve it, but all depend on your code/architecture (depth of objects, creation order of instances, etc), so I can't give a finihsed perfect solution. If you will have a problem with fix, just share your project and I'll help.