What are the differences between a variable and a global variable?

Jump to navigation Jump to search

Well, basically, a global variable is a variable that can be used anywhere.


A global variable has global "scope". Scope is a term that refers to where, exactly, a variable can be used. In HamsterSpeak, there are two scopes: Global and Local. Global means that the variable can be seen and used in any script, where as Local means that the variable can only be used in the script it's defined in.

Method of Declaration[edit]

A global variable is defined with a global variable command:

global variable(1,Foo)
global variable(2,Bar)

However, something many people don't know is that there's a slightly different syntax for globals (and scripts, and constants, etc):

global variable, begin
  1, Foo
  2, Bar

If you have 100 globals, this way of writing it can be easier.

A local variable, however, is defined completely differently:

script, FooBar, begin

Here, a variable baz is defined. It can only be used inside of FooBar, nowhere else. If you define baz somewhere else, it'll be totally separate from this baz.

Parameters to a script are, in fact, just local variables. The only difference is that they already have values. In fact in fact, until recently, if there weren't enough parameters defined, the extra values would overflow into the first couple of variables, since that's all parameters are.

See Also[edit]