Jump to content


Photo

Broken variables?


  • Please log in to reply
9 replies to this topic

#1 CorruptionEX

CorruptionEX

    The Wistful Shadow

  • New Member
  • 52 posts
  • Version:GM8

Posted 18 March 2012 - 07:05 PM

alright tell me whats wrong with this code...

//Game Controller Main

//Menu Key
if keyboard_check(81)
{
    if global.Paused = false
    {
        global.Paused = true
    }
    if global.Paused = true
    {
        global.Paused = false
    }
    Menu(global.Paused)
}

The Menu() script will have my entire pause menu script, but it wont do anything if the global.Paused variable is false.

For some reason, that variable never changes from false...
no other objects or scripts reference that variable except for this controller, and the menu script.

When i run the game in debug mode, the variable stays at 0
shouldn't it be false?

im just frustrated.... :/
  • 0

#2 thegame

thegame

    Flying Penguin

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

Posted 18 March 2012 - 07:09 PM

0 = false. 1 = true.
false and true are built in constants that we see one way and a computer reads another.
  • 0

#3 CorruptionEX

CorruptionEX

    The Wistful Shadow

  • New Member
  • 52 posts
  • Version:GM8

Posted 18 March 2012 - 07:10 PM

ok, but do you know why its not changeing? i dont work with true/false variable much, so im not even sure if thats how i set them...
  • 0

#4 thegame

thegame

    Flying Penguin

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

Posted 18 March 2012 - 07:15 PM

That is how you set them, same as any variable. For your purpose though, it would work like this:
if(keyboard_check_pressed(ord("Q"))) {
    global.Paused = !global.Paused;
    Menu(global.Paused);
}
Also, are you sure you want Q for your pause button? Usually it's P...
As for why it's not changing, it may be because you do want it to be P but you used Q by accident... other than that I don't know

Edited by thegame, 18 March 2012 - 07:15 PM.

  • 0

#5 CorruptionEX

CorruptionEX

    The Wistful Shadow

  • New Member
  • 52 posts
  • Version:GM8

Posted 18 March 2012 - 07:16 PM

That is how you set them, same as any variable. For your purpose though, I think you should probably do it like this though:

if(keyboard_check_pressed(ord("Q"))) {
    global.Paused = !global.Paused;
    Menu(global.Paused);
}
Also, are you sure you want Q for your pause button? Usually it's P...
As for why it's not changing, it may be because you do want it to be P but you used Q by accident... other than that I don't know


thank you, ima go try this now
and no, i wanted Q because i wanted this to be the menu key, an have the player inventory as well as a few other things appear. its all defined in the Menu() script
  • 0

#6 Figuras

Figuras

    GMC Member

  • New Member
  • 51 posts
  • Version:GM8

Posted 18 March 2012 - 07:16 PM

Thats just the old mistake every new programmer will make (and advanced ones too :D).
You say:
if false set true
if true set false
So it will set it true because it's false, and then immediately set it false again, because it's true.

You have to stop the script after it has checked ONE of these conditions. You can do it with exit.

if keyboard_check(81)
{
    Menu(global.Paused)

    if global.Paused = false
    {
        global.Paused = true
        exit
    }
    if global.Paused = true
    {
        global.Paused = false
        exit
    }
}

  • 0

#7 thegame

thegame

    Flying Penguin

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

Posted 18 March 2012 - 07:19 PM

Thats just the old mistake every new programmer will make (and advanced ones too :D).
You say:
if false set true
if true set false
So it will set it true because it's false, and then immediately set it false again, because it's true.

You have to stop the script after it has checked ONE of these conditions. You can do it with exit.

if keyboard_check(81)
{
    Menu(global.Paused)

    if global.Paused = false
    {
        global.Paused = true
        exit
    }
    if global.Paused = true
    {
        global.Paused = false
        exit
    }
}

OH yes, that's it... though this way won't work either... now it's doing Menu() with the wrong value of global.Paused... :P
There's 2 ways around that:
1. Use the opposite value of global.Paused
if keyboard_check(81)
{
    Menu(!global.Paused)

    if global.Paused = false
    {
        global.Paused = true
        exit
    }
    if global.Paused = true
    {
        global.Paused = false
        exit
    }
}
2. Use if () {} else if () {}
if keyboard_check(81)
{
    if global.Paused = false
    {
        global.Paused = true
    } else if global.Paused = true
    {
        global.Paused = false
    }
    Menu(global.Paused)
}

  • 0

#8 CorruptionEX

CorruptionEX

    The Wistful Shadow

  • New Member
  • 52 posts
  • Version:GM8

Posted 18 March 2012 - 07:21 PM

That is how you set them, same as any variable. For your purpose though, it would work like this:

if(keyboard_check_pressed(ord("Q"))) {
    global.Paused = !global.Paused;
    Menu(global.Paused);
}
Also, are you sure you want Q for your pause button? Usually it's P...
As for why it's not changing, it may be because you do want it to be P but you used Q by accident... other than that I don't know


Thank you! it worked. I just didnt think of inverting the value...
  • 0

#9 Figuras

Figuras

    GMC Member

  • New Member
  • 51 posts
  • Version:GM8

Posted 18 March 2012 - 07:22 PM

Oh right. I didn't take a closer look on what the line Menu(global.Paused) actually does. ^^
  • 0

#10 CorruptionEX

CorruptionEX

    The Wistful Shadow

  • New Member
  • 52 posts
  • Version:GM8

Posted 18 March 2012 - 07:23 PM

Thank you both! Not only do I have it working, I know WHY it works =D

Topic can be closed now...

(Cya in the credits thegame!)
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users