Minetest 5.0.0 is on the road

User avatar
sorcerykid
Member
 
Posts: 832
Joined: Fri Aug 26, 2016 15:36
Location: Illinois, USA
GitHub: sorcerykid
In-game: Nemo

Re: Minetest 5.0.0 is on the road

by sorcerykid » Thu Sep 13, 2018 12:15

If you are using dofile(), you don't need to resort to a global table. The following trick makes it possible to to share a truly protected namespace amongst all the included files in your mod :)

In init.lua, you pass the namespace as a parameter to the function returned by dofile()

Code: Select all
local this = {}

dofile(mod_path .. "/helpers.lua")(this)
dofile(mod_path .. "/tools.lua")(this)

And in the included file(s), you simply capture the namespace via a returned function.

Code: Select all
local this

local function bar(whatever)
  -- add functionality here
  this.foo()
end

return function (parent)
  this = parent
end

PS. If you're not averse to using loadfile() instead of dofile(), then the included files could be simplified even further

Code: Select all
local this = ...

local function bar(whatever)
  -- add functionality here
  this.foo()
end
 

User avatar
Beerholder
Member
 
Posts: 198
Joined: Wed Aug 03, 2016 20:23
GitHub: evrooije
In-game: Beerholder

Re: Minetest 5.0.0 is on the road

by Beerholder » Thu Sep 13, 2018 15:08

sorcerykid wrote:In init.lua, you pass the namespace as a parameter to the function returned by dofile()


I learned something new today O_o Thanks sk!! I think there are some places here and there where I could use this little trick :D
 

User avatar
PolySaken
Member
 
Posts: 256
Joined: Thu Nov 09, 2017 05:18
Location: djelibabi
GitHub: PolySaken-I-Am
In-game: PolySaken

Re: Minetest 5.0.0 is on the road

by PolySaken » Thu Sep 13, 2018 20:44

sorcerykid wrote:If you are using dofile(), you don't need to resort to a global table. The following trick makes it possible to to share a truly protected namespace amongst all the included files in your mod :)

In init.lua, you pass the namespace as a parameter to the function returned by dofile()

Code: Select all
local this = {}

dofile(mod_path .. "/helpers.lua")(this)
dofile(mod_path .. "/tools.lua")(this)

And in the included file(s), you simply capture the namespace via a returned function.

Code: Select all
local this

local function bar(whatever)
  -- add functionality here
  this.foo()
end

return function (parent)
  this = parent
end

PS. If you're not averse to using loadfile() instead of dofile(), then the included files could be simplified even further

Code: Select all
local this = ...

local function bar(whatever)
  -- add functionality here
  this.foo()
end

I did in fact know of this, but I wanted other mods to be able to interact with the global table as it has functions for creating new recipes for machines and also contains it's own settings object. I don't dislike the minetest settings object, but I prefer to use my own. This also allows me to have per-world settings (being added next), and you can change some of te settings while the game is running.
 

Previous

Return to News



Who is online

Users browsing this forum: No registered users and 4 guests