Infinite loop prevent keyboard scan

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.
Post Reply
gotzmann
Prole
Posts: 4
Joined: Tue Jan 10, 2023 6:36 pm

Infinite loop prevent keyboard scan

Post by gotzmann »

I'm trying to find the way not to block love.keypressed() callback having this loop in main thread of PICO-8 emulator:

Code: Select all

while(true) do
     if (btn(❎)) then
         sfx(30)
         return
     end
 end
Not sure, is it possible to have a separate thread scanning keyboard / controller and filling all data for pressed keys in the background, if the main thread have such infinite loop?
User avatar
Sasha264
Party member
Posts: 131
Joined: Mon Sep 08, 2014 7:57 am

Re: Infinite loop prevent keyboard scan

Post by Sasha264 »

Hi and welcome! :3
Love already have some kind of internal main loop inside https://love2d.org/wiki/love.run, which is hidden by default, but is still here. And that loop is carefully developed and already have important things (keyboard reaction included) embedded. So it looks like a bad idea to add some another infinite loop in your game. At least not in 95% of cases. That PICO-8 emulator did that :o: ?

First idea is to check love.keyboard.isDown(...) inside that loop. But it looks like you are not looking for this?

About separate thread scanning keyboard: Love have some multi-thread support https://love2d.org/wiki/love.thread, but it is rather poor. And the wiki says that:
...love.keyboard, love.mouse, and love.touch modules have several restrictions and therefore can only be used in the main thread.
So, maybe there are some ways to do that, but they will be from "hack" category :|
Last edited by Sasha264 on Tue Jan 10, 2023 9:19 pm, edited 1 time in total.
User avatar
BrotSagtMist
Party member
Posts: 657
Joined: Fri Aug 06, 2021 10:30 pm

Re: Infinite loop prevent keyboard scan

Post by BrotSagtMist »

You will need to put keyboard scanning inside this loop on your own.
see love.run
essentially you just need to copy the event pump and call parts.
obey
gotzmann
Prole
Posts: 4
Joined: Tue Jan 10, 2023 6:36 pm

Re: Infinite loop prevent keyboard scan

Post by gotzmann »

Sasha264 wrote: Tue Jan 10, 2023 9:09 pm About separate thread scanning keyboard: Love have some multi-thread support https://love2d.org/wiki/love.thread, but it is rather poor. And the wiki says that:
...love.keyboard, love.mouse, and love.touch modules have several restrictions and therefore can only be used in the main thread.
So, maybe there are some ways to do that, but they will be from "hack" category :|
Oh, thanks for that! I've expected something like this.
Love already have some kind of internal main loop inside https://love2d.org/wiki/love.run, which is hidden by default, but is still here. And that loop is carefully developed and already have important things (keyboard reaction included) embedded. So it looks like a bad idea to add some another infinite loop in your game. At least not in 95% of cases. That PICO-8 emulator did that :o: ?
I understand that Love and Picolove (PICO8 emulator) itself are great products with thought out architectures where infinite loop in main game thread just not expected and never should be used. So maybe there just one way to fix the problem - is to fix the PICO game itself :)

But the thing is - real PICO8 fantasy console has no such problem and infinite loop in main game thread have no any bad consequences on the
game itself - keyboard are scanned properly and after user pressing the key, the infinite loop is ended.

I'll go to read more about multi threading in Love2D, but as quoted earlier, it seems, it doesn't help me with keyboard scan issues anyway.
gotzmann
Prole
Posts: 4
Joined: Tue Jan 10, 2023 6:36 pm

Re: Infinite loop prevent keyboard scan

Post by gotzmann »

BrotSagtMist wrote: Tue Jan 10, 2023 9:19 pm You will need to put keyboard scanning inside this loop on your own.
see love.run
essentially you just need to copy the event pump and call parts.
Yep! I've had high hopes it's possible to fix Love2D or emulator to avoid fixing the games ("the cartridges") itself, but do not see the easy way to achieve that now.
User avatar
BrotSagtMist
Party member
Posts: 657
Joined: Fri Aug 06, 2021 10:30 pm

Re: Infinite loop prevent keyboard scan

Post by BrotSagtMist »

What? I just said its easy. Its adding 3 lines.
obey
gotzmann
Prole
Posts: 4
Joined: Tue Jan 10, 2023 6:36 pm

Re: Infinite loop prevent keyboard scan

Post by gotzmann »

BrotSagtMist wrote: Wed Jan 11, 2023 2:39 pm What? I just said its easy. Its adding 3 lines.
I might fix my local game code easy peasy.

But I can't fix the original games which will be downloaded by the users of emulator from other locations.

My main goal is to fix emulator, not the game itself.
User avatar
Sasha264
Party member
Posts: 131
Joined: Mon Sep 08, 2014 7:57 am

Re: Infinite loop prevent keyboard scan

Post by Sasha264 »

My main goal is to fix emulator, not the game itself.
Absolutely!

Bad, bad, bad idea:
Before running the game poke up some replacements in its original code string (programmatically, with regex or something). And replace btn(<green-square>) with some function that actually checks if that key is currently down :rofl:
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests