How to call multiple functions in an if() statements in c#

1.2k views Asked by At

So I need a c# if() statement which should look something like this, maybe? Code:

...
if(CurrentGameState != gameState.gameLoading && gameState.mainMenu && keyboardState.IsKeyDown(Keys.Escape))
{
      CurrentGameState = gameState.mainmenu;

}
//I get error in: CurrentGameState != gameState.gameLoading && gameState.mainMenu part
...

And that's the code I have used the variables above from:

    //Game States
    enum gameState
    {
        gameLoading,
        mainMenu,
        gameOptions,
        levelSelect,
        gamePlaying,
        gameOver
    }
    gameState CurrentGameState = gameState.gameLoading;
    //Keyboard State
    KeyboardState keyboardState = Keyboard.GetState();

Any suggestions?

3

There are 3 answers

0
Tim S. On BEST ANSWER

gameState.mainMenu is an enum value, not a boolean value. You can't use it the way you're trying to. Did you mean to do this?

if (CurrentGameState != gameState.gameLoading && 
    CurrentGameState != gameState.mainMenu &&
    keyboardState.IsKeyDown(Keys.Escape))
0
bansi On
if(CurrentGameState != gameState.gameLoading 
   && gameState.mainMenu && keyboardState.IsKeyDown(Keys.Escape))

may be

if(CurrentGameState != gameState.gameLoading && 
   CurrentGameState==gameState.mainMenu && keyboardState.IsKeyDown(Keys.Escape))
0
Sayse On

I appologise for formatting but writing from my phone, but I presume your working with xna, you should have your game states in a switch statement otherwise your code will get messy very fast as well as inefficient

 Switch(currentgamestate)
 {
      Case gamestate.loading:
          If(pressing escape)
              Currentgamestate = gamestate.mainmenu;
       Break;
       Case gamestate.mainmenu;
            //todo
        Break;
   }

Your error is because you are trying to link two conditions together ..if currentgamestate isnt loading and is main menu, you need to reference current gamestate for each condition.. if currentgamestate isn't loading and currentgamestate is mainmenu