Page 1 of 1

Infinite loop prevent keyboard scan

Posted: Tue Jan 10, 2023 6:43 pm
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?

Re: Infinite loop prevent keyboard scan

Posted: Tue Jan 10, 2023 9:09 pm
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 :|

Re: Infinite loop prevent keyboard scan

Posted: Tue Jan 10, 2023 9:19 pm
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.

Re: Infinite loop prevent keyboard scan

Posted: Wed Jan 11, 2023 12:28 pm
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.

Re: Infinite loop prevent keyboard scan

Posted: Wed Jan 11, 2023 12:30 pm
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.

Re: Infinite loop prevent keyboard scan

Posted: Wed Jan 11, 2023 2:39 pm
by BrotSagtMist
What? I just said its easy. Its adding 3 lines.

Re: Infinite loop prevent keyboard scan

Posted: Wed Jan 11, 2023 3:27 pm
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.

Re: Infinite loop prevent keyboard scan

Posted: Thu Jan 12, 2023 7:01 pm
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: