I'm trying to create a module through the best practice means rather than package.seeall. Below is the code I've used. I'm running into an error where once I run require("mylibrary"), m seems to be returning a nil value.
I'm getting the following error:
[ERROR] gamemodes/mygamemode/gamemode/init.lua:374: attempt to index local 'mm' (a nil value)
1. addonsandmodules - gamemodes/mygamemode/gamemode/init.lua:374
2. unknown - gamemodes/mygamemode/gamemode/init.lua:380
from mylibrary.lua
print("DEBUG: prior to creation of moduel done")
local m = {}
print("creation of module done")
function m.soup()
return "soup is good"
end
function m.roll(num)
print("rolling the dice...")
return math.random(1,num)
end
print("DEBUG: functions defined")
m.public_variable = "hello from my_module"
print(m.public_variable)
print("made it right up to return value")
return m
from init.lua
local mm = {require("mylibrary")}
print(mm)
print(mm.public_variable)
I could use package.seeall and kind of skip this but if anyone can help me figure out where I'm going wrong, would be appreciated.
I tried printing debugging steps to see where the module failed. Unfortunately nothing is printing.
require.require("mylibrary")runs the code inmylibrary.lua, so here it returns the tablemfrommylibrary.lua. But you wrap this return value in another table: you could get to the module with, e.g.,mm[1].public_variable, but instead load the module withlocal mm = require("mylibrary")so that you can usemm.public_variable.