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

GML Using the same button to switch between things?

Dr_Nomz

Member
Okay, say you have a variable, and it has values of 0 and 1, and you want to switch between them at a moments notice. So you could bind two different keys to get the different results, but what about just one key?

Like if it were bound to Q and you hit Q, it would switch between them.
 

Dr_Nomz

Member
I have no idea how I'm supposed to use that, but the idea I had was this:
if equip_slot == 0{
equip_slot = 1;
}
if equip_slot == 1{
equip_slot = 0;
}

Thing is, if I do it like this, it won't do anything, because both actions cancel each other out.
 

samspade

Member
I have no idea how I'm supposed to use that, but the idea I had was this:
if equip_slot == 0{
equip_slot = 1;
}
if equip_slot == 1{
equip_slot = 0;
}

Thing is, if I do it like this, it won't do anything, because both actions cancel each other out.
equip_slot = !equip_slot is the way to go but your version would also work if you used else if rather than two separate conditionals.

Code:
if (action) {
    equip_slot = !equip_slot
}

//or

if (action) {
    if (equip_slot == 0) {
        equip_slot = 1;
    } else if (equip_slopt == 1) {
        equip_slot = 0;
    }
}

//or 

if (action) {
    if (equip_slot < 0.5) {
        equip_slot = 1;
    } else {
        equip_slot = 0;
    }
}
There are other ways as well. Again, the first version is the most common.
 
variable = !variable;
equip_slot = !equip_slot;
These ^

Thing is, if I do it like this, it won't do anything, because both actions cancel each other out.
Or if you do not want to do it the nice short way as has been suggested, then you really need to learn the "else" statement. If you were going to use your long-winded way of doing it, you would change your code to be:
Code:
if equip_slot == 0{
equip_slot = 1;
} else if equip_slot == 1{
equip_slot = 0;
}
or even
Code:
if equip_slot == 0{
equip_slot = 1;
} else {
equip_slot = 0;
}
Whichever way you go is up to you, but I would personally go for the single line approach as that is the simplest and most refined way to do things.

Edit: Ninja'd
 

spe

Member
Are you using the drag and drop instead of full GML coding? If so, I might know how you can do it...
1. Create a button press event for whatever button you want. Say, Q.
2. Drag a set variable block into the event.
3. Put the name of the variable in the variable box, and then in the value box, put the name of the same variable with an exclamation point in front. Like equip_slot in the first box, and !equip_slot in the second. Don't check the 'relative' box.
Basically the exclamation point will invert whatever your variable is, if it's a binary or boolean value. That basically means, if your variable is either a 1 or a 0, or it's a TRUE or a FALSE (both are the same in the underlying code) it'll change your variable to whatever it isn't. So 1 becomes 0, true becomes false.

Disclaimer: I never use drag and drop, so I'm not completely sure this is correct. If it isn't... just use a code block and copy/paste the code @Simon Gust posted. It does the exact same thing.
 

Dr_Nomz

Member
OKAY well, turns out that very piece of code by itself works exactly like what I want, can't believe it honestly. xD A little more explanation from the first two posters would've been nice, but I can see how it was so simple now.

But now that I do know I have to ask: HOW exactly does this work, and why? <_<

I mean from what I'm seeing it's only taking the variable, and saying it doesn't equal itself, but that makes no sense on it's own, and less so in this context.
 

Slyddar

Member
HOW exactly does this work, and why?
It's using boolean algebra. The ! means not, which means it changes it's boolean value. A boolean value can equal 0 or 1, and the not symbol flips it from one to the other, which means either your variable is enabled (1), or it's disabled (0).
 

samspade

Member
OKAY well, turns out that very piece of code by itself works exactly like what I want, can't believe it honestly. xD A little more explanation from the first two posters would've been nice, but I can see how it was so simple now.

But now that I do know I have to ask: HOW exactly does this work, and why? <_<

I mean from what I'm seeing it's only taking the variable, and saying it doesn't equal itself, but that makes no sense on it's own, and less so in this context.
In GML (and some other languages) true and false correspond to 0 and 1. And moreover GML can convert any number to true or false (0 or 1). I believe anything < 0.5 equals false, otherwise true. ! is the simple for not true. Applying ! essentially tells GM to treat the variable as a boolean or true false. Since 1 is > 0.5 (and already the GML value for true) GML returns false. So saying variable = !variable is the same as saying the variable should equal the opposite of its truth value.

That sounds a little complicated, but its because I'm trying to explain it with numbers, which works in GML because true and false are numbers. If you want to think of it as true or false it is easier. You're simply saying the variable should flip from true to false or vice versa.

Edit, someone with more experience can correct me if I'm wrong, but basically it is like writing the following:

Code:
variable = !(variable >= 0.5);
Of course for that to make sense you have to know that you can write 'conditional' statements like that so this is valid in GML as well:

Code:
variable = 5 < 0; 

variable = place_meeting(x, y, object1) || (place_meeting(x, y, object2);
In both cases variable will equal the result of that statement. False in the first case because five is not less than 0. And either false or true in the second case if either of the place meetings return true.
 

NeoShade

Member
There's been a couple of attempts at explaining this already, but I think they can be simplified a little by breaking it down:


Any number is either true or false.
Technically, if a number is less than 0.5 it is false and everything else is true, but it's easier to just think of it as: 1 = true, 0 = false.


The ! symbol means NOT.

True is not false, therefore true = !false.
Numberwise, 1 is not 0, therefore 1 = !0.


If you want to change any variable from true to false or vice versa, just set it to it's own NOT.

Code:
equip_slot = 0;  // Equip slot is set to 0 (or false).
equip_slot = !equip_slot; // Now equip slot is set to 1 (or true).
 
Top