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?
[solved] How to create a private mod namespace
- 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
Last edited by addi on Mon Jul 04, 2016 08:39, edited 1 time in total.
- rubenwardy
- Moderator
- Posts: 6978
- 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
dofile(file, http)
In file
local http = ....
It's like function calling with parameters
It's either ... or args or something like that
In file
local http = ....
It's like function calling with parameters
It's either ... or args or something like that
- 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
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
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
- 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
Try:
Then use `...` as rubenwardy said.
Code: Select all
assert(loadfile(blah))(http)
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
Check out my stuff! | Donations greatly appreciated! PayPal
Re: How to create a private mod namespace
Loadfile() does not execute the code. It returns it as a function which you can then execute at your discretion. Try this:
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
File1.lua :
local http = minetest.request_http_api()
dofile(file2.lua)(http)
File2.lua :
local http = ...
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)
- 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
Yeah! It works! Thanks very much kaeza and blert2112!
Who is online
Users browsing this forum: No registered users and 9 guests