Hi peeps,
Sorry… another long-winded query… from a newbie trying to learn best practises.
As I code my game, I am noticing that I am repeating much of the same code, particularly functions, on each scene. I’ve 78 Game play scenes, plus title scenes etc.
Much of the game flow between scenes and scene creation is the same code repeated on each scene.
I’ve started to declare some of my own Global Variables, for “information” I need to use regularly but don’t want to repeatedly calculate.
I’ve used the _G. approach. but start every variable name with the prefix “my” so that I know it’s my own created global variable (so I don’t inadvertently overwrite system created _G. variables in the Global Table with variables that incidentally end up having the same name).
But… I’ve created a prerequisites file that I store these in and load them right at the start of the app launch.
EG.
In main.lua calls…
composer.gotoScene( "loadPrerequisites", { time=1000, effect="crossFade" } )
Then in loadPrerequisites.lua I call…
composer.gotoScene( "GamePlay\_Scene1", { time=1000, effect="crossFade" } )
So by the time I get to my first scene in the game, I’ve loaded into memory any variables that any scene in my game will need.
BUT I want to adopt a similar approach for functions.
I know by declaring a function without the “local” word, it’s scope is app-wide. But it’s how to manage where these functions live.
Would the principle work by also putting them in my loadPrerequisites.lua file.
HOWEVER…
I just read this…
https://docs.coronalabs.com/tutorial/basics/globals/index.html
In particularly the section about the Data Module and requiring it into the scenes that need to use it’s content.
Is this what I should be doing?
Have I worked out my own solution using a pre-requisites file, but is it “bad” practise to work my way?
I’d rather “learn” to do things the way “industry” expects and conventions already in place.
Some advice please.
My goal is to tidy up all code used on my scenes, by creating functions once, that I call from where and when necessary, simply passing and returning results as I need them.
I am also hoping, that this resolves my memory leakage problem (which I speak of in another post) .
Thanks
Angela