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.
How to access request_http_api() functions from other scripts than init?
- 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?
You need to pass it in to other files.
init.lua:
myscript.lua:
Files are functions in Lua. loadfile loads a file as a function. dofile is, in fact, implemented using loadfile like so:
init.lua:
Code: Select all
local http = minetest.request_http_api()
local path = minetest.get_modpath("mymod")
loadfile(path .. "/myscript.lua")(http)
Code: Select all
local http = ...
Code: Select all
function dofile(path)
local func = assert(loadfile(path))
return func()
end
Core Developer | Donate | Join the Matrix chat! | My Twitter | Mods | Minetest Modding Book
Hello profile reader
Hello profile reader
Re: How to access request_http_api() functions from other scripts than init?
I did not know you could do this:
Can you finish that line? Is it http since that is the variable you passed in the loadfile command?
... But then in my script.lua you have:rubenwardy wrote: ↑Wed Feb 10, 2021 13:21Code: Select all
... loadfile(path .. "/myscript.lua")(http)
Code: Select all
local http = ...
- 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?
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 = ...
Core Developer | Donate | Join the Matrix chat! | My Twitter | Mods | Minetest Modding Book
Hello profile reader
Hello profile reader
Who is online
Users browsing this forum: No registered users and 1 guest