Each variable has a specified scope when it is declared. The scope affects where in the source code it is visible and where it can be read or written to. For every scope except for “Project” scope, space is actually allocated for the variable when declared.
File | This describes a variable that is visible only inside the file it is declared in. The declaration for this is “global foo;”. |
Project | This scope implies the variable is visible anywhere after seeing the declaration. Note, that this form of declaration does not allocate space for a File scoped variable, so you’ll need a File scope declaration somewhere in your source. The declaration for this is “extern global foo;”. |
Block | This scope is what’s traditionally referred to as a “local” scope. The variable is visible only within the curly braces defining the statement block in which it was declared. This scope will hide any global variable with the same name. Procedure parameters are considered to have Block scope. The declaration for this is “local foo;”. |
You can see several methods of declaration in the table above, however a few things need to be explained. First, to use a variable, it must be declared. In declaring a variable, you tell Rapture to allocate space in memory for the data that the variable will hold and also to give that space a name. All variables in Rapture are initialized to default values at different times depending on the scope of the variable. Global variables are initialized at load (and re-load) time, and locals upon entering the block they were declared in. To declare variables, you simply use the global or local keywords followed immediately by a comma separated list of variable names.
Example
(* Declare some external variables (typically included in header files) *)
extern global myglobal;
(* Declare some 'private' variables *)
global x, y, z;
(* Make sure we declare the previous extern once. *)
global myglobal;
(* Now let's declare some locals inside a procedure *)
subroutine main()
{
local a, b, x;
(* Note that the local 'x' hides the global 'x' while inside
this subroutine's statement block. *)
}
The above example also contains a subroutine definition, but it is present only to show how (and where) local variables are declared.