Jump to content

Recommended Posts

There is a thisList passed from a trigger to a function.  The function assumes it is an array, as it should be according to trigger documentation.  But I'm getting an error saying it is an object.

civCred = { // check player credibility as civilian
	params["_civList"];
	{
		if ("" != primaryWeapon _x) then {
			_x call joinRes;
		}
		else { if ("" != secondaryWeapon _x) then {
			_x call joinRes;
		}
		else { if ("" != handgunWeapon _x) then {
			_x call joinRes;
		}}};
	} forEach _civList;
};
area setTriggerStatements [
	"this",
	"thisList call civCred",
	"hint 'trigger off'"
];
Spoiler

 1:18:34 Error in expression <apon _x) then {
_x call joinRes;
}}};
} forEach _civList;
};
airport1 setTrigger>
 1:18:34   Error position: <forEach _civList;
};
airport1 setTrigger>
 1:18:34   Error foreach: Type Object, expected Array
 1:18:34 File H:\Storage\Win7-64\C - Home Basic\Arma 3\missions\Checkpoints.Altis\init.sqf..., line 31
 1:18:34 Error in expression <apon _x) then {
_x call joinRes;
}}};
} forEach _civList;
};
airport1 setTrigger>
 1:18:34   Error position: <forEach _civList;
};
airport1 setTrigger>
 1:18:34   Error Generic error in expression
 1:18:34 File H:\Storage\Win7-64\C - Home Basic\Arma 3\missions\Checkpoints.Altis\init.sqf..., line 31

 

Share this post


Link to post
Share on other sites

You're already parsing an array into the function (thisList is an array, yes). Then, by using params, you're selecting the first argument of that array (being whatever unit is first in the array) and trying to use that unit as the forEach loop. The trigger works as it should, and the easy way to fix your code is:

 

"thisList call civCred" //old
"[thisList] call civCred" //new

OR

params["_civList"]; //old
private _civList = _this; //new

 

Pick one, but not both.

Share this post


Link to post
Share on other sites

What is [] in an argument?  That is not documented anywhere.

Share this post


Link to post
Share on other sites

What do you mean? That's just an empty array.

Share this post


Link to post
Share on other sites
5 hours ago, beno_83au said:

 


"thisList call civCred" //old
"[thisList] call civCred" //new

 

 

Share this post


Link to post
Share on other sites

That's an array. Long story short, params["_civList"]; in your script picks the first element of the array, which is thislist. That's why it won't work, so use beno_83au's either solution.

Share this post


Link to post
Share on other sites

It sinks in now.  Passing it in array makes it index=0 instead of array.  SQF is very confusing coming from Java.  I thought params was like the para definition in void myMethod(Object[] myArray).

Share this post


Link to post
Share on other sites

That page does not explain what the elements are.

Share this post


Link to post
Share on other sites

Can be anything.  An array can be viewed as a list and can contain any data types, including nested arrays (lists).

 

When your trigger runs "thisList call civCred", it is passing an array (thisList) to your civCred function, which civCred receives as _this.  The elements within the array are objects in the trigger.

 

When civCred runs "params["_civList"];" on the received list _this, it is taking the first element from the list and defining it as _civList.  civCred then runs forEach on that element, not the array.  That's why you get the object error, the element is an object from the list, not the list itself.

 

But civCred needs to run the forEach on _this, the received list itself.  The forEach then cycles each element of the array.

 

Hope that's more clear.

  • Like 1

Share this post


Link to post
Share on other sites
4 hours ago, Sertica said:

That page does not explain what the elements are.

It literally does just that, in the first sentence.

Share this post


Link to post
Share on other sites
1 hour ago, Harzach said:

It literally does just that, in the first sentence.

I put you on my ignore list.

 

3 hours ago, opusfmspol said:

Can be anything.  An array can be viewed as a list and can contain any data types, including nested arrays (lists).

 

When your trigger runs "thisList call civCred", it is passing an array (thisList) to your civCred function, which civCred receives as _this.  The elements within the array are objects in the trigger.

 

When civCred runs "params["_civList"];" on the received list _this, it is taking the first element from the list and defining it as _civList.  civCred then runs forEach on that element, not the array.  That's why you get the object error, the element is an object from the list, not the list itself.

 

But civCred needs to run the forEach on _this, the received list itself.  The forEach then cycles each element of the array.

 

Hope that's more clear.

I have a long time programming with Java.  I know arrays.  But params is baffling.  After staring and drooling for a while I think it may have clicked.  It sees the entire function argument as 1 array.  So a single array needs to be wrapped inside another array before passing to function. 😵

  • Haha 2

Share this post


Link to post
Share on other sites
6 hours ago, Sertica said:

So a single array needs to be wrapped inside another array before passing to function.

 

 

 

Params command always applies the alternative syntax.  When using main syntax, it uses _this as argument for the alternative syntax.  And when _this is not an array it gets placed into an array, but if already an array it remains as is.  So yes, you wrap a single array argument when you want the given variable defined with the array intact.  When you want the array's element(s) assigned to given variable(s), you don't wrap.

 

 

 

Share this post


Link to post
Share on other sites

params just parses the array you give to it (or that it grabs from _this) according to the parameters you specify.

if you give it an array, and one parameter, it takes the first element of that array as parameter.

thus if you pass thisList, the _civList is the first element in the array, not the array itself.

 

This is literally explained down in the notes on the wiki page, by me, 3,5 years ago.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×