For those about to _G, we warn you

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,

?:slight_smile: [import]uid: 3826 topic_id: 16282 reply_id: 316282[/import]

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:

[lua]-- 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;
.
.
.[/lua]

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? [import]uid: 67217 topic_id: 16282 reply_id: 60646[/import]

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? [import]uid: 44010 topic_id: 16282 reply_id: 60651[/import]

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

 local function test(a,b,c)  
 local i, total  
 for i=1,1000 do  
 total = total + a \* b / c  
 end  
 end  

to

 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,

?:slight_smile: [import]uid: 3826 topic_id: 16282 reply_id: 60652[/import]

@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,

?:slight_smile: [import]uid: 3826 topic_id: 16282 reply_id: 60657[/import]

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. [import]uid: 67217 topic_id: 16282 reply_id: 60662[/import]

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! :frowning:

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! [import]uid: 49447 topic_id: 16282 reply_id: 60658[/import]

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. [import]uid: 38820 topic_id: 16282 reply_id: 60689[/import]

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. [import]uid: 12704 topic_id: 16282 reply_id: 60717[/import]

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 :slight_smile: [import]uid: 52491 topic_id: 16282 reply_id: 60720[/import]