I’m working on setting up application-wide variables, and I’m caching some global variables (and functions) that I use a lot. I’m not sure if locally caching a table ALSO locally caches all its indexes.
This seems like a dumb question, my first thought was “of course!”, but then I found out all the global variables are stored in _G and I can’t imagine that “local _G = _G” would cache every global variable in a single line, seems like that’s too dangerous to allow.
Let’s say I have a situation like this, I declare some global variables in my main.lua file:
[lua]gameVars = {}
gameVars.sfx = true
gameVars.currentLevel = 1
gameVars.playerName = “Default Player”[/lua]
Then in another module I have this:
[lua]local gameVars = gameVars
print ( gameVars.playerName ) – is gameVars.playerName local or global?[/lua]
So is caching “gameVars” as a local enough, or do I have to cache EACH of its indexes individually as local, like this:
[lua]local gameVars.sfx
local gameVars.currentLevel
local gameVars.playerName
print ( gameVars.playerName ) – gameVars.playerName is definitely local here[/lua]
The same situation applies (I think) for external libraries:
[lua]local enemyLib = require “enemyLib”
enemyLib.spawnEnemy() – is this a local or a global function call?[/lua]
I can’t seem to find any documentation on this and I’m not sure how to test for it… [import]uid: 49372 topic_id: 9407 reply_id: 309407[/import]