Variable Naming and Text Files

I’m new to Corona and have 2 general questions.

  1. What’s the difference between declaring a variable with and without the ‘local’ keyword? I have seen both in code examples:

local myVar1 = 0
myVar2 = 0

  1. I want to put some functions and variable declarations, especially those that don’t change frequently, into a different source file so that main.lua doesn’t get unmanageable. For example, I would like to have a myFunctions.lua and myVariableConstants.lua. Can this be done? [import]uid: 41809 topic_id: 16983 reply_id: 316983[/import]

Hey Ned, you may want to read up on Rob’s blog post. I found it super helpful:

http://omnigeek.robmiracle.com/2011/10/14/understanding-scope-for-beginning-programmers/

The above is linked from here:

http://developer.anscamobile.com/forum/2011/10/14/understanding-scope-blog-post

Naomi [import]uid: 67217 topic_id: 16983 reply_id: 63689[/import]

And here’s another good one by Jayant:

http://blog.anscamobile.com/2011/09/tutorial-scopes-for-functions/

Naomi [import]uid: 67217 topic_id: 16983 reply_id: 63690[/import]

And… another good one from Jonathan:

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

Naomi [import]uid: 67217 topic_id: 16983 reply_id: 63691[/import]

By the way, I believe you need .lua files (not .txt files) to build project and execute functions. I haven’t even thought of adding .txt file as part of my project (but then, I’m a newbie, and who knows, I may be missing something that the expert lua programmers know about.)

Naomi [import]uid: 67217 topic_id: 16983 reply_id: 63693[/import]

Hey Ned,

First up, as Naomi said - no .txt extensions - just .lua please :wink:

For the local/non local, you want to use local wherever possible. In short, if it isn’t local, it’s global and global is more memory intensive.

You should still consider looking at some of the links above, just wanted to provide a short answer too :wink:

Peach [import]uid: 52491 topic_id: 16983 reply_id: 63717[/import]

Thanks, Naomi and Peach. Great links. And yes, I did mean .lua as the file extension. It was late :frowning: [import]uid: 41809 topic_id: 16983 reply_id: 63748[/import]

@ned, @naomi,

You can use a .txt file, but then you have to parse it yourself, like my PropertyBag class used to.

However there is an advantage of using a .lua file, it can be use with a require and no parsing, no nothing all done for you.

Depends on what you are trying to achieve, one or the other will be the best solution.

cheers,

?:slight_smile:
[import]uid: 3826 topic_id: 16983 reply_id: 63749[/import]

It seems that Corona strongly discourages using globals. This is from their programming guide @ http://developer.anscamobile.com/content/app-programming-guide:

“Avoid global variables. Period. In Lua, you will sacrifice performance if you use global variables. When in doubt, precede your variable declarations with local” [import]uid: 41809 topic_id: 16983 reply_id: 63822[/import]

As Jayant said you can use .txt files, however in the scenario you describe I believe .lua makes a lot more sense. (Unless you’re a masochist ;))

RE globals, yes, you do want to avoid them wherever possible. If you can use none then that is ideal and certainly the way to go.

Peach :slight_smile: [import]uid: 52491 topic_id: 16983 reply_id: 63975[/import]

I am trying to get rid of all my globals. However, I also want to have different lua files so that I can more easily manage my code.

I created a test with 2 files: main.lua and myVariables.lua.

_ myVariables.lua: _
module(…, package.seeall)

gameScore = 0
gameTime = 20

function updateGameVariables(s, t)
gameScore = s
gameTime = t
end
_ main.lua: _
local gm = require(“gameVariables”)

print("Score: " … gm.gameScore)
print("Time: " … gm.gameTime)

gm.updateGameVariables(100, 200)

print("Score: " … gm.gameScore)
print("Time: " … gm.gameTime)

The output from print() is correct:
Score: 0
Time: 20
Score:100
Time:200

The problem is that this did not help me solve the global issue. If I put ‘local’ in front of the variables in gameVariables.lua, then I get runtime errors since main.lua can’t see them.

What is the best practice on this so that I can keep separate files and not have globals?
[import]uid: 41809 topic_id: 16983 reply_id: 64064[/import]

@ned,
this topic of global and modules has a divided group of developers, so it is touchy, without treading on those bits, I can suggest have a look at *encapsulation*. Try to use that as your way to deliver local variables as global via an object.

I might end up writing a tutorial on howto.oz-apps.com on this topic, but it will be a while, am pressed for time on other stuff.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16983 reply_id: 64133[/import]

Thanks JayantV. I look forward to your tutorial.

I also found this helpful article on Lua Classes at http://jessewarden.com/2011/10/lua-classes-and-packages-in-corona.html. [import]uid: 41809 topic_id: 16983 reply_id: 64169[/import]