or download [Globals.c2addon] directly
Why native globals are bad?
The discussions whether native C2 global variables are good or not have been raised many times on the Scirra forum and beyond. Some people say globals are ok, some that they’re not. I’m in this second group and that is why I made this plugin.
The biggest issue with global variables is that they bring mess to the project in variety of ways.
Issue #1 – variables listing
Global variables appear on the variables list together with local variables. In bigger projects it starts to be really annoying as it becomes to be uncomfortable to find and pick particular variable. Especially when you just want to pick the one and only local variable available in particular group/event.
(NOTE that the following example shows just a mix of several variables of each type, I saw big projects with even more than a hundred of global variables!)
Issue #2 – polluted event sheets
C2 developers try to organize global variables by putting them in separate event sheets, what in the end makes sheets headers polluted with tons of various variable declarations.
This approach leads directly to the next issue.
Issue #3 – where the heck is my global variable?!
Trying to group up global variables by placing then on various event sheets quicky ends up in confusion of where is it located. This may not be an issue for small projects but using this approeach in at least mid-sized project with many event sheets is highly frustrating. It’s easy to forget where a particular global was created as globals by their nature doesn’t really belong to one section of the project. So after reaching this point people often create a separate event sheet only for globals. One empty event sheet with tons of globals – this is not a decent solution either. I’m not going to paste an example here for obvious reasons.
Issue #4 – vulnerability
You might have heard that global variables are dangerous for the project. This is true for real globals in standard programming languages as globals are accessible across different apps in particular environment. It is however not an issue for C2 as global variables are just values in C2 runtime object which is not accessible for apps from outside.
What is the good alternative then?
First of all you should avoid global variables as much as you can. People often overuse globals as placeholders for any logical operations. Whenever it is possible you should use local variables instead by creating variable in the event (below some condition) or under the group.
But it is not possible to live without passing data across various sections of our app so we need some kind of global placeholders. We can use Dictionary or Array objects but they aren’t handy as their indexing is string or numeric based and we lack autocompletion then.
The smartest solution that came to my mind is to use instance variables as they are globally accessible as well and have even more features than global variables.
Globals is just an empty plugin. It does absolutely nothing at all. I has no conditions, no action and no expressions at all (except of some native ones which are automatically added by C2 engine). It does not affect performance nor memory usage of the app at all. So the decent alternative solution for global variables is to use instance variables of Globals plugin. I’ll list some reasons why I find this a decent solution.
Because #1 – you can create a boolean type variable
Using boolean variables doesn’t really differ from using numeric variable with 0/1 values. Still it is better semantic-wise to use booleans whenever we need a flag variable.
Also boolean conditions look much better than numeric ones.
Because #2 – it doesn’t lack any feature
You can still set the variable name, initial value and a comment for the variable.
Because #3 – you can really group your variables
It is possible to have many instances of Globals plugin. Rename various instances and use them as groups for your “global” variables.
Because #4 – there is no more pollution on variables listing
Local variables and variables of Globals have different listing so you always see only those variables you are interested in on the listing.
Because #5 – you still have autocompletion
Unlike with Dictionary, Globals instance variables can be used in expressions and have autocompletion.
It is better to use Globals plugin approach instead of native global variables because:
- it has the same features as native globals (and more!)
- it has boolean type variables (not possible with native globals)
- it doesn’t pollute the variables listing
- it doesn’t pollute the event sheets
- it is possible to group variables (not possible with native globals)
- it doesn’t affect memory nor CPU usage
- it’s free