Can you make love not crash when waiting for input?

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
User avatar
dr.flimflam9
Prole
Posts: 3
Joined: Fri Oct 18, 2024 10:53 am

Can you make love not crash when waiting for input?

Post 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?
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: Can you make love not crash when waiting for input?

Post 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?
User avatar
dr.flimflam9
Prole
Posts: 3
Joined: Fri Oct 18, 2024 10:53 am

Re: Can you make love not crash when waiting for input?

Post 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.
User avatar
dusoft
Party member
Posts: 654
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Can you make love not crash when waiting for input?

Post 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.
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: Can you make love not crash when waiting for input?

Post 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.
User avatar
dr.flimflam9
Prole
Posts: 3
Joined: Fri Oct 18, 2024 10:53 am

Re: Can you make love not crash when waiting for input?

Post 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.
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: Can you make love not crash when waiting for input?

Post 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.
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: Can you make love not crash when waiting for input?

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests