9.1 Global variables
So far, all our variables were local variables, usable only inside the function
where they were declared. But what if we need to access the same variable inside more functions?
Sending it as a parameter to all of them isn't always nice.
So, we can declare it global and it will be usable in every function of the program.
You can declare a global variable in global space (outside any function) using the keyword var
folowed by the variable's name. Doing so, it will create a ZERO global variable which
the programmer can initialize and change later, in any function, or even in global space.
A global variable can be declared anywhere in the global space, even after the functions where it's used.
To avoid confusions, global variables can be named using the "g_" prefix
as a simple convention.
Here is an example of global variables.
More to it, you can initialize global variables and even have code in the global space,
like in any function. This code will be listed in a special function and executed in order,
right after compilation.
Here is an example of global code.
In GS9 language, constant are stored as constant global variables with values set during compilation.
Their values can't be changed later.
As a convention, they are written in upper case, so the reader knows they are not usual variables.
Here's an example:
If in a program where you often use the application's title, or a specific value, it is recomended to keep it in such a define.
This way, if at some point you decide to change it, you won't have to do it in every place you used it.
9.3 Advanced access
As mentioned before, when declaring a function a global variable with that name is declared, addressing
the function content. This variable is a constant and can't be changed, but you can declare another
function variable to point to it.
Here is an example of using a function variable.
The processor associates each global variable (or function) an internal integer ID.
Usually you access functions and global variables in a program by their given names, but internaly,
the processor converts those names into ids to use them.
Of course if you try to access an undeclared function, the processor will not be able to find
a valid id for it's name and will report an error.
This is GS9 language specific behaviour.
GS9 allows you to use these internal ids for advanced access to functions and global variables.
To obtain the internal id of a global variable use gs_gid( variable_name )
To get the value of a global varaible by it's internal id use gs_gget( id )
To set the value of a global varaible by it's internal id use gs_gset( id )
To get the name of a global varaible by it's internal id use gs_gname( id )
Here is an example of such advanced access.
You probably wonder why bother with these internal ids when you could just
use the names of the variables or functions to access them.
Indeed it doesn't makes much sense now, unless used in a specific context.
Imagine you want to write a program that, given a numeric value as input,
it will call a function named callback_1,
callback_2, ... or callback_100, depending on that input.
Since the user can give any numeric value, it's not recommended to use thousands of "if"s instructions.
Instead, you can form a string with the function name, and seek for a function id associated with that name.
If the function exists the id will be different than -1 and you can use it to call it.
Such functions are sometimes refered as "callbacks", or "handlers". Here is an example:
This kind of technique is specific to game engines where script functions are used to
respond to different actions of the game. The engine will look for such functions and call
them when the action requires it.