SOLVED Why giving a same button two different jobs depending on a variable is not working?

Michoh

Member
Hello, I'm new around here and with Game Maker Studio 2. I don't know about coding, so I use DnD instead.

I want to create a character selection menu, so when you press spacebar to select a character, you can't move the selector, but I want you to press spacebar again to cancel and be free to select character again. To do so I give 1 or 0 to a variable depending on if you pressed spacebar to select or not. It works once, to restrain your movement and don't let you move, but if you press it again, does not work


(this is the event what occurs when you press spacebar)
GML:
// Si variable
if(jchsok2 == 0)
{


    // Asignar variable
    jchsok2 = 1;
}

// Si variable
if(jchsok2 == 1)
{
 

    // Asignar variable
    jchsok2 = 0;

}
If you press once, works, if you press twice, variable jchsok2 should be returned to it original value, but it's not working, I don't understand... What is not working?


If you could explain to me in DnD I'd be very grateful. Thanks and sorry for my poor english and zero knowledge about code and GMS2



Default value is 1, if I press once, it drops to 0, but pressing twice, does not get it to 1 again :T
 

gnysek

Member
Code is executed line after line, so after setting jchsok2 = 1;, because there's no return;no exit; and no else, it checks second "if", and it's true - as you just changed this variable to 1 two lines above.

Btw. jchsok2 = !jchsok2; is probably shortest way of doing it, but if+else or if+return+if isn't bad too.
 

Roldy

Member
In general, a program executes each line in series.

You can either step through this in your head, line by line, or use the debugger to see each line execute and the variables change.

Doing so will answer your question for you.
Using the debugger is a skill you should develop as it will improve your code and experience dramatically.
 

TailBit

Member
if jchsok2 is 0 .. then the first check will trigger .. and it will change it to 1 .. BUT .. the code doesn't stop .. and on will do the other check as well, and since it is 1 now then it is turned back into 0 .. on a single press

it can be solved by using else between them

Or by a single if else:
if(jchsok2 == 0) jchsok2 = 1 else jchsok2 = 0;

Or by toggeling the value instead:
jchsok2 = jchsok2 == 0;
jchsok2 = !jchsok2;
 

Michoh

Member
I understand now, but wich is the way in DnD to tell the program that it are two different options and do what I want? šŸ˜…

Sorry, I'm absolute useless at coding, I'd want to learn it in DnD
 

gnysek

Member
You need to add "else" (or "else if") block. I don't know how it looks cause I've used DnD last time decades ago, but it should be close to "if" :p

Edit: here's example image of if-else:
 

Michoh

Member
You need to add "else" block. I don't know how it looks cause I've used DnD last time decades ago, but it should be close to "if" :p
I was trying and it worked with the loop and break options,

Thanks everyone for helping me to understand! šŸ˜
 
Top