Sertica 18 Posted March 31, 2020 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'" ]; Reveal hidden contents 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
beno_83au 1369 Posted March 31, 2020 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
killzone_kid 1333 Posted March 31, 2020 forEach _this Share this post Link to post Share on other sites
Sertica 18 Posted March 31, 2020 What is [] in an argument? That is not documented anywhere. Share this post Link to post Share on other sites
POLPOX 779 Posted March 31, 2020 What do you mean? That's just an empty array. Share this post Link to post Share on other sites
Sertica 18 Posted March 31, 2020 On 3/31/2020 at 7:38 AM, beno_83au said: "thisList call civCred" //old "[thisList] call civCred" //new Share this post Link to post Share on other sites
POLPOX 779 Posted March 31, 2020 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
Sertica 18 Posted March 31, 2020 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
Harzach 2518 Posted March 31, 2020 When in doubt, look it up! https://community.bistudio.com/wiki/params Share this post Link to post Share on other sites
Sertica 18 Posted March 31, 2020 That page does not explain what the elements are. Share this post Link to post Share on other sites
opusfmspol 282 Posted March 31, 2020 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. 1 Share this post Link to post Share on other sites
Harzach 2518 Posted March 31, 2020 On 3/31/2020 at 4:54 PM, 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
Sertica 18 Posted March 31, 2020 On 3/31/2020 at 9:05 PM, Harzach said: It literally does just that, in the first sentence. I put you on my ignore list. On 3/31/2020 at 7:02 PM, 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. 😵 2 Share this post Link to post Share on other sites
opusfmspol 282 Posted April 1, 2020 On 3/31/2020 at 11:46 PM, 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
Dedmen 2723 Posted April 1, 2020 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