View changing not working

C

captpman

Guest
I feel like such an idiot, but I don't know what to do. Someone please help.

So I set up an object that's supposed to change between views when the space bar is pressed. I created a step event and put in an execute code action. After digging through a few pages of the manual, I created this bit of code:

Code:
if(keyboard_check_pressed(vk_space)){
    if(view_visible[0]){
        view_visible[1]=true;
        view_visible[0]=false;
    }
    if(view_visible[1]){
        view_visible[0]=true;
        view_visible[1]=false;
    }
}
I placed the object in the room and began running the game. It didn't work. As far as I could tell, when I pressed the space key it wasn't working, and view[0] stayed visible while view[1] didn't. What am I supposed to do?
 
P

ParodyKnaveBob

Guest
Howdy, captpman,

The good news is, it works. The bad news is, it doesn't work as you intend. $;^ ] Here, lemme show you. Let's say you start with View 0 being visible, okay? And then you press the Spacebar, right? All right, this is what happens:
Code:
if(keyboard_check_pressed(vk_space)){ // TRUE! keep going
    if(view_visible[0]){ // TRUE! keep going
        view_visible[1]=true; // now set View 1 to true
        view_visible[0]=false; // and now set View 0 to false
    }
    // with View 1 now set to true and View 0 now set to false...
    if(view_visible[1]){ // TRUE! keep going
        view_visible[0]=true; // now set View 0 back to true
        view_visible[1]=false; // now set View 1 back to false
    }
}
Uh-oh. See the problem now? $:^ b There are a couple solutions here. One is, if you intend to have more than just these two views going on, then you might just want to add an else statement. Like so:
Code:
if (keyboard_check_pressed(vk_space)) {
  if (view_visible[0]) {
    view_visible[1] = true;
    view_visible[0] = false;
  } else {
    view_visible[0] = true;
    view_visible[1] = false;
  }
}
...or similarly (again, depending how much setup you intend,)...
Code:
if (keyboard_check_pressed(vk_space)) {
  if (view_visible[0]) {
    view_visible[1] = true;
    view_visible[0] = false;
  } else if (view_visible[1]) {
    view_visible[0] = true;
    view_visible[1] = false;
  }
}
However, I suspect you'll only be toggling between these two views, eh? Therefore, you can probably remove a lot of that redundant code. Like so:
Code:
if (keyboard_check_pressed(vk_space)) {
    view_visible[0] = !view_visible[0];
    view_visible[1] = !view_visible[1];
}
Furthermore, you didn't tell what event you placed this, but I have a feeling you dumped it into the Step Event like many others do. Let's get rid of yet another bit of GML and use the event system even more (as my suggestion anyway):
Code:
// in Keyboard Pressed <Spacebar> Event
view_visible[0] = !view_visible[0];
view_visible[1] = !view_visible[1];
Voila. $:^ ] (I could say more about the "magic numbers" (nondescript literals) of 0 and 1 vs. using constants/enums/something which self-explain, but this is already a whole bunch thrown at you all at once for one simple answer as it is.)

I hope this helps, $:^ J
 
C

captpman

Guest
Oh my God, I am such an idiot. I promise I'll reimburse you for any brain cells lost as a result of exposure to my stupidity!

I actually didn't know that you could do the whole
Code:
booleanVariable=!booleanVariable;
thing to set a boolean variable to the opposite of the true/false value it holds. Well frick, if only I'd known that earlier this whole problem would never have happened!

All jokes aside, thank you for your help. Now I can move forward and hopefully use my new knowledge of GML in more, useful situations!
 
Last edited by a moderator:
P

ParodyKnaveBob

Guest
I'm just Bob, captpman -- not God. Nevertheless, you're quite welcome, and yeah, toggling Booleans (and toggling positive/negative plus all kinds of nifty things) is one of those awesome, eye-opening tricks when any of us first sees it. $E^ D
 
Top