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

Legacy GM Switching between 2 values for a variable

R

Rax135

Guest
Hey everyone,

i wanted to know if there is a way to "toggle" between two values of a variable when i press a key. e.g.:

if key_pressed = true {
if state = true {
state = false;
}
if state = false {
state = true;
}
}

i tried it like the code above, it didnt work. switching the state to true works, but pressing again does not work.
 
I found a really simple way to alternate between true/false a while ago, just do
Code:
if (KEY PRESSED) {
    variable = !variable;
}
 

jo-thijs

Member
Just to make sure you understand why your code wasn't working, you forgot an else:
Code:
if key_pressed = true {
if state = true {
state = false;
} else
if state = false {
state = true;
}
}
Without the else, the first if executes, setting state to false, then the second if executes because state is false and it gets set back to true.
 

DanMunchie

Member
This is causing me massive problems...

Using variable = !variable, in the debugger, the value turns from either true or false to -blank-, and after saving/reloading (using ds_map_secure_save), they become <undefined>.

Any idea why this would be happening?

Quick Edit - Just tested the Beta channel and the problem disappears... so I'll leave this comment up for people on the "Stable" version.
 
Top