Difference between revisions of "Globals"
m |
Thacuber2a03 (talk | contribs) (it's a map, "globals isa map" returns 1) |
||
Line 1: | Line 1: | ||
− | '''globals''' is a built-in | + | '''globals''' is a built-in map that references [[variables]] at the global scope. Any assignment outside of any function creates (or updates) an entry in the '''globals''' map. |
Inside a function, an unqualified assignment (i.e. an assignment to an identifier that does not follow a dot — like ''x'' rather than ''foo.x'') creates a local variable. Local variables exist only within the function where they are created, and disappear as soon as the function exits. This is true even if there happens to already be a global variable of the same name. | Inside a function, an unqualified assignment (i.e. an assignment to an identifier that does not follow a dot — like ''x'' rather than ''foo.x'') creates a local variable. Local variables exist only within the function where they are created, and disappear as soon as the function exits. This is true even if there happens to already be a global variable of the same name. | ||
− | So, when you want to create or update a global variable inside a function, the '''globals''' | + | So, when you want to create or update a global variable inside a function, the '''globals''' map must be used on the left-hand side of the assignment. |
See also: [[locals]]; [[outer]]. | See also: [[locals]]; [[outer]]. | ||
== Example == | == Example == | ||
− | The ''check'' function below compares its parameter to a global variable. The comparison does not need the '''globals''' | + | The ''check'' function below compares its parameter to a global variable. The comparison does not need the '''globals''' map, because it's only reading the value (and there is no local variable of the same name). But when it updates the global value, it has to use '''globals'''. |
<ms>lastValue = null | <ms>lastValue = null |
Revision as of 20:03, 9 September 2022
globals is a built-in map that references variables at the global scope. Any assignment outside of any function creates (or updates) an entry in the globals map.
Inside a function, an unqualified assignment (i.e. an assignment to an identifier that does not follow a dot — like x rather than foo.x) creates a local variable. Local variables exist only within the function where they are created, and disappear as soon as the function exits. This is true even if there happens to already be a global variable of the same name.
So, when you want to create or update a global variable inside a function, the globals map must be used on the left-hand side of the assignment.
Example
The check function below compares its parameter to a global variable. The comparison does not need the globals map, because it's only reading the value (and there is no local variable of the same name). But when it updates the global value, it has to use globals.
lastValue = null
check = function(someValue)
if someValue != lastValue then
print "The value is now: " + someValue
globals.lastValue = someValue // <--- Note use of globals
end if
end function
x = 0
while true
check round(x)
x = x + 0.1
wait 0.1
end while
Also note that the while loop at the end is at the global scope, so it does not need to use globals at all; globals and locals are the same in this case.