Page 1 of 1

[solved] How to create a private mod namespace

PostPosted: Sun Jul 03, 2016 12:17
by addi
How do I make http = minetest.request_http_api() make only usable by my mod?

If I use local http = minetest.request_http_api() I can use it in the init.lua file, but not in a file executed by dofile(). :-(

If I use mymod = {}; mymod.http = minetest.request_http_api(), I can use it in init.lua any file used by dofile, but it can also executed by any other mod. :-(

Is there a way to make a variable private, so that they can used by all files of the mod, but is not available globally?

Re: How to create a private mod namespace

PostPosted: Sun Jul 03, 2016 12:21
by rubenwardy
dofile(file, http)

In file

local http = ....

It's like function calling with parameters
It's either ... or args or something like that

Re: How to create a private mod namespace

PostPosted: Sun Jul 03, 2016 13:25
by addi
Thanks for your fast response.

It seems its not such simple

2016-07-03 15:20:11: WARNING[Main]: Undeclared global variable "http" accessed at file.lua:5
2016-07-03 15:21:45: ERROR[Main]: ServerError: Lua: Runtime error from mod 'mymod' in callback on_joinplayer(): file.lua:7: attempt to index upvalue 'http' (a nil value)

Also the lua documentation does not tell about such a feature: http://www.lua.org/manual/5.1/manual.html#pdf-dofile

Re: How to create a private mod namespace

PostPosted: Sun Jul 03, 2016 21:20
by kaeza
Try:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
assert(loadfile(blah))(http)

Then use `...` as rubenwardy said.

Re: How to create a private mod namespace

PostPosted: Sun Jul 03, 2016 21:50
by blert2112
Loadfile() does not execute the code. It returns it as a function which you can then execute at your discretion. Try this:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
File1.lua :
local http = minetest.request_http_api()
dofile(file2.lua)(http)

File2.lua :
local http = ...

Have not had the chance to try it yet but it should work, I think.


Well, what do you know... That didn't work at all and, contrary to the documentation, loadfile() does execute the code.
This worked well though:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
init.lua:
local passme = "I passed!"
loadfile(minetest.get_modpath("dofile_test").."/file2.lua")(passme)

file2.lua:
local args = ...
print(args)

Re: How to create a private mod namespace

PostPosted: Mon Jul 04, 2016 08:39
by addi
Yeah! It works! Thanks very much kaeza and blert2112!