Global or local variables?

Hi guys

As the title are torn between the use of local and global variables.

In my game I did different classes and almost all require common variables as standard size (es. 

local dimGuy = display.contentWidth/3

).

My question is therefore whether it would be better to declare these variables (about 10) as global in the main, or declare from time to time these variables in each class. The fact is that if I had to change one should do it in all classes, and I do not like.

On the other hand if the global variables did, my classes would no longer OOP. What do you suggest? I have the same problem with some of the methods that I use often …

If you keep your globals organized with a standard naming convention and there are no objects in them, there is no harm in using them. Other than that, stay local.

thank you bgmadclown

So in this case dimGuy should be global ? Would not give problems with OOP?

Nope, it will be fine. Just try not to change names of the variables too much as you will lose track of it while renaming.

Then I proceed so.

Thanks for support :slight_smile:

Before you proceed, please read:  https://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

In addition Global’s are the slowest Lua variables to index. Resolving the address on _G members is slower than addressing members of a normal table (something about how they are allocated).

Global variable names need to follow the basic rules of scoping and name spacing. That is for a local function a variable x might be sufficient for the context of a for loop. But x on a module level makes very little since when screenTouchX makes more sense. Global variables are the widest scope and need to most unique, detailed variable names.

There are many ways of passing values around between modules. Writing an extra line per module and a few more characters for each variable (which if you name spaced the variable correctly, will be about the same), is a healthy sacrifice to make to avoid the numerous pitfalls of using global variables.

Rob

Many thanks Rob.

I read the article and not I will use global variables. After reading everything I believe that the best approach is to create a module with variables and functions in common use and call in the other modules / classes. But I have not read anything about the OOP.

Can I then ask you if you recommend using a form as I was saying or if to be consistent with the 'OOP is better to declare the same variables and functions for each class?

Thanks very much for the clarification

If you keep your globals organized with a standard naming convention and there are no objects in them, there is no harm in using them. Other than that, stay local.

thank you bgmadclown

So in this case dimGuy should be global ? Would not give problems with OOP?

Nope, it will be fine. Just try not to change names of the variables too much as you will lose track of it while renaming.

Then I proceed so.

Thanks for support :slight_smile:

Before you proceed, please read:  https://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

In addition Global’s are the slowest Lua variables to index. Resolving the address on _G members is slower than addressing members of a normal table (something about how they are allocated).

Global variable names need to follow the basic rules of scoping and name spacing. That is for a local function a variable x might be sufficient for the context of a for loop. But x on a module level makes very little since when screenTouchX makes more sense. Global variables are the widest scope and need to most unique, detailed variable names.

There are many ways of passing values around between modules. Writing an extra line per module and a few more characters for each variable (which if you name spaced the variable correctly, will be about the same), is a healthy sacrifice to make to avoid the numerous pitfalls of using global variables.

Rob

Many thanks Rob.

I read the article and not I will use global variables. After reading everything I believe that the best approach is to create a module with variables and functions in common use and call in the other modules / classes. But I have not read anything about the OOP.

Can I then ask you if you recommend using a form as I was saying or if to be consistent with the 'OOP is better to declare the same variables and functions for each class?

Thanks very much for the clarification