Using io.read() without halting the game

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
EliterScripts
Citizen
Posts: 85
Joined: Sat Oct 25, 2014 7:07 pm

Using io.read() without halting the game

Post by EliterScripts » Wed Dec 12, 2018 5:09 pm

Hello,
So I would like to use io.read() in my game, as it allows the user to input commands using the terminal approach. This is a great feature for my server edition of my game. However, I do not want to bring the entire game to a halt. I tried slapping io.read() on its own thread, and was planning on having a separate thread for printing console output.

However, my code is doing this strange thing where if I have 'io.read()' it will halt executing code before it, then I have to tap the 'enter' key a few times before it runs some code before it. Here is the input thread: https://gist.github.com/EliterScripts/5 ... bd02d8f6ab here's the output thread: https://gist.github.com/EliterScripts/c ... 173e717cb3 Here's what loads the output thread: https://gist.github.com/EliterScripts/2 ... be60dc1917 and here's what loads the input thread: https://gist.github.com/EliterScripts/f ... 32f42e3eda

and so the input thread is loaded (using the require function) before the output thread, and here is the kind of weird stuff this is doing:

"
depended: game/server/index
depended: game/server/commandLineInput
depended: game/server/commandLineOutput
enabling Fancy CLI features...


Fancy CLI features enabled.



"

The part where is says "depended...." is the part that is normal, and is code that is run beforehand. I had to hit "enter" before "Fancy CLI features enabled." was printed, which is weird.

User avatar
pgimeno
Party member
Posts: 1943
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia, ES

Re: Using io.read() without halting the game

Post by pgimeno » Wed Dec 12, 2018 9:48 pm

Works fine for me if I comment out these lines:

Code: Select all

        if(fancy == true)then
--            local handle = io.popen("tput lines")
--            local xSize = handle:read("*a")
--            handle:close()
--            handle = io.popen("tput cols")
--            local ySize = handle:read("*a")
--            handle:close()

            local lines = {}
        end
Don't ask me why.

Next time please provide a .love file that can reproduce the problem, rather than leaving that up to us. Help us help you.
Attachments
thread_problem.love
(2.9 KiB) Downloaded 75 times

User avatar
Nixola
Inner party member
Posts: 1940
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Using io.read() without halting the game

Post by Nixola » Mon Dec 24, 2018 4:33 pm

You can use a raw TCP socket if you want non-blocking keyboard input, useful if you don't want to create a new thread just to handle that. I can provide more details if you're interested, but you basically have to create a new TCP socket and set its file descriptor to 0; you can then use luasocket's select to "query" if it's got any input.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics

EliterScripts
Citizen
Posts: 85
Joined: Sat Oct 25, 2014 7:07 pm

Re: Using io.read() without halting the game

Post by EliterScripts » Thu Dec 27, 2018 10:42 pm

Nixola wrote:
Mon Dec 24, 2018 4:33 pm
You can use a raw TCP socket if you want non-blocking keyboard input, useful if you don't want to create a new thread just to handle that. I can provide more details if you're interested, but you basically have to create a new TCP socket and set its file descriptor to 0; you can then use luasocket's select to "query" if it's got any input.
I feel like this is such a hacky way of doing it, and it's just bound to go wrong. Not sure if having a separate thread for command line is the best idea either, but it seems to work well so far. The thing that sucks is that the command line interface sometimes keep going while the rest of the game is halted. Not sure if I should be worried or if that is a good thing.

I've also read somewhere that taking up a unix socket hogs up system resources, and that is why nginx performs better than Apache or something like that. Plus, I want to be able to slap the same code everywhere without needing to port code to every little stupid system, which I assume sockets are more of a Linux thing. Isn't there also a limit to how many ports a computer has? Like, isn't it like 16,000 or something?

Also, when I tested the game on an actual Ubuntu server (SSH'ing into it), I couldn't use control + C to terminate the application. Luckily, I was smart enough to create a "stop" command before throwing it on the server, but I don't like that control + C doesn't do the trick.

Post Reply

Who is online

Users browsing this forum: cazura and 19 guests