Getting into some bad habits with _G

I'm finding it very convenient to store variables and tables in _G

It makes it easy to access variables across modules and different screens.

I also realise that this isn't efficient.

Two questions:

How much of an issue is this in game development?

And where can I find some real newbie tutorials on alternatives to using _G?

And I MEAN newbie. ;-)

Thanks!

Tom

Global variables can cause memory problems... Just make sure that when you are finished with the global variables you nil them out so they take up less memory!

This blog post should help you...

http://blog.anscamobile.com/2011/07/local-variables-lua/

Global variables are not something that should be never used.
The 2 main problems I see in using global variables are
1. It clutters the global environment with unnecessary names.
2. Access to global variables are slower than local ones.

but to overcome this you just need to make sure that it is properly handled. If you are using a function its better not to use global variables. But I think global variables are the best way to pass a value from one file to another.
So once you get a value from the global variable just set it to nil. you may do something like this

1
2
local var = _G.var
_G.var = nil

@renvis .... I wouldn't go as far as saying global variables should never be used.

@ The topic :

As nice as it is to believe you can keep everything 100% local, not every situation can work with this rigid theory.

Sure the less things that you have to have defined as global the better, but, at the end of the day it's all down to the programmer to manage his code effectively to ensure things are being cleaned up and handled correctly.

Danny, Renjith said "Global variables are not something that should be never used." - a little confusing, but you both agree ;)

As do I - store some things by all means if you don't have other options - I've made a few things using quite a lot of global variables, particularly in my earlier Corona days and I've never seen it as a big deal.

Local is your goal but when it wont work, I wouldn't stress if you have to be a little liberal with your globals ;)

Peach

Guys, thank you very much for the posts and help.

I had a real lightbulb moment when I read "So once you get a value from the global variable just set it to nil."

That's a great tip!

I think I'm finally getting the hang of Corona... maybe!

Check out this chapter of "Lua Programming Gems" from Roberto himself (the creator of Lua):

http://www.lua.org/gems/sample.pdf

He discusses the performance benefits of using local variables instead of globals (_G).

Way to one up us all, Walter ;)

I haven't seen that before, look forward to reading it properly this afternoon - looks very interesting :)

@Tom, stick with it. Lightbulbs will start going off left, right and center. The more you play around with Lua the more it will start to become intuitive. With Corona, it only gets better :)

views:1623 update:2011/9/26 15:43:22
corona forums © 2003-2011