Home   Help Search Login Register  

Author Topic: Testing functions... can't get a return value  (Read 657 times)

0 Members and 1 Guest are viewing this topic.

Offline Razorwings18

  • Contributing Member
  • **
  • I'm NOT a llama. I checked.
Testing functions... can't get a return value
« on: 16 Mar 2003, 07:13:23 »
Trying to learn how to use functions, I did the following:

getunitattrib.sqf
Code: [Select]
_private = ["_aUNITINFO", "_attrib", "_result"];
_aUNITINFO = _this select 0;
_attrib = _this select 1;

_result = 233;
_result;

init.sqs
Code: [Select]
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?

Offline Spinor

  • Members
  • *
  • I'm a llama!
    • The chain of command
Re:Testing functions... can't get a return value
« Reply #1 on: 16 Mar 2003, 18:36:00 »
Hi,

simply remove the semicolon after _result:
Quote
_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.

Offline Razorwings18

  • Contributing Member
  • **
  • I'm NOT a llama. I checked.
Re:Testing functions... can't get a return value
« Reply #2 on: 17 Mar 2003, 01:10:59 »
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
Code: [Select]
_private = ["_aUNITINFO", "_attrib", "_result"];
_aUNITINFO = _this select 0;
_attrib = _this select 1;

if (_attrib == "GROUP") then
{
       _result = (_aUNITINFO select 0);
};
_result

init.sqs
Code: [Select]
_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.
« Last Edit: 17 Mar 2003, 01:12:46 by Razorwings18 »

Offline Spinor

  • Members
  • *
  • I'm a llama!
    • The chain of command
Re:Testing functions... can't get a return value
« Reply #3 on: 17 Mar 2003, 01:59:26 »
I only just discovered that your usage of the private command is not correct. It is
Code: [Select]
private ["_aUNITINFO", "_attrib", "_result"];rather than
Code: [Select]
_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
Code: [Select]
_result = 0;(You need to define _result anyway in case the if yields false). This is from the official COMREF:
Quote
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.

Offline Razorwings18

  • Contributing Member
  • **
  • I'm NOT a llama. I checked.
Re:Testing functions... can't get a return value
« Reply #4 on: 17 Mar 2003, 03:57:01 »
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.