0 Favourites

A working pause button?

  • I have a global variable called PlayState. I'm using this variable to switch between some of the various states of play.

    Now, pressing P during PlayState 1 (the main game) sets PlayState to 2, the pause screen.

    Likewise, pressing P during Playstate 2 (the pause screen) sets PlayState to 1, continuing the game.

    So I have it set up like this.

    System PlayState = 1

          Keyboard On P Pressed: System Set PlayState to 2.

    System PlayState = 2

          Keyboard On P Pressed: System Set PlayState to 1.

    However this doesn't work. It seems that both conditions end up being true within a single tick, so the game pauses and unpauses before the next frame is drawn. I know this because disabling the second condition causes the pause button to work (but of course it won't unpause).

    So how would I get this to work? I considered perhaps adding basic timer variable to force the pause screen to appear for at least a second, but I was wondering if there was a slightly neater way of fixing this.

  • I notice people tend to miss this. Code is an In ORder of operation. You must have a data of logic flow. You example actually is very clear on the error made.

    if(state = 1) then state = 2

    if(state = 2) then state = 1

    the Keyboard on pressed state will be TRUE for the entire tick and not just one event. So we can technically just view the two lines of code as always true.

    So let's re-examine those lines

    Layout start: state = 1;

    ----

    if(state = 1) then state = 2 // now the state is 2

    if(state = 2) then state = 1 // since the state was set to 2 in the last line. The condition is now true. Now state =1 is being set.

    so in the same tick of Pressed you evaulate both condition as true.

    Instead what you want is

    Event: If P is Pressed

    Sub Event: if state == 1 then state = 2

    ELSE

    sub Event: if state == 2 then state =1

    actually you can skip the if state==2 as the ELSE condition covers that.

    Also when you need to pause make sure to set yout timescale to 0 :)

  • oh. here is an alternative

    Event On P Pressed

    --Action: set state = (state == 1 ) ? 2 : 1

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • I actually didn't know there was an ELSE condition in Construct 2. That makes things so much easier. Thanks! :D

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)