Global Variables Question

Is there an easy way to find global variables? I’m not talking about dumping the _G table, but looking fro variables that I may have forgotten to put a “local” on and I want to make sure they are stomping on each other.

[import]uid: 19626 topic_id: 33072 reply_id: 333072[/import]

Hey, Rob, check this out:

http://developer.coronalabs.com/forum/2011/12/16/handy-code-snippets#comment-126283

This code snippet that Ntero shared is awesome.

Naomi [import]uid: 67217 topic_id: 33072 reply_id: 131342[/import]

@rob,

I don’t entirely understand your question. You’re basically looking for loose globals or accidentally created globals? Can you give a for example?

Note: You should be able to do some interesting debug setup by modifying the metatable for _G:
http://www.lua.org/pil/14.2.html

You could make your globals ‘shout out’ when they get created or initialized.

You could try this:

  1. Modify _G to report when globals are created or initialized.

  2. Modify require to spit out the file name you’re requiring. This will make it easier to find globals that are created during init/loading.
    [lua]_G.cachedRequire = require

_G.require = function( … )

print("***@@@*** Requiring file: ", arg[1] )
cachedRequire( unpack(arg) )
print("Done requiring file: ", arg[1], “***@@@***” )
end[/lua]

  1. Run game.

When you load your game, you’ll get a bunch of messages. You will be able to find and check globals that were created during the require because they’ll be framed by the ‘requiring’ messages.

Then it will go quiet.

After than, any more messages saying, “I got initialized.” are probably be accidental globals.

Am I making sense?

It’s a bit brute force, but it might help solve most of the accidental globals. [import]uid: 110228 topic_id: 33072 reply_id: 131346[/import]

@Naomi,

Nice! Ntero’s solutions is way better (sharing part a solution but much better) and super simple.

Awesome Cakes! [import]uid: 110228 topic_id: 33072 reply_id: 131351[/import]

It’s been awhile since I used it, but I think the Corona Profiler tool from M.Y. Developers ( http://www.mydevelopersgames.com/site/ ) has a report for all the globals in your code (even if you don’t use _G). [import]uid: 67839 topic_id: 33072 reply_id: 131353[/import]

I use Lua Glider (http://www.mydevelopersgames.com/CIDER/). Like any good IDE it has a variable watch on the bottom, open it up while you’re running your app and expand ‘globals’. I found about 10 variables that I forgot to put local in front of, also fixed a bug where I created a misspelled version of a variable I was trying to access :stuck_out_tongue:

-Treb [import]uid: 181948 topic_id: 33072 reply_id: 131355[/import]

Hey, Rob, check this out:

http://developer.coronalabs.com/forum/2011/12/16/handy-code-snippets#comment-126283

This code snippet that Ntero shared is awesome.

Naomi [import]uid: 67217 topic_id: 33072 reply_id: 131342[/import]

@rob,

I don’t entirely understand your question. You’re basically looking for loose globals or accidentally created globals? Can you give a for example?

Note: You should be able to do some interesting debug setup by modifying the metatable for _G:
http://www.lua.org/pil/14.2.html

You could make your globals ‘shout out’ when they get created or initialized.

You could try this:

  1. Modify _G to report when globals are created or initialized.

  2. Modify require to spit out the file name you’re requiring. This will make it easier to find globals that are created during init/loading.
    [lua]_G.cachedRequire = require

_G.require = function( … )

print("***@@@*** Requiring file: ", arg[1] )
cachedRequire( unpack(arg) )
print("Done requiring file: ", arg[1], “***@@@***” )
end[/lua]

  1. Run game.

When you load your game, you’ll get a bunch of messages. You will be able to find and check globals that were created during the require because they’ll be framed by the ‘requiring’ messages.

Then it will go quiet.

After than, any more messages saying, “I got initialized.” are probably be accidental globals.

Am I making sense?

It’s a bit brute force, but it might help solve most of the accidental globals. [import]uid: 110228 topic_id: 33072 reply_id: 131346[/import]

@Naomi,

Nice! Ntero’s solutions is way better (sharing part a solution but much better) and super simple.

Awesome Cakes! [import]uid: 110228 topic_id: 33072 reply_id: 131351[/import]

It’s been awhile since I used it, but I think the Corona Profiler tool from M.Y. Developers ( http://www.mydevelopersgames.com/site/ ) has a report for all the globals in your code (even if you don’t use _G). [import]uid: 67839 topic_id: 33072 reply_id: 131353[/import]

I use Lua Glider (http://www.mydevelopersgames.com/CIDER/). Like any good IDE it has a variable watch on the bottom, open it up while you’re running your app and expand ‘globals’. I found about 10 variables that I forgot to put local in front of, also fixed a bug where I created a misspelled version of a variable I was trying to access :stuck_out_tongue:

-Treb [import]uid: 181948 topic_id: 33072 reply_id: 131355[/import]