[R81] Keyboard "On Pressed" odd execution order

0 favourites
  • 5 posts
From the Asset Store
Total customisation of the input! You can combine inputs from all peripherals. Make your game accessible for everyone!
  • Note: This has been a problem for a while. R81 just indicates that it's still a problem in the current build.

    A couple others and myself have been playing with Construct 2 a bit and ran into a bit of an odd behavior with events that may or may not be classified as a bug... but regardless it's less than desirable for what we are trying to do.

    Suppose we have a sprite object with variables to track control in the following manner:

    sprite.up = 0

    sprite.up_pressed = 0

    And then we have an event at the beginning of the frame as follows:

    System.Every Tick
        sprite.set(up = 0)
        sprite.set(up_pressed = 0)
    Keyboard.Up arrow is down
        sprite.set(up = 1)
    Keyboard.Up arrow pressed
        sprite.set(up_pressed = 1)

    For some reason, in spite of the order of events dictating that if the up arrow is pressed that the value of up_pressed should be set to zero if the key is pressed, at the end of that code, the value is always 0 as though the System.Every Tick event occured afterwards. It's pretty troubling since it leaves me wondering whether I can trust the event order to follow logically with how I established it...

    Naturally, if I disable sprite.set(up_pressed = 0), then pressing up will set up_pressed to 1.

    I'm going to go ahead and link an example:



    Oh, I should amend this to mention that this only occurs with the pressed event conditions. With the key down conditions, the order of execution appears to perform as expected... which is visible in this demonstration.

    EDIT 3:

    Confirmed in Chrome 17.0.963.78, Firefox 10.0.2 and Internet Explorer 9.0.8112.16421

  • That's not a bug.

    That's the difference between a Trigger and a non-Trigger event.

    Trigger event (those with the green arrow on the left) are events outside the game loop. They are usually triggered before the next game loop to respond fast to the player input.

    The order of event is kept inside triggers though.

    if you do a

    Keyboard: On any key pressed
    	[li]> set Right_Pressed to 0[/li]
    [/ul]Keyboard: On Right arrow pressed
    	[li]> set Right_Pressed to 1[/li]
    [/ul]Keyboard: On any key pressed
    Right_Pressed = 1
    	[li]> Play sound[/li]
    	[li]> set Right_Pressed to 0

    The sound will play

    Keyboard: On any key pressed
    	[li]> set Right_Pressed to 0[/li]
    [/ul]Keyboard: On any key pressed
    Right_Pressed = 1
    	[li]> Play sound[/li]
    	[li]> set Right_Pressed to 0[/li]
    [/ul]Keyboard: On Right arrow pressed
    	[li]> set Right_Pressed to 1

    The sound won't be played

    Also notice that in your capx, if you put event 12 as the first event, you'll hear the sound.

    And for the number not displaying as you expect. The trigger is "On Up arrow pressed" that means that this event will play the instant you press the Up arrow, not before, not after. Then your Every tick that reset these variables will prevail.

  • Yann is right, it's by design - triggers aren't checked in the top-to-bottom running of the event sheet, they fire whenever the event happens, and most events happen before or after the event sheet runs. If you want a flag set to 1 for the one tick an event fires, make sure you clear the flag at the end of the event sheet. However I would recommend doing everything inside the trigger event if possible.

    I've updated the documentation on how events work to clarify this.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for the expedient response and useful information.

  • Congrats Ashley, I like the infos and the editing of the manual entry.


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