Page 1 of 1
Can you make love not crash when waiting for input?
Posted: Fri Oct 18, 2024 10:58 am
by dr.flimflam9
I'm making a multiplayer game, and I have a call function that sends a message to the server, and then waits for a response (Because of udp:settimeout(1)).
Code: Select all
function call(c)
udp:send(c)
local val = udp:receive()
return val
end
Is there a way to make love not crash while waiting, but just freeze?
Re: Can you make love not crash when waiting for input?
Posted: Fri Oct 18, 2024 12:40 pm
by pgimeno
Strange. The `receive` docs of the `socket` library say it should return nil:
In case of success, the method returns the received datagram. In case of timeout, the method returns nil followed by the string 'timeout'.
https://w3.impa.br/~diego/software/luas ... ml#receive
yet the `settimeout` docs say it will fail (though it's not clear what it means by "fail"):
When a timeout is set and the specified amount of time has elapsed, the affected methods give up and fail with an error code.
https://w3.impa.br/~diego/software/luas ... settimeout
Anyway, you can always use pcall():
Code: Select all
function call(c)
udp:send(c)
local ok, val = pcall(udp.receive, udp)
if ok then
return val
end
return nil, val
end
Edit: Wait, "just freeze"? Why use settimeout() then?
Re: Can you make love not crash when waiting for input?
Posted: Fri Oct 18, 2024 7:34 pm
by dr.flimflam9
when i turn on timeout the window just crashes, not errors. I want to make it so that the program will only proceed after receiving the response without the game crashing.
Re: Can you make love not crash when waiting for input?
Posted: Fri Oct 18, 2024 8:07 pm
by dusoft
dr.flimflam9 wrote: ↑Fri Oct 18, 2024 7:34 pm
when i turn on timeout the window just crashes, not errors. I want to make it so that the program will only proceed after receiving the response without the game crashing.
Is it possible to do it async here? Maybe launching a second thread:
https://love2d.org/wiki/love.thread
And then just check in love.update(), whether there was a response.
Re: Can you make love not crash when waiting for input?
Posted: Fri Oct 18, 2024 8:20 pm
by pgimeno
dr.flimflam9 wrote: ↑Fri Oct 18, 2024 7:34 pm
when i turn on timeout the window just crashes, not errors. I want to make it so that the program will only proceed after receiving the response without the game crashing.
I don't understand. Then again why turn on timeout? By default receive() will block indefinitely until it receives something, according to the docs.
Re: Can you make love not crash when waiting for input?
Posted: Fri Oct 18, 2024 8:42 pm
by dr.flimflam9
pgimeno wrote: ↑Fri Oct 18, 2024 8:20 pm
dr.flimflam9 wrote: ↑Fri Oct 18, 2024 7:34 pm
when i turn on timeout the window just crashes, not errors. I want to make it so that the program will only proceed after receiving the response without the game crashing.
I don't understand. Then again why turn on timeout? By default receive() will block indefinitely until it receives something, according to the docs.
I know, but then the game doesn't respond and crashes. the usage of settimeout is just so i remember it is enabled, even if it is by default.
Re: Can you make love not crash when waiting for input?
Posted: Sat Oct 19, 2024 11:11 am
by pgimeno
Oh I think what you mean by "crashes" is that it freezes (stops responding). Normally the word "crash" means that the application gets closed, with or without an error message.
Then there are a few possibilities.
- You can use a separate thread as dusoft has mentioned, have the thread send the response through a channel, and check in the main thread whether the channel has data.
- By having a very low timeout, or none at all, instead of a thread you can use either the main loop of Löve, or a coroutine.
Re: Can you make love not crash when waiting for input?
Posted: Sat Oct 19, 2024 11:42 am
by RNavega
dr.flimflam9 wrote: ↑Fri Oct 18, 2024 10:58 am
Is there a way to make love not crash while waiting, but just freeze?
I want to make it so that the program will only proceed after receiving the response without the game crashing.
From what pgimeno read in the docs, even if the request timed out, it should return the pair (nil, "timeout"), not outright crash back to the desktop.
There's something strange going on.
If you use pcall like he showed, what error message comes back? When pcall succeeds (variable "ok" from his example is true), "val" is the first result of the function. When pcall fails (variable "ok" will hold false), then "val" will hold the error message that can be printed onto the debug console or the screen.