This is good stuff! So when you run a script asynchronously, you said you can not access the returning value of the function. In order to utilize the function then, does that mean your only option is to return the function to a global variable in order to access it?
Ah, this makes sense to me now. So the only variables that basically "count" are object members and the return values of functions.
That's an odd behavior, but i suppose it logically makes sense then. Thanks for the info on that. You're just a wealth of information. [EDIT] Ah, got it. A little OT but still relevant.
#include <iostream>
using namespace std;
int main()
{
int x = 5;
cout << "Local to main: " << x << endl;
{
cout << "Test block: " << x << endl;
int x = 10;
cout << "Local to block: " << x << endl;
}
cout << "Back to main: " << x << endl;
return 0;
}
This of course returns 5, 5, 10, and then 5 again. That's really cool - I wish they had covered that in my CET 300s courses.
To store the returning value of a function you can use both a global variable and a local variable. The syntax is the following:
_myVariable = [arguments] call compile preprocessFile myFunction.sqf
the preprocessFile command enables the use of C++ preprocessor commands (like comments with // and /*, or #define statements), the compile command compiles the file and call executes the code returned by the compile command.
To properly build a function (that is a script returning a value) you MUST use the SQF syntax and the last line of the script is the returned value and it is not followed by ";" . For example this function computes the sum of two numbers
_num1 = _this select 0;
_num2 = _this select 1;
_res = _num1 + _num2
Remember that the last line can be an expression or a simple variable. If there are code blocks executed in an exclusive way like the if-then-else then you can put a returning value for each block (since one of them is never executed in every instance of the script). For example this script returns the maximum between two values
_val1 = _this select 0;
_val2 = _this select 1;
if (_val1 > _val2) then
{
_val1
}
else
{
_val2
};
Note that there are two returned lines but it is correct because just one of them is executed. You can do the same for the other controls structures but remember that there must always be onnly one returned value.
The spawn command syntax is the same of the call command except that you can't access the value of the function. This is because the spawn command already returns the "script handler", that is a sort of script ID you can use to synchronize scripts with the command "scriptDone". The code is:
[arguments] spawn compile preprocessFile "myFunction.sqf"
or
_scriptHandler = [arguments] spawn compile preprocessFile "myFunction.sqf"
NOTE: If you use call or spawn for a function local to the script, i.e. defined with a code block variable, you don't to have to use "compile" and "preprocessFile" because the variable already contains a code block and not a text file which must be compiled first.
C++ OT:
Yeah, basically variables that counts outside a context (or a scope) are function returned values and object member variables. The last one are different because you normally declare them as "private" so the user can't access them but with the class public functions. But the idea is that.
Nevertheless there's a difference between memory automatic allocation and dynamic allocation. The dynamic allocated variables are declared with the "new" instance and exists until you explicitly delete them with the "delete" instance. Dynamic allocated variables are used for example to define arrays whith a custom size, that is you don't to have to use a constant value to specify their size but you can use a variable. Usually an automatic array (the one with the size specified by a constand value) are declared like this:
int myArray[30]
if you want the size of the array to be a custom input value for example you must declare a dynamic array like this>
int size;
cin >> size;
int *myArray = new int[size];
where myArray is an integer pointer.
This is just to make you understand that for dynamic variables the scope rules do not apply. The array will exist until the program ends or you delete it with the command:
delete[] myArray;