How to access request_http_api() functions from other scripts than init?

Post Reply
mpathy
New member
Posts: 1
Joined: Wed Feb 10, 2021 11:41

How to access request_http_api() functions from other scripts than init?

by mpathy » Post

Hi there!

I have extended a existing mod plugin (classroom) to provide additional functionality.
Most of the functionality is in another program on another server and will be called over an REST API call.

Now I added the buttons and wanted to do the http requests, but it doesnt work.

I already found out that it can be only called in init.lua - but how can I access it from there?
When I define a function I cant use it in another lua script in my mod.

User avatar
rubenwardy
Moderator
Posts: 6431
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: United Kingdom
Contact:

Re: How to access request_http_api() functions from other scripts than init?

by rubenwardy » Post

You need to pass it in to other files.

init.lua:

Code: Select all

local http = minetest.request_http_api()

local path = minetest.get_modpath("mymod")
loadfile(path .. "/myscript.lua")(http)
myscript.lua:

Code: Select all

local http = ...
Files are functions in Lua. loadfile loads a file as a function. dofile is, in fact, implemented using loadfile like so:

Code: Select all

function dofile(path)
	local func = assert(loadfile(path))
	return func()
end

Badkraft
Member
Posts: 28
Joined: Wed Jan 27, 2021 13:44

Re: How to access request_http_api() functions from other scripts than init?

by Badkraft » Post

I did not know you could do this:
rubenwardy wrote:
Wed Feb 10, 2021 13:21

Code: Select all

...
loadfile(path .. "/myscript.lua")(http)
... But then in my script.lua you have:

Code: Select all

local http = ...
Can you finish that line? Is it http since that is the variable you passed in the loadfile command?

User avatar
rubenwardy
Moderator
Posts: 6431
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: United Kingdom
Contact:

Re: How to access request_http_api() functions from other scripts than init?

by rubenwardy » Post

Badkraft wrote:
Sun Feb 14, 2021 10:46
Can you finish that line? Is it http since that is the variable you passed in the loadfile command?
That is the finished line. ... is used to capture variable arguments as a tuple. You can also use arg, which captures variable arguments as a table.

For example, if you passed in two arguments you could do:

Code: Select all

local one, two = ...
Some info is here, although it doesn't mention using ... explicitly: https://www.lua.org/pil/5.2.html

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest