Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411413 Posts in 69360 Topics- by 58415 Members - Latest Member: sophi_26

April 16, 2024, 04:39:21 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Pausing/Unpausing with keyboard and gamepad?
Pages: [1]
Print
Author Topic: Pausing/Unpausing with keyboard and gamepad?  (Read 1558 times)
ScaryPotato
Level 2
**



View Profile
« on: June 21, 2016, 06:03:27 AM »

Hey everybody,

I'm using Gamemaker 8.1, and am having an issue with unpausing. For the keyboard it works fine, but I am unable to make it work with a gamepad. I've tried a bunch of different methods, and it seems like the simplest way would be to simulate the 'P' key being pressed when a button is pressed on the gamepad, but the code below doesn't unpause. Could someone please point out to me what the heck I'm doing wrong and how to go about making it work? I'd really appreciate it!

Code:
if joystick_check_button(1,8)
{keyboard_key_press(ord('P'));}//simulates the 'P' key being pressed



if (keyboard_check_pressed(ord('P'))) //if key is currently pressed    
{        
    io_clear();//clears keyboard and mouse states      
    pause=1; //variable to control actions when game is paused          
    sound_loop(PauseLoop); //Pause BGM loop
    instance_create(view_xview,view_yview,PauseScreenObj); //image that covers the viewport
    instance_create(view_xview,view_yview,PauseTextObj);//'Paused' text that goes ontop of PauseScreenObj
              
    with PauseScreenObj
    {image_alpha=0.5;}//makes it so you can see the state of the game when you paused it
            
    while (pause>0)//originally (pause==1)    
    {            
            screen_redraw(); //if there are any Draw Events, this should keep them on-screen          
            io_handle();//updates keyboard and mouse states
            
            //new stuff
            
            //if joystick_check_button(1,8)
            //{keyboard_key_press(ord('P'));}//simulates the 'P' key being pressed
            
            if !keyboard_check_pressed(ord('P')) and pause=1//if button isn't pressed, increase value of variable
            {pause=2;}
                        
            //end of new stuff
            
                if pause=2//originall pause>0        
                {
                    
                    if keyboard_check_pressed(ord('P'))//if key is pressed            
                    {
                        sound_stop(PauseLoop);//stop the Pause BGM
                        ss=SoundObj;//variable for Game BGM
                        if (ss != noone)//check that there is a song to play
                        {sound_loop(ss.Song);}//loops the song
                                      
                        pause=0;                                
                        with (PauseScreenObj) instance_destroy();//get rid of blackened screen  
                        with (PauseTextObj) instance_destroy(); //get rif of 'Paused' text        
                    }                    
                }
    }
}
Logged

ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #1 on: June 21, 2016, 08:10:47 AM »

Replace this:
Code:
if joystick_check_button(1,8)
{keyboard_key_press(ord('P'));}

...With this:
Code:
if joystick_check_button_pressed(1,8)
{keyboard_key_press(ord('P'));}
if joystick_check_button_released(1,8)
{keyboard_key_release(ord('P'));}

I bet you're pretty dumbfounded as to how dumb Game Maker can be right now huh?
Logged

ScaryPotato
Level 2
**



View Profile
« Reply #2 on: June 21, 2016, 08:24:24 AM »

Thanks for the quick response! The joystick_check_button_released() isn't something I ever considered because it wasn't part of the list of commands...which struck me as incredibly odd. Damn. And now I feel like a total ass for not at least trying it. I'll try it out when I'm home and post an update  Smiley
Logged

ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #3 on: June 21, 2016, 08:36:08 AM »

Oh, if you're using GM 8.1 that might be different because I was going off of the GM:S documentation. If you encounter problems with that then just post here again!
Logged

ScaryPotato
Level 2
**



View Profile
« Reply #4 on: June 22, 2016, 05:24:05 AM »

Well, those commands must have been added into GM:S, because they're not recognized by Gamemaker8.1.  Seems silly that they'd only have a command to check whether or not the button is held down! It got me thinking though, and I figured with some variables, I could try to get something to work. Here's the code I tried (with some NEW STUFF de-activated):

Code:
if joystick_check_button(1,8) and pause=0//the Start Button on the XBox controller
{
    keyboard_key_press(ord('P'));//simulates the 'P' key being pressed
    pause=1;
}



if (keyboard_check_pressed(ord('P'))) //if key is currently pressed   
{       
    io_clear();//clears keyboard and mouse states       
    pause=1; //variable to control actions when game is paused         
    sound_loop(PauseLoop); //Pause BGM loop
    instance_create(view_xview,view_yview,PauseScreenObj); //image that covers the viewport
    instance_create(view_xview,view_yview,PauseTextObj);//'Paused' text that goes ontop of PauseScreenObj
             
    with PauseScreenObj
    {image_alpha=0.5;}//makes it so you can see the state of the game when you paused it
           
    while (pause==1)   
    {           
            screen_redraw(); //if there are any Draw Events, this should keep them on-screen         
            io_handle();//updates keyboard and mouse states
           
            //NEW STUFF
            //if joystick_exists(1)//to make sure the following commands only take place if the gamepad is used
            //{
               //if !joystick_check_button(1,8) and pause=1//if the gamepad button isn't pressed and 'pause' is a certail value
                //{
                    //keyboard_key_release(ord('P'));//simulates the 'P' key being released
                    //pause=2;
                   
                    //if joystick_check_button(1,8) and pause=2//changing the 'pause' variable to allow the command to stop
                    //{pause=3;}   
                //}
            //}
            //END OF NEW STUFF
           
            //if pause>0         
            //{                   
                    if keyboard_check_pressed(ord('P'))||pause=3//if key is pressed OR if 'pause' variable is the right value         
                    {
                        sound_stop(PauseLoop);//stop the Pause BGM
                        ss=SoundObj;//variable for Game BGM
                        if (ss != noone)//check that there is a song to play
                        {sound_loop(ss.Song);}//loops the song
                                     
                        pause=0;                               
                        with (PauseScreenObj) instance_destroy();//get rid of blackened screen   
                        with (PauseTextObj) instance_destroy(); //get rif of 'Paused' text         
                    }                   
            //}
    }
}

The really weird thing about this new bit of code is that if the NEW STUFF is active, using the keyboard lets you create the PauseScreenObj and Pause TextObj over and over without unpausing.
I could really use some help on how to get this bit of code working properly, because I'm starting to confuse myself!
Logged

ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #5 on: June 22, 2016, 05:56:49 AM »

Maybe this will work instead?

Code:
if joystick_check_button(1,8)
{keyboard_key_press(ord('P'));}
else
{keyboard_key_release(ord('P'));}
Logged

ScaryPotato
Level 2
**



View Profile
« Reply #6 on: June 22, 2016, 06:36:26 AM »

It seems like joystick_check_button(id,numb) only checks whether or not the button is initially pressed. With the idea you had, it cycles through pause/unpause if the button is held down.
I still can't believe that the press/release functions weren't part of the commands in GM8.1 for gamepads  Lips Sealed
Logged

Alessio
Level 0
***


Visual Artist


View Profile
« Reply #7 on: June 29, 2016, 01:31:30 PM »

Not to bump the thread but aren't "joystick_check_button_pressed" and "joystick_check_button_pressed" inexistent in both Game Maker 8 and Game Maker: Studio? I believe they only exist for the new gamepad functions but not for the legacy ones. I agree, that was pretty lame by YoYo, even if their tool is great.

Edit: vv post below vv  No problem at least is cleared up. Wink
« Last Edit: June 29, 2016, 02:06:30 PM by Alessio » Logged
ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #8 on: June 29, 2016, 02:03:17 PM »

Yup, my bad. I mixed up the gamepad and joystick functions here. Sorry :/
Logged

ScaryPotato
Level 2
**



View Profile
« Reply #9 on: June 30, 2016, 08:00:19 PM »

Hey guys, sorry about the lack of responses. Nothing I tried worked with the code in the first post, so I scrapped it for a completely different method. The new way works, but it's such a friggin' patchwork of garbage. It's funny. When GM8.1 was released, I really enjoyed using it, but having to go back to it after using GM:S was frustrating!

If you want, I can post the code, but since GM8.1 isn't something anyone uses willingly these days, it might be better to let this thread disappear forever!

Any moderators that see this, feel free to delete the thread Smiley
Logged

quantumpotato
Quantum Potato
Level 10
*****



View Profile WWW
« Reply #10 on: August 16, 2016, 05:55:23 AM »

Disclaimer: This wasn't Gamemaker, but I had a similar issue handling pause with keyboard & gamepad in Quantum Pilot recently.

I ended up keeping a dictionary of which keyboard buttons were pressed and which ones weren't, and a boolean for tracking if you were currently "pausing", which got assigned to true when you pressed Esc. This paused the game. If you were already pausing, then don't do anything else. If the game is paused & you're pressing Esc again and you're not already "pausing" (holding ESC), then the game quits to menu.

With the gamepad I recorded which action you were taking menu down, menu up or pausing (which button you were pressing) to prevent holding the control stick from rapidly scrolling or.

It's also not pretty but I'll post it here for your reference:

(A 'Pauser' is the name of the pause menu..)
(the controller flag is used for the Pauser menu to display text on using the keyboard or gamepad shoot button to resume.)
(The bit about the clone count is to prevent you from pressing Start on your Xbox controller on the menu and have the game pause immediately upon entering the game because you were still holding Start. The Title screen and Game are different instances so the Game starts fresh without any input flags set)

Code:
          pause(controller) {
this.paused = true;
this.pressingEsc = true;
this.pauser = new Pauser();
this.pauser.active = true;
if (controller) {
this.pauser.controller = true;
}
this.add('pause', this.pauser);
}

parseInput(key_pressed_map, key_up_map, key_pressing_map, key_depressing_map) {
if (key_up_map['ESC']) {
this.pressingEsc = false;
}

if (key_pressed_map['ESC'] == true) {
if (!this.paused) {
this.pause();
return;
} else if (this.pressingEsc == false) {
this.returnToMenu();
}
                }

(in parseGamepadInput)

if (this.paused && this.pilots[playerIndex].firing) {
this.unpause();
}

if (input.buttons[8].pressed || input.buttons[9].pressed) {
if (this.paused && !this.pausing) {
this.pausing = false;
this.returnToMenu();
} else if (this.cloneCount > 1 || this.p1.recorder.timeIndex > 30) {
this.pause();
this.pausing = true;
}
} else {
this.pausing = false;
}
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic