When does love.keypressed actually run?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

When does love.keypressed actually run?

Post by parallax7d » Sun May 03, 2015 5:56 pm

Lets say the player presses a key in the middle of the part of the game loop where love.update is running

Does the function defined in love.keypressed trigger right there and then, essentially 'interrupting' love.update?

Or is it queued up and run during the next tick during the love.event part of love.run? Or something else? I'm confused. :huh:

User avatar
slime
Solid Snayke
Posts: 2853
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: When does love.keypressed actually run?

Post by slime » Sun May 03, 2015 6:32 pm

parallax7d wrote:Or is it queued up and run during the next tick during the love.event part of love.run? Or something else? I'm confused. :huh:
Yep. All input / system events are processed at the beginning of every frame, before love.update is executed.
The love.event.pump() call in [wiki]love.run[/wiki] adds the events to LÖVE's event queue, and then the love.event.poll() call processes each pending event, and sends it to the love handlers (which call love.keypressed, love.mousepressed, etc. based on the event.)

User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

Re: When does love.keypressed actually run?

Post by parallax7d » Mon May 04, 2015 4:16 am

What would happen while a user enters text into a text box? Would love.keypressed trigger for each letter typed?

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: When does love.keypressed actually run?

Post by Robin » Mon May 04, 2015 9:38 am

Yes, and for any other keys you press. Have you tried it out. (Also, for a text box, you'd probably want to use [wiki]love.textinput[/wiki] for the actual text input.)
Help us help you: attach a .love.

User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

Re: When does love.keypressed actually run?

Post by parallax7d » Mon May 04, 2015 4:18 pm

Lets say the game loop is in the middle of love.update and the user presses 'a'. Does keypressed actually capture 'a' at that point - or does it simply add an event for the next tick to capture any keyboard input if a key happens to still be pressed during the actual execution of keypressed?

If it does capture 'a' immediately, is it possible for it to capture other info? Like the id of the entity that's currently selected? Or can that sort of info only be captured when keypressed is actually run the next tick?

I'm just thinking about user input being missed, or a key's action being applied to the wrong entity, if there is some sort of delay between user input and execution.

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: When does love.keypressed actually run?

Post by Robin » Mon May 04, 2015 4:23 pm

parallax7d wrote:Lets say the game loop is in the middle of love.update and the user presses 'a'. Does keypressed actually capture 'a' at that point - or does it simply add an event for the next tick to capture any keyboard input if a key happens to still be pressed during the actual execution of keypressed?
The latter.
parallax7d wrote:I'm just thinking about user input being missed, or a key's action being applied to the wrong entity, if there is some sort of delay between user input and execution.
The first is not a problem, as user input is queued up. The second problem is unlikely, since with a moderate framerate, each tick only lasts a fraction of a second, so it won't have a noticable delay.

Note that this is how pretty much all GUI systems work, as far as I'm aware: the whole event queue comes pretty much directly from the operating system, so if other programs are fast enough for this kind of stuff, your game will be too.
Help us help you: attach a .love.

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: When does love.keypressed actually run?

Post by bartbes » Mon May 04, 2015 8:23 pm

parallax7d wrote:I'm just thinking about user input being missed, or a key's action being applied to the wrong entity, if there is some sort of delay between user input and execution.
How would the other entity be selected? Would that not also use an input event?

User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

Re: When does love.keypressed actually run?

Post by parallax7d » Mon May 04, 2015 10:36 pm

Mouse clicks and key presses can be very quick, especially while typing or double clicking;

loop n
during love.update, user presses 'x'
event knows something happened
during love.draw, user lets go of 'x'
loop n+1
event triggers, captures whatever key is currently pressed (none) -- the user's input is essentially lost

Edit: ignore this second part - I get what bartbes was saying

As far as a command going to the wrong entity;

loop n
user clicks on entity1
loop n+1
during love.update, user presses 'x'
event knows something happened
user selects entity2 with mouse
loop n+2
keypressed is triggered, grabs whatever key is currently pressed (still 'x'), applies that action to the current entity (entity2)

Perhaps it would make more sense to apply the current triggered input action onto the last entity selected, if the currently selected entity changed last frame? The edge case bugs that would cause is making me :cry:


Either I'm not getting how this works, or I'm being overly paranoid. It's just that it realllly annoys me when playing a game and the keypress is lost. I remember in AOE2 rapidly trying to delete walls, and half your delete key presses would get lost! I want to do everything I can to avoid that sort of thing, even if it only applies to 1 in 1,000 inputs.
Last edited by parallax7d on Mon May 04, 2015 11:45 pm, edited 2 times in total.

User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: When does love.keypressed actually run?

Post by s-ol » Mon May 04, 2015 11:31 pm

parallax7d wrote: Either I'm not getting how this works, or I'm being overly paranoid.
both.

Whenever the user does something, an event is sent into the queue. At the start of every loop, the whole queue is applied to the callbacks in order; That means as far as love.keypressed and love.mouseclicked are concerned, you can pretend the events are completely real-time, you cannot select entity A, hit delete and select entity B before A gets deleted and mess up the input.

The only thing that could happen is your deletion happening in love.update. love.update and love.keyboard.isDown always query the current state, that means what happened in this frame's input. This means that if the use somehow manages to press E and R in the same frame, there is no way of telling which came first from within love.update alone.

I just ran a quick test and when I type as fast as I absolutely can there are still a minimum of 5 frames between letters (for hello). I can't manage to type the same key twice in under 7 frames.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end

User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

Re: When does love.keypressed actually run?

Post by parallax7d » Mon May 04, 2015 11:47 pm

S0lll0s wrote:
Whenever the user does something, an event is sent into the queue. At the start of every loop, the whole queue is applied to the callbacks in order; That means as far as love.keypressed and love.mouseclicked are concerned, you can pretend the events are completely real-time, you cannot select entity A, hit delete and select entity B before A gets deleted and mess up the input.
cool, I got that now, thanks for elaborating
S0lll0s wrote: I just ran a quick test and when I type as fast as I absolutely can there are still a minimum of 5 frames between letters (for hello). I can't manage to type the same key twice in under 7 frames.
Well, that depends on the time between frames :3

But yeah, I guess maybe I am being too paranoid about lost input.

I figure the fastest APM in the world is 818 = 13.6 actions per second. I guess as long as the fps of the game stay above double that, the chances for a lost command are tiny. It's just so annoying when it does happen!
Last edited by parallax7d on Mon May 04, 2015 11:54 pm, edited 1 time in total.

Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests