Lua Global Namespace Confusion - Why Does This Throw An Error?

Spring E. Thing

Member
Modder
Apr 8, 2024
41
5
230
TL;DR
Global functions in one sibling .lua file of main.lua work whereas global functions in a different sibling .lua file of main.lua outputs an error to the console about not existing. Why?


I've got a directory of .lua files for my mod that so far has sibling files that look like this:
actions.lua
animations.lua
commands.lua
common.lua
main.lua
models.lua
In main.lua, I call several functions from across the sibling .lua files of main.lua that must be global since their signature is not prefixed with the local keyword. Here's how main.lua looks in its entirity right now:
Code:
load_models()
load_anims()

hook_mario_action(ACT_MOTOMARIO_CAR_GROUNDED, act_motomario_car_grounded)
hook_mario_action(ACT_MOTOMARIO_CAR_AIR, act_motomario_car_air)

hook_chat_command('motomario', "<HELP HERE>", motomario_command)

You'll notice several function names are used as anonymous functions passed into the various hooks that aren't present in main.lua themselves, including examples such as motomario_command() and act_motomario_car_grounded(), defined in commands.lua and actions.lua respectively without the local keyword prefixing the function signature.
These have worked fine for several days now, which makes sense to me since any function with a non-local signature should be accessible for execution to all sibling .lua files to the .lua file that defined the function with the non-local signature in the mod directory.
However, then you have the start of main.lua with the calls to load_models() and load_anims(). These functions are defined in models.lua and animations.lua respectively and have non-local function signatures, meaning they should be in the global namespace and accessible to any files that are siblings of those aforementioned .lua files. load_models() and load_anims() are defined with complete parity to the likes of motomario_command() and act_motomario_car_grounded(), so they should be just as accessible to main.lua, right?
Wrong!
For whatever reason, trying to read main.lua upon booting a server with the calls to load_models() and load_anims() present gives the following error output in the console:
[LUA] ...\Portable Software\SM64 Co-Op Deluxe\mods\build\main.lua:4: attempt to call a nil value (global 'load_models')
[LUA] [0] [C]:-1 -- load_models [C]
[LUA] [1] ...\Portable Software\SM64 Co-Op Deluxe\mods\build\main.lua:4 -- <unknown> [main]
--------------
1 string ...\Portable Software\SM64 Co-Op Deluxe\mods\build\main.lua:4: attempt to call a nil value (global 'load_models')
--------------
[LUA] Failed to execute lua script 'C:\Users\JArms\Portable Software\SM64 Co-Op Deluxe\mods\build\main.lua'.

How can this output be happening? Many thanks for reading and continued support across threads in this section of the forum : )
 
Solution
These functions haven't initialized is why. Main.lua is ran first, so these functions dont exist. I'd recommend running these on level_init with a check if this is the first time or not
These functions haven't initialized is why. Main.lua is ran first, so these functions dont exist. I'd recommend running these on level_init with a check if this is the first time or not
As I understand it, your solution of the HOOK_ON_LEVEL_INIT hook would work because that hook will happen pretty much for the first time immediately after the server is done loading and therefore after all .lua files have been read and not just main.lua, right? Regardless, I'll give it a shot now.
 

Users who are viewing this thread