Lua file seems to be imported multiple times

,

As per the documentation, a lua file should never be executed more than once when using import

I feel this breaks when structuring a project in subfolders. See my structure here:

This produces the following output:

importing gameHUD.lua
table: 0x7faafa461490	gameHUD init
stack traceback:
	/lua/gameHUD.lua:38: in field 'init'
	CoreLibs/object.lua:70: in global 'GameHUD'
	/lua/gameHUD.lua:43: in main chunk
importing gameHUD.lua
table: 0x7faafa3dc5b0	gameHUD init
stack traceback:
	/lua/level-select/../gameHUD.lua:38: in field 'init'
	CoreLibs/object.lua:70: in global 'GameHUD'
	/lua/level-select/../gameHUD.lua:43: in main chunk
importing gameHUD.lua
table: 0x7faafa3f0d10	gameHUD init
stack traceback:
	/lua/start/../level-select/../gameHUD.lua:38: in field 'init'
	CoreLibs/object.lua:70: in global 'GameHUD'
	/lua/start/../level-select/../gameHUD.lua:43: in main chunk

What I'm expecting to happen is that the file gets imported once, and also the init function gets called once because I use GameHUD() nowhere else except in the file itself

-- global singleton
gameHUD = GameHUD()

Wow. I don't know what's going on here or how we missed it, but that's definitely not right. :frowning: Filed, and will look into it as soon as I catch up on getting all the bug reports in the tracker.

1 Like

A hunch: when keeping a map of files which have been executed; make sure you use absolute paths so that "ganeHUD.lua" "../gameHUD.lua" get transformed to Source/GameHUD.lua" and match the same entry

That's what it should be doing--I patched the Lua compiler so that import statements resolve the file and replace the given path with the full project-relative path--but I must have goofed something up. But I expect it'll be easy to reproduce with your test case and hopefully not too terrible to fix. :crossed_fingers:

1 Like