[solved] How to create a private mod namespace

Post Reply
User avatar
addi
Member
Posts: 666
Joined: Thu Sep 20, 2012 03:16
GitHub: adrido
Location: Black-Forest, Germany

[solved] How to create a private mod namespace

by addi » Post

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?
Last edited by addi on Mon Jul 04, 2016 08:39, edited 1 time in total.

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

Re: How to create a private mod namespace

by rubenwardy » Post

dofile(file, http)

In file

local http = ....

It's like function calling with parameters
It's either ... or args or something like that
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
addi
Member
Posts: 666
Joined: Thu Sep 20, 2012 03:16
GitHub: adrido
Location: Black-Forest, Germany

Re: How to create a private mod namespace

by addi » Post

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

User avatar
kaeza
Moderator
Posts: 2162
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza
Location: Montevideo, Uruguay
Contact:

Re: How to create a private mod namespace

by kaeza » Post

Try:

Code: Select all

assert(loadfile(blah))(http)
Then use `...` as rubenwardy said.
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal

blert2112
Member
Posts: 244
Joined: Sat Apr 25, 2015 04:05
GitHub: blert2112

Re: How to create a private mod namespace

by blert2112 » Post

Loadfile() does not execute the code. It returns it as a function which you can then execute at your discretion. Try this:

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.[/s]

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:

Code: Select all

init.lua:
local passme = "I passed!"
loadfile(minetest.get_modpath("dofile_test").."/file2.lua")(passme)

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

User avatar
addi
Member
Posts: 666
Joined: Thu Sep 20, 2012 03:16
GitHub: adrido
Location: Black-Forest, Germany

Re: How to create a private mod namespace

by addi » Post

Yeah! It works! Thanks very much kaeza and blert2112!

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests