Call http.request() from lua module (SOLVED)

I tried to call http.request() from a lua module, it seems not working and there is a debug warning says:

WARNING:SCRIPT: Failed to return http-response. Requester deleted?

The codes are from the http API reference example, and work well in game object scripts. Are there some limitations in lua modules to call the builtin APIs? I might see only message APIs can work across game object scripts and GUI scripts, but I cannot recall the detail, I really appreciate if anyone can update more detail about the behaviors and under the hood.:smiley:

This happens when the game object that made the http.request is deleted before the http response has been received. Remember that even though you have the http.request function in a Lua module, it’s still invoked from a script component attached to a game object.

Actually, I required the module outside of any of “member” functions of the script component, and do the http.request in the initalizing codes instead of a function. So I am a bit confused about the lifecycle since the lua module can be required by multiple script component.

You mean in the init() function of a script or outside the init function?

What matters is from which script the function in the Lua module is called. The callback of http.request will be assigned the Lua scope/context of the script from which it is called. And if that script is deleted before the callback is invoked you’ll get the error in your original post.

emm, it’s odd. I’m pretty sure the game object is not deleted because it is the only game object in my collection… I’ll check it again, thanks @britzl !

Outside the init function

Hmm, ok, then it could be a problem that you’re calling the http.request function not from a lifecycle function.

1 Like

I required my module in script component init() function, and the http.request in my test codes works well, thank you @britzl ! :grinning: