[solved] How to create a private mod namespace

User avatar
addi
Member
 
Posts: 605
Joined: Thu Sep 20, 2012 03:16

[solved] How to create a private mod namespace

by addi » Sun Jul 03, 2016 12:17

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
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: How to create a private mod namespace

by rubenwardy » Sun Jul 03, 2016 12:21

dofile(file, http)

In file

local http = ....

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

User avatar
addi
Member
 
Posts: 605
Joined: Thu Sep 20, 2012 03:16

Re: How to create a private mod namespace

by addi » Sun Jul 03, 2016 13:25

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
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

Re: How to create a private mod namespace

by kaeza » Sun Jul 03, 2016 21:20

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.
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 | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 

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

Re: How to create a private mod namespace

by blert2112 » Sun Jul 03, 2016 21:50

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)
 

User avatar
addi
Member
 
Posts: 605
Joined: Thu Sep 20, 2012 03:16

Re: How to create a private mod namespace

by addi » Mon Jul 04, 2016 08:39

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


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 12 guests

cron