If you know another solution to create a lua function that returns the json for a given save id then I'm all ears. Expecting scripters with limited programming experience to handle asynchronous http requests in a time-restricted tick function seems more complicated than resuming a 6 line coroutine to me.
I know how coroutines work, I'm trying to explain things as simple as possible. It's rather obvious that the http requests aren't getting blocked when the coroutine is active (in the resumed state) and is blocking the caller, that's why this works.
evt.registerand register the
evt.TICKevent to check the status in there.
sorry for forgetting the :status(). ive forgot about it.
Ive tried doing it with event.register(event.tick, getjson(*save id*)), but it now says that i did not provide the save id lol.
Current code snippet:
function getjson(id) local httpreq = http.get("https://powdertoy.co.uk/Browse/View.json?ID="..id) if httpreq:status() == "done" then do return httpreq:finish() end evt.unregister(evt.tick, getjson()) end end
rawjson = event.register(evt.tick, getjson(1)) -- expected to get raw json of save 1 in var rawjson, got "bad argument #2 to 'register' (function expected, got no value)" instead
i dont get why does it think that i gave no value, though i did
local httpreq = nil
local function startgetjson(id)
httpreq = http.get("https://powdertoy.co.uk/Browse/View.json?ID="..id)
end
local function tick()
if httpreq:status() == "done" then
do something
end
end
event.register(evt.tick, tick)
No, this isn't easy because a tick function runs once every frame or 60x per second. This means:
To read the response you use :finish() on the request which changes its status to "dead".
The request will be handled only once.
Nothing will hang if your scripts uses too much time either. The game will prompt the user about an unresponsive script after a few seconds in the worst case. That wouldn't happen since the request gets handled once.
If you are really worried about that one if statement running every tick, you can unregister the tick function after you handled the request which isnt hard to do at all.
I'll hand you the dead status, but the tick function will definitely cause trouble if it takes longer than 1/60th of a second even if happens only once, because the next call to tick() will start before the present call to tick() is finished.
The entire game waits for lua to finish, so its not a problem