Jump to content


Photo

Questions on Global Variables


  • Please log in to reply
5 replies to this topic

#1 Christovix

Christovix

    GMC Member

  • New Member
  • 11 posts
  • Version:GM:Studio

Posted 28 March 2016 - 09:16 PM

So I'm making a platformer and I have global.platform defined as such:

global.platform = (obj_reg_ground) || (obj_reg_breakablebox) || (obj_reg_moveablebox)

Would this work when used in this code? It doesn't seem to be however the next problem listed below (controls not working) makes this one hard to test. But when using this global variable, my player just falls through the ground at game start.

if ((hspd > 0) &&  (place_meeting(x, y+1, global.platform))) { sprite_index = spr_hank_run };

I want this code to mean: if player is moving horizontally, and the place just below them is one of the objects listed in global.platform, the sprite displayed is spr_hank_run

The code just isn't working, I tried parenting all my solid objects from a single object that I can use in this code instead of  global.platform, and it seems to be working now (my player stays on the ground), but I really don't want to have to parent all of the solid objects in the game to each other so that collisions will work. I'd prefer to stick them in a global variable and reference that like I did at first.

Also, a similar issue (or exact same issue probably), my controls aren't working, and I have a hunch that I'm not setting up global variables with || operators properly. I'm wanting players to easily be able to simply use WASD, Arrrows, or a Gamepad at any time. So I've set up global variables for the controls, and the control code I'm using does work when I just toss in a standard keyboard_check instead of global.left etc. Is there a way I can make this work? I'm trying to get the framework for everything before I start building levels and such, so I don't have to sift through ton's of code that's been changed over the course of building a game. This is an effort to make everything as clean as possible. See below:
 

global.right    = ((keyboard_check(vk_right))           || (keyboard_check(ord('D')))           || (gamepad_button_check(0, gp_padr)))
global.left     = ((keyboard_check(vk_left))            || (keyboard_check(ord('A')))           || (gamepad_button_check(0, gp_padl)))
global.up       = ((keyboard_check(vk_up))              || (keyboard_check(ord('W'))            || (gamepad_button_check(0, gp_padu)))
global.down     = ((keyboard_check(vk_down))            || (keyboard_check(ord('S')))           || (gamepad_button_check(0, gp_padd)))
global.jump     = ((keyboard_check_pressed(vk_space))   || (keyboard_check_pressed(ord('J')))   || (gamepad_button_check_pressed(0, gp_face1)))
global.shoot    =                                          ((keyboard_check_pressed(ord('I')))  || (gamepad_button_check_pressed(0, gp_face3)))
global.pickup   =                                          ((keyboard_check_pressed(ord('K')))  || (gamepad_button_check_pressed(0, gp_face2)))
global.itemuse  =                                          ((keyboard_check_pressed(ord('L')))  || (gamepad_button_check_pressed(0, gp_face4)))   

Edited by Christovix, 28 March 2016 - 09:24 PM.

  • 0

#2 TsukaYuriko

TsukaYuriko

    Remember... and never forget

  • Global Moderators
  • 9535 posts
  • Version:GM:Studio

Posted 28 March 2016 - 09:17 PM

http://gmc.yoyogames...howtopic=652746

 

 

Misguided belief #2.


  • 0

yBGBXQa.pngmWOQbeq.png


#3 Christovix

Christovix

    GMC Member

  • New Member
  • 11 posts
  • Version:GM:Studio

Posted 28 March 2016 - 09:30 PM

http://gmc.yoyogames...howtopic=652746

 

 

Misguided belief #2.

So please correct me if I'm wrong, I'm trying to understand this. This also doesn't seem right to me...

global.platform = (obj_reg_ground) || global.platform = (obj_reg_breakablebox) || global.platform = (obj_reg_moveablebox)

Also...

global.right    = (keyboard_check(vk_right))          || global.right = (keyboard_check(ord('D')))           || global.right = (gamepad_button_check(0, gp_padr))

Last minute revelation...
I can just do this can't I? 

global.right    = (keyboard_check(vk_right))
global.right = (keyboard_check(ord('D'))
global.right = (gamepad_button_check(0, gp_padr))

//and

global.platform = (obj_reg_ground)
global.platform = (obj_reg_breakablebox)
global.platform = (obj_reg_moveablebox)

That would define all those things as global.platform? Or would it overwrite the first two global.platform definitions, when the third definition is read?

 


  • 0

#4 cdrdub8

cdrdub8

    GMC Member

  • GMC Member
  • 518 posts
  • Version:GM:Studio

Posted 28 March 2016 - 09:55 PM

What you want to do is check a collision against a GROUP OF OBJECTS instead of checking

a collision with each of these objects seperatly, is that right?

 

If that's the case, then instead of using global.platform, create an object called obj_all_platforms ( leave it empty ).

Then, for all your objects ( so obj_reg_ground, obj_reg_breakablebox, obj_reg_moveablebox ) set their parent

to obj_all_platforms. ( it's under the sprite selection in the object editor )

 

Now, in your code, whennever you want to check a collision with all of those objects at the same time, you can use obj_all_platforms:

if ((hspd > 0) &&  (place_meeting(x, y+1, obj_all_platforms))) { sprite_index = spr_hank_run };

 

Hope this helps ~

- Cdrdub8


  • 0

I am building awesomeness, wait for it!


#5 TsukaYuriko

TsukaYuriko

    Remember... and never forget

  • Global Moderators
  • 9535 posts
  • Version:GM:Studio

Posted 28 March 2016 - 09:59 PM

Variables can only hold one value at a time. Assigning a value to a variable that already holds a value will overwrite it.

 

 

The way your control checks were initially set up technically works. You might want to consider just checking the one that is currently configured to be used in the game's options, though, so you won't be checking who knows how many different input methods every step (this might be especially problematic when you're using gamepad functions and no gamepad is plugged in, as that has led to issues in the past).

 

As for the other problem... see cdrdub8's reply.


  • 0

yBGBXQa.pngmWOQbeq.png


#6 Christovix

Christovix

    GMC Member

  • New Member
  • 11 posts
  • Version:GM:Studio

Posted 29 March 2016 - 05:01 AM

What you want to do is check a collision against a GROUP OF OBJECTS instead of checking

a collision with each of these objects seperatly, is that right?

 

If that's the case, then instead of using global.platform, create an object called obj_all_platforms ( leave it empty ).

Then, for all your objects ( so obj_reg_ground, obj_reg_breakablebox, obj_reg_moveablebox ) set their parent

to obj_all_platforms. ( it's under the sprite selection in the object editor )

 

Now, in your code, whennever you want to check a collision with all of those objects at the same time, you can use obj_all_platforms:

if ((hspd > 0) &&  (place_meeting(x, y+1, obj_all_platforms))) { sprite_index = spr_hank_run };

Hope this helps ~

- Cdrdub8

Thanks, this seems to work, as I stated above, this is essentially what I've done now. It's not ideal for me, but I can make it work!

 

 

Variables can only hold one value at a time. Assigning a value to a variable that already holds a value will overwrite it.

 

 

The way your control checks were initially set up technically works. You might want to consider just checking the one that is currently configured to be used in the game's options, though, so you won't be checking who knows how many different input methods every step (this might be especially problematic when you're using gamepad functions and no gamepad is plugged in, as that has led to issues in the past).

 

As for the other problem... see cdrdub8's reply.

So what I've decided to do is just give the player the choice, they can select WASD, Arrows, or Gamepad controls. The selection changes global.rightchoice, to equal the selection they've made (and automatically sets to gamepad without another selection, for now) and thus global.right = global.rightchoice (seen at the bottom of the code). The following code is my setup for that. 
I'm sure this is all fine code, but if for some reason I'm crazy and you can tell that this won't work, please let me know. Because this code is not working. Everything works when I put regular keyboard_checks in the physics code, but not when I replace them with the global.choice variables.

//////////////////////////////////////////////////////////////////////////////        
////////////////////////////CONTROLS//////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////


//player choice controls - get set to  main control variables by selecting and changing the 'choice' global variables.
                    //Move right
global.rightarrow       = keyboard_check(vk_right)
global.rightkeyboard    = keyboard_check(ord('D'))
global.rightgp          = gamepad_button_check(0, gp_padr)
global.rightchoice      = gamepad_button_check(0, gp_padr)
                    //move left
global.leftarrow        = keyboard_check(vk_left)
global.leftkeyboard     = keyboard_check(ord('A')) 
global.leftgp           = gamepad_button_check(0, gp_padl)
global.leftchoice       = gamepad_button_check(0, gp_padl)
                    //dpad up
global.uparrow          = keyboard_check(vk_up)
global.upkeyboard       = keyboard_check(ord('W'))
global.upgp             = gamepad_button_check(0, gp_padu)
global.upchoice         = gamepad_button_check(0, gp_padu)
                    //dpad down
global.downarrow        = keyboard_check(vk_down)
global.downkeyboard     = keyboard_check(ord('S'))     
global.downgp           = gamepad_button_check(0, gp_padd)
global.downchoice       = gamepad_button_check(0, gp_padd)
                    //jump
global.jumpkeyboard     = keyboard_check(ord('J'))
global.jumpgp           = gamepad_button_check(0, gp_face1)
global.jumpchoice       = gamepad_button_check(0, gp_face1)
                    //shoot
global.shootkeyboard    = keyboard_check_pressed(ord('I'))
global.shootgp          = gamepad_button_check_pressed(0, gp_face3)
global.shootchoice      = gamepad_button_check_pressed(0, gp_face3)
                    //pickup - case sensitive action
global.pickupkeyboard   = keyboard_check_pressed(ord('K'))
global.pickupgp         = gamepad_button_check_pressed(0, gp_face2)
global.pickupchoice     = gamepad_button_check_pressed(0, gp_face2)
                    //use selected item
global.itemusekeyboard  = keyboard_check_pressed(ord('L'))
global.itemusegp        = gamepad_button_check_pressed(0, gp_face4)
global.itemusechoice    = gamepad_button_check_pressed(0, gp_face4)
                    //start menu button
global.startkeyboard    = keyboard_check_pressed(vk_escape) 
global.startgp          = gamepad_button_check_pressed(0, gp_start) 
global.startchoice      = gamepad_button_check_pressed(0, gp_start) 


//MAIN CONTROL Variables
global.right    = global.rightchoice
global.left     = global.leftchoice
global.up       = global.upchoice
global.down     = global.downchoice
global.jump     = global.jumpchoice
global.shoot    = global.shootchoice
global.pickup   = global.pickupchoice
global.itemuse  = global.itemusechoice
global.start    = global.startchoice
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

 


  • 0