Which is more memory efficient with Composer?

When using composer, would it be more memory efficient to require via a global variable in say, main.lua?

Or would it be better to use a local variable and require it in every file as necessary?

Michael

Global variables are never more efficient than local variables.  One recommendation is to use a faux-global data table.

Create a module. I call my mydata.lua but people name it whatever is convenient with these contents:

local M = {} return M

You can initialize items in there if needed:

local M = {} M.settings = {} M.score = 0 return M

Then in each module:

local myData = require("mydata.lua")

At that point you can add any variable, table, or function you want globally available to this “myData” table.  Since it’s not actually global and you have to explicitly add “myData” before each variable (myData.score), it’s immune to many of the problems with globals.

Rob

That depends on your perspective (coding v framework)…

But as Rob rightly points out globals can have real issues and cause debugging nightmares!  

But saying that, if you want a couple of globals to reference things like available screen width and they are only read only there is nothing wrong there and they require a lot less code if you have a lot of scenes.

Global variables are never more efficient than local variables.  One recommendation is to use a faux-global data table.

Create a module. I call my mydata.lua but people name it whatever is convenient with these contents:

local M = {} return M

You can initialize items in there if needed:

local M = {} M.settings = {} M.score = 0 return M

Then in each module:

local myData = require("mydata.lua")

At that point you can add any variable, table, or function you want globally available to this “myData” table.  Since it’s not actually global and you have to explicitly add “myData” before each variable (myData.score), it’s immune to many of the problems with globals.

Rob

That depends on your perspective (coding v framework)…

But as Rob rightly points out globals can have real issues and cause debugging nightmares!  

But saying that, if you want a couple of globals to reference things like available screen width and they are only read only there is nothing wrong there and they require a lot less code if you have a lot of scenes.