To give an example, here is how my structure roughly looks like in main.lua:
Code: Select all
NFS = require( "lib.nativefs" )
PATH = require( "lib.path.path" )
JSON = require( "lib.dkjson.dkjson" )
STRICT = require( "lib.luastrict.strict" )
require( "scripts.utils" )
require( "scripts.font" )
require( "scripts.music" )
require( "scripts.input" )
require( "scripts.splash" )
require( "scripts.gamestate" )
function love.load( Args )
FONT:Init()
MUSIC:Init()
SPLASH:Init()
end
function love.update( DeltaTime )
INPUT:Update( DeltaTime )
MUSIC:Update( DeltaTime )
GAME_STATE:Update( DeltaTime )
end
function love.draw()
GAME_STATE:Draw()
end
Code: Select all
GAME_STATE = {
StateList = {
"Splash",
"MainMenu",
"Map",
"World",
"Fight",
"PauseMenu",
"Credits"
},
CurrentState = "Splash",
----------------------------------------
GetCurrentState = function (self)
return self.CurrentState
end,
----------------------------------------
Update = function(self, DeltaTime)
if self.CurrentState == "Splash" then
SPLASH:Update( DeltaTime )
end
end,
----------------------------------------
Draw = function(self)
if self.CurrentState == "Splash" then
SPLASH:Draw()
end
end
}
This is convenient for me to be able to reference each object via other script, kind of like a system.
So now I know I could store the reference to the global into a local variable inside each script to speed things up a bit, however I wonder if there is an even better approach ? I prefer to look into that now before having to refactor a lot of code later.