Data Modules vs Loading Prerequisites file. Good or Bad Practises.

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

The easiest approach is to create modules that you can require and call for in scenes that you need them in. For instance,

main.lua

local myModule = require("myModule") local number = myModule.double( 4 ) print( number )

myModule.lua

local mod = {} function mod.double( n ) return n\*2 end return mod

All that really happens here is that I create a table inside of myModule and then I add a function inside of that table. Then, once I return the table to whatever scene/file I need it in, e.g. main.lua, then I can just require it and call the function from within the table. This keeps everything local and running smoothly.

The same really applies if you have some variables that you need to keep track of. If you are using Corona’s composer, then you could create a table there where you store information that you’ll require elsewhere, i.e.

local composer = require("composer") -- You can store variables to this table and they'll be accessible wherever you require composer. composer.state = {} composer.state.score = 999 composer.state.lives = 3 -- and so on.

Lua keeps any module that you require cached until you clear it, which means that you could also have Lua files that consist of a single table that you require in various scenes, e.g.

stats.lua

local t = { score = 999, lives = 3 } return t

Once you require that “stats.lua”, you’ll be able to easily access the table  t’s contents. Since Lua keeps it cached, if you require it in numerous scenes, they’ll all access and update the same table, so it’ll stay updated across scenes.

Oh, and finally, it makes sense to arrange your modules per type or category. For instance, I have a trigVert.lua module that I’ve filled with all sorts of trigonometric and vertex equations, I also have an encryption enabled save/load module, etc. that I can just move from project to the next.

Thank you XeduR @Spyric

This was just the clarity I needed.

I’m trying to future proof my code as well in exactly the same way you just described with your modules.

Thanks for replying.