For those about to _G, we warn you

9 replies [Last post]
JayantV
User offline. Last seen 1 week 4 days ago. Offline
Joined: 31 Oct 2009

A few tutorials have got a lot of developers using some interesting code. It is nice to see that it has encouraged experimentation, but to what extent?

Anyways, what you use is upto you, however if there is something that you believe in called *good practices* of coding, then you need to pay attention to a few things.

Here's one article http://c2.com/cgi/wiki?GlobalVariablesAreBad that talks about why Global variables are bad and the author is using C/C++ code as an example.

He even talks about the GOTO http://c2.com/cgi/wiki?GotoConsideredHarmful

Well, if you still feel that you need to use _G, who's there to tell you not to, after all you are all responsible developers that make apps that people will either like or not like.

cheers,

?:)

Replies

Naomi
User offline. Last seen 4 hours 55 min ago. Offline
Joined: 6 Jun 2011

Very interesting & useful articles.

About global variables, I don't use _G to define them in my game project. I've been puzzling over whether it's important to use _G, or not-to-add "local" in front of a variable to declare it as global. Is there any difference?

For the time being, all global variables are declared in my main.lua, and just to make sure I don't end up modifying any exiting global variables that the system uses, I set up mine to a table in main.lua, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-- just to make sure I know what global variables are added, insert them to the list up top as I go
gamedata = {}
gamedata.fontName1 = nil;
gamedata.fontName2 = nil;
gamedata.fontSize = nil;
.
.
.
-- later on, the global variables are defined where they are used the first time around (and once defined, it never gets changed)
-- I'm considering to move these definition up top where the global variables are first declared (but not implemented yet).
gamedata.fontName1 = "some custom font1";
gamedata.fontName2 = "some custom font2";
gamedata.fontSize = 10;
 
-- as an example, I use the gamedata.fontSize as the anchor, and use it like this in my code
textObject1.size = gamedata.fontSize;
textObject2.size = gamedata.fontSize;
textObject3.size = gamedata.fontSize +10;
.
.
.

I'm wondering what might be the reason to declare variable as local in main.lua. I could be totally wrong, but I'm thinking/guessing that main.lua is always in memory/present throughout the runtime session, so whatever is local to main.lua is available to any module my project may access. And if so, what might be the point of declaring variables as local in main.lua -- is my train of thought totally mistaken?

r.pudlowski
User offline. Last seen 43 weeks 4 days ago. Offline
Joined: 9 Mar 2011

hmmm... but what you do, if for example you are using director for your levels, and you have a score to past to the score board when you finish your game.
I use a global variable for this, since all locals are erased when you change your scene in director.
wouldn't it be overkill to save a file each time, if you don't have a highscore screen?

JayantV
User offline. Last seen 1 week 4 days ago. Offline
Joined: 31 Oct 2009

I would be speculating as I am not an expert on Lua myself, there is a lot to learn about it...

However my speculation on the Local and Global is all about the Memory Heap where they are stored.

I need to run some tests, but I *think* there is some increase in performance by modifying the code

1
2
3
4
5
6
 local function test(a,b,c)
  local i, total
  for i=1,1000 do
    total = total + a * b / c
  end
 end

to

1
2
3
4
5
6
7
8
 local function test(a,b,c)
  local i, total
  local a, b, c = a, b, c
  
  for i=1,1000 do
    total = total + a * b / c
  end
 end

I hope that little speculative answer on this topic has kind of helped you to see that though global to the main.lua, it makes a difference to have them local... and in scope local again (if possible)

cheers,

?:)

JayantV
User offline. Last seen 1 week 4 days ago. Offline
Joined: 31 Oct 2009

@pudlowski,
I understand where you are coming from, but referring back to Naomi's comment, everything that you declare in main.lua is *supposedly* global.

However I also understand that saving data to files might feel like an overkill, so save it to memory, is what I would suggest.

I have used director very early in my projects, after which I have written my own little engine for my apps. If we go into how director works and then into the conversation on using module, we can keep going ... so, to cut a long story short, you have to bite the bullet somewhere and take a stance...

I would rather save to the disk, as that would help me modularise my code for suspend and resume and state save, etc (if required) than use a _G, but then that is a matter of personal choice... What works for me or what I like is not necessarily the best or the *only* way.

cheers,

?:)

producerism
User offline. Last seen 1 year 8 weeks ago. Offline
Joined: 31 Mar 2011

I'd love to see some results from those tests! I'm trying to squeeze as much performance out of Corona as possible.

edit: I was going to compare the using of Globals with your PropertyBag module... and just found out it's been taken away! :(

That's a shame, it seems that despite the overwhelming requests for support, there was some user feedback in the process that seemed to help evolve the code over time. I'd be happy to branch it out so nobody bothers you for support!

Naomi
User offline. Last seen 4 hours 55 min ago. Offline
Joined: 6 Jun 2011

Like @producerism, I'd love to learn what might be the results from the tests you mentioned above, Jayant.

@pudlowski, I use JSON to save/load high score data and any other data that are specific to each level that I need to access after the session ends (and relaunches/restarts) or from other modules. Like Jayant noted, it works for me, but it may not be the best solution for you.

glennbjr
User offline. Last seen 40 weeks 6 days ago. Offline
Joined: 17 Feb 2011

Saving to file isn't overkill. In fact I use it and it works great to save scores, settings, hero data, level unlocked and anything else you can think of that you would need to load when you start a new level or game.

Corona api, examples and other tutorials make it easier than you think.

gtatarkin
User offline. Last seen 3 weeks 6 days ago. Offline
Joined: 16 Dec 2010

hmmm... but what you do, if for example you are using director for your levels, and you have a score to past to the score board when you finish your game.

SQLite database to save result/points and SQL query as coroutine (something like cooperative multitasking) to do not block other tasks.

peach pellen
User offline. Last seen 1 year 37 weeks ago. Offline
Alumni
Joined: 12 Apr 2011

Really if you want to learn about performance benefits of using local rather than global, the best place to do that (IMHO) is from the creator of Lua; www.lua.org/gems/sample.pdf

Peach :)

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.