[R81] Keyboard "On Pressed" odd execution order

Bugs will be moved here once resolved.

Post » Tue Mar 13, 2012 12:55 am

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:
[code]
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)
[/code]

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:
http://www.dimensionwarped.sonicfangameshq.com/remote/Keyboard_Malfunction.capx

EDIT:
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.16421DimensionWarped2012-03-13 01:10:53
B
4
Posts: 17
Reputation: 484

Post » Tue Mar 13, 2012 1:02 pm

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
[code]Keyboard: On any key pressed
-> set Right_Pressed to 0
Keyboard: On Right arrow pressed
-> set Right_Pressed to 1
Keyboard: On any key pressed
Right_Pressed = 1
-> Play sound
-> set Right_Pressed to 0[/code]The sound will play

[code]Keyboard: On any key pressed
-> set Right_Pressed to 0
Keyboard: On any key pressed
Right_Pressed = 1
-> Play sound
-> set Right_Pressed to 0
Keyboard: On Right arrow pressed
-> set Right_Pressed to 1[/code]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.Yann2012-03-13 13:06:13
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Tue Mar 13, 2012 4:00 pm

@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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Tue Mar 13, 2012 10:20 pm

Thanks for the expedient response and useful information.
B
4
Posts: 17
Reputation: 484

Post » Thu Mar 15, 2012 8:07 am

Congrats @Ashley, I like the infos and the editing of the manual entry.
Thanks.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 7,000
Reputation: 57,795


Return to Closed bugs

Who is online

Users browsing this forum: No registered users and 5 guests