What about this generic "mod" var ?

Post Reply
User avatar
Pyrollo
Developer
Posts: 385
Joined: Mon Jan 08, 2018 15:14
GitHub: pyrollo
In-game: Naj
Location: Paris

What about this generic "mod" var ?

by Pyrollo » Post

Hello,

Usually, I start my mods with code looking like that :

Code: Select all

mymod = {}
mymod.name = minetest.get_current_modname()
mymod.path = minetest.get_modpath(mymod.name)

dofile(mymod.path.."/functions.lua")
...
mymod being the name of my mod.

In functions.lua, I have to prefix mod wide functions with mymod.

Tired of always changing the name of the mod and searching a solution to make it possible to include same helper lua file in several mods, I ended up writing this:

Code: Select all

local mod = {}
_G[minetest.get_current_modname()] = mod
mod.name = minetest.get_current_modname()
mod.path = minetest.get_modpath(mod.name)

dofile(mod.path.."/functions.lua")
...
Then add this line in every lua file:

Code: Select all

local mod = _G[minetest.get_current_modname()]
In this way, the var for current mod is always mod but it is seen by other mods as a global var named with the mod name (mymod for example).

It looks a good solution for portability but I'm not sure about the drawbacks (creating a global var named like mod, without any controls).

What do you think about that ?
[ Display Modpack ] - [ Digiterms ] - [ Crater MG ] - [ LATE ]

User avatar
Linuxdirk
Member
Posts: 3216
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: What about this generic "mod" var ?

by Linuxdirk » Post

Pyrollo wrote:What do you think about that ?
Stick with mymod = {}, it’s much cleaner.

User avatar
Pyrollo
Developer
Posts: 385
Joined: Mon Jan 08, 2018 15:14
GitHub: pyrollo
In-game: Naj
Location: Paris

Re: What about this generic "mod" var ?

by Pyrollo » Post

Linuxdirk wrote:
Pyrollo wrote:What do you think about that ?
Stick with mymod = {}, it’s much cleaner.
It needs some explanations. Said like that it looks like an opinion. Can you elaborate ?
[ Display Modpack ] - [ Digiterms ] - [ Crater MG ] - [ LATE ]

User avatar
AspireMint
Member
Posts: 415
Joined: Mon Jul 09, 2012 12:59
GitHub: AspireMint
IRC: AspireMint
In-game: AspireMint
Location: Stuck at spawn

Re: What about this generic "mod" var ?

by AspireMint » Post

Not sure if i understood...
My solution is append functions to mymod/thatmod/alsothatmod object/table.
(objects Functions and Helpers are singletons, they are just helpers... in my case)

Lets go:

init.lua

Code: Select all

local DIR_WITH_GLOBAL_FUNCTIONS = "C:/Windows/Boot/Fonts/whatever/"

dofile(DIR_WITH_GLOBAL_FUNCTIONS .. "helpers.lua") -- register global Helpers object
dofile(DIR_WITH_GLOBAL_FUNCTIONS .. "functions.lua") -- register global Functions object

mymod = {}
mymod.name = minetest.get_current_modname()
mymod.path = minetest.get_modpath(mymod.name)

Helpers.decorate_obj(mymod, Functions)

mymod.print_hello()
output: Hello universe!

helpers.lua

Code: Select all

function getHelpers()
    if __HELPERS__ then return __HELPERS__ else __HELPERS__ = {} end
    
    local self = __HELPERS__
    
    self.decorate_obj = function(mod, functions)
        for k,v in pairs(functions) do
            mod[k] = v
        end
    end
    
    -- self.blahblah = function ...
    
    return self
end

Helpers = getHelpers()
functions.lua

Code: Select all

function getFunctions()
    if __FUNCTIONS__ then return __FUNCTIONS__ else __FUNCTIONS__ = {} end
    
    local self = __FUNCTIONS__
    
    self.print_hello = function()
        print("Hello universe!")
    end
    
    -- self.blahblah = function ...
    
    return self
end

Functions = getFunctions()
---

if you need to access mod path or other properties in functions (you should not, but yeah it is up to you) then use mymod as parameter in functions or use mymod as param in Functions constructor, etc etc...

User avatar
Linuxdirk
Member
Posts: 3216
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: What about this generic "mod" var ?

by Linuxdirk » Post

Pyrollo wrote:It needs some explanations.
Don’t “manually” mess with _G. No, simply don’t.

This aside you need to do …

Code: Select all

local mod = {}
_G[minetest.get_current_modname()] = mod
… for every mod instead of a simple modname = {}. And in all mod files you need to repeat local mod = _G[minetest.get_current_modname()] instead of simply accessing the global variable mymod.

User avatar
Pyrollo
Developer
Posts: 385
Joined: Mon Jan 08, 2018 15:14
GitHub: pyrollo
In-game: Naj
Location: Paris

Re: What about this generic "mod" var ?

by Pyrollo » Post

Linuxdirk wrote:No, simply don’t.
Sorry that's way too dogmatic for me. We are talking about programming, not religion :p
I can't see anything preventing from using this language feature (even the LUA manual refers to this practice for dynamic variable naming).
Linuxdirk wrote:This aside you need to do …

Code: Select all

local mod = {}
_G[minetest.get_current_modname()] = mod
… for every mod instead of a simple modname = {}. And in all mod files you need to repeat local mod = _G[minetest.get_current_modname()] instead of simply accessing the global variable mymod.
Yes, you are right. I saw these advantages of using a local var :
  • Ability to change the mod name without touching a single line in its code (ok, that does not happen often)
  • Easily reuse the same code from a mod to another one (but copy pasting code is bad ;) but init.lua files always look the same)
  • Possibility to reuse the same file (for debuging, profiling, etc) just puting the file in the mod
Inconvenients :
  • Less readable (and as you said, less convenient)
  • Unable to use mod code as example (have to change prefix from mod to actual mod name)
I'll keep that for my quick and dirty debuging and profiling lua files.
[ Display Modpack ] - [ Digiterms ] - [ Crater MG ] - [ LATE ]

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests