Global Variables, too much of a good thing?

Started by Sparkplug.Creations, Mon 28/07/2008 06:32:33

Previous topic - Next topic

Sparkplug.Creations

I only use global variables with the new 3.02 release because it's super easy to use, declare them in one place and then any room script can access them.

Are there any downsides to doing this instead of using local variables?
Reid Kimball - Game Designer
__________________________________
Sparkplug Creations - Play for a Change!
http://sparkplugcreations.org/

LeChuck

#1
If you use a lot of them they quickly become hard to keep track of. If you use them, type down the numbers in a text file along with a destription of what they do. If you mix them up, be prepared for strange bugs. I've completely stopped using 'em.

edit:
Quote from: monkey_05_06 on Mon 28/07/2008 09:51:06
He's not referring to SetGlobalInt/GetGlobalInt which are completely different and unrelated functions.

Ay caramba.. Well I guess my statements are true for people using 2.72 and older...

hedgefield

That's what used to happen with Global Ints. I had a long list of 'em, and one time I forgot to give one a description and consequently could not for the life of me figure out where I used it.

But with the global variables editor its easy. You can call its name directly from the script so no need to remember it's number. As extra, I always add the prefix gvar_ to whatever name I give the variable so I can easily recognize it in a script.
I guess one of the 'dangers' is that if you edit the variable a lot from many different rooms, you could lose track of what you're doing to it and where. But I'm not convinced turning it into local variables is any less confusing. Afer all, it's still code. :)

monkey0506

#3
He's not referring to SetGlobalInt/GetGlobalInt which are completely different and unrelated functions.

He is referring to a new feature of the AGS editor, the "Global Variables" pane which allows us to create global variables of various types to simplify the import/export process.

I can't say for sure, so this may be complete bullocks, but I imagine that if you have a lot of global variables, the only difference would be a higher static memory usage. That is, since the variables are global, they are always defined, they are always taking up memory. Compare this to local room variables which would only be defined and take up memory while that room was loaded.

The amount of memory required by variables is (AFAIK; if anyone spots an error feel free to clue me in):

char: 1 byte
short: 2 bytes (note that if you're operating at 32-bit or higher, it is actually slower to use the 16-bit short data-type than the 32-bit int type, so it should only be used for things like very large arrays)
int, float, bool, enumerated types, and pointers: 4 bytes
String: 4 bytes + length of string bytes

So realistically unless you're using several hundred global variables, I'd say you probably have no need to worry.

Edit:

Khris brings up a fair point. You shouldn't use global variables just because you can. Global variables are intended as just that: global variables. If you need to keep track of whether a light-switch is on or off in one room, and you only need to know that information in that room, a local variable is the better alternative. But then for example if the breaker was blown and you needed to know in several rooms whether the lights were on or off (whether the breaker had been reset), a global variable would be more accommodating.

Khris

As an additional note, the first 300 rooms save the state of their local variables, so it's definitely recommended to use them if you don't need the var to be global.

Sparkplug.Creations

OK, I'll try to do better managing which variables should be local vs. global. Thanks all for the input.
Reid Kimball - Game Designer
__________________________________
Sparkplug Creations - Play for a Change!
http://sparkplugcreations.org/

SMF spam blocked by CleanTalk