Custom function scope adjustments

Some adjustments made to the function scope documentation to make it easier for people to understand. This came up in Discord where some of us understood it but others did not and I think the way I changed it will make it a little cleaner
This commit is contained in:
JJ 2018-02-20 14:44:28 -05:00 committed by GitHub
parent ba6ed1f6b6
commit 6713dca1d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -864,27 +864,7 @@ The example above prints the following in its log::
**Note about variable scope in functions:**
Functions can access "global" variables declared outside of the function's scope. However, they cannot change the value of any "global" variables.
Any changes to "global" variables will only be applied locally to the function. This also means that any variable that is first defined inside a
function will NOT be accessible outside of the function.
For example, the following code::
function sum(values) {
res = 0;
for (i = 0; i < values.length; ++i) {
res += values[i];
}
return res;
}
print(res);
results in the following runtime error::
Script runtime error:
Server Ip: 75.7.4.1
Script name: test.script
Args:[]
variable res not defined
Any changes to "global" variables will only be applied locally to the function.
The following example shows that any change to "global" variable inside a function only applies in the function's local scope::
@ -903,6 +883,27 @@ Results in the following log::
0
0
Furthermore, this also means that any variable that is first defined inside a function will NOT be accessible outside of the function as shown in the following example::
function sum(values) {
res = 0;
for (i = 0; i < values.length; ++i) {
res += values[i];
}
return res;
}
print(res);
results in the following runtime error::
Script runtime error:
Server Ip: 75.7.4.1
Script name: test.script
Args:[]
variable res not defined
**Other Notes about creating your own functions:**
Defining a function does not create a Javascript function object in the underlying game code. This means that you cannot use any function