OFPEC Forum
Editors Depot - Mission Editing and Scripting => OFP - Editing/Scripting General => Topic started by: Razorwings18 on 16 Mar 2003, 07:13:23
-
Trying to learn how to use functions, I did the following:
getunitattrib.sqf
_private = ["_aUNITINFO", "_attrib", "_result"];
_aUNITINFO = _this select 0;
_attrib = _this select 1;
_result = 233;
_result;
init.sqs
getUnitAttrib = preprocessfile "getunitattrib.sqf";
private ["_thisunitpos"];
_thisunitpos = [(aBLUEUNITS select 5), "GROUP"] call getUnitAttrib;
hint format["%1", _thisunitpos];
_thisunitpos gets assigned no value (returns "scalar bool array..." when hinted), instead of the 233 I'm expecting. As you can see there're variables passed to the function, but they serve no purpose right now since I "downsized" the function to make sure I'm making no OTHER mistake.
What am I doing wrong? Why don't I get the 233 returned?
-
Hi,
simply remove the semicolon after _result:
_private = ["_aUNITINFO", "_attrib", "_result"];
_aUNITINFO = _this select 0;
_attrib = _this select 1;
_result = 233;
_result
This should work.
As a sidenote, I think you never need the private command in scripts (as you use in init.sqs). It is only used in functions (or such constructs as forEach, while, if) to define the scope of variables as private.
-
Thanks for your reply, Spinor, that worked just right. As a matter of fact I do use conditionals and loops within the script, and since what I'm doing is a pretty long and complicated process, I prefer to declare all local variables involved rather than only those used within the control structures.
As for functions, now I'm trying to do what I had initially intended, but I ran into trouble again.
getunitattrib.sqf
_private = ["_aUNITINFO", "_attrib", "_result"];
_aUNITINFO = _this select 0;
_attrib = _this select 1;
if (_attrib == "GROUP") then
{
_result = (_aUNITINFO select 0);
};
_result
init.sqs
_coso = [(aBLUEUNITS select 3), "GROUP"] call getUnitAttrib;
hintc format["%1", _coso];
(aBLUEUNITS select 3) being an array containing a pointer to a group in its first element (position 0).
Again, _coso is assigned nothing. The problem is in the conditional, since when I put the "_result = (_aUNITINFO select 0);" line out of it, it works perfectly.
-
I only just discovered that your usage of the private command is not correct. It is
private ["_aUNITINFO", "_attrib", "_result"];rather than
_private = ["_aUNITINFO", "_attrib", "_result"];Even with that correction, you may get an error with your latest function because you define _result only within the "then" construct. To be on the save side you should initialise _result before the if with something like
_result = 0;(You need to define _result anyway in case the if yields false). This is from the official COMREF:
Local variable is any variable which name starts with underscore. All other variables are global.
Each of commands then, do, while, forEach, count, exec, call defines a visibility scope for local variables. All local variables from outer scopes are visible as well. If assignment is made into a variable that does not exist in any visible scope, it is created in the innermost scope. You can use function private to introduce variable at any given scope.
-
Yep, the problem was my use of Private. I had actually copied that line from a function found in the OFPEC functions section, so I assumed it to be right.
I can now say that I'm a function-enabled OFP scripter :D
Thanks for all your help.