count – Talk
(code examples) |
Lou Montana (talk | contribs) m (Text replacement - "<code>" to "<code style="display: block">") |
||
(6 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
The notes mention using '''{alive _x} count units''' to check how many are actually alive, but I don't think 'alive' updates any faster than the group, i.e. "alive unit" returns true until the leader discovers the unit is dead. So, a more accurate count can be obtained with | The notes mention using '''{alive _x} count units''' to check how many are actually alive, but I don't think 'alive' updates any faster than the group, i.e. "alive unit" returns true until the leader discovers the unit is dead. So, a more accurate count can be obtained with | ||
<code>{damage _x < 1} count units</code> | <code style="display: block">{damage _x < 1} count units</code> | ||
--[[User:Nomdeplume|nomdeplume]] 10:24, 4 January 2010 (CET) | --[[User:Nomdeplume|nomdeplume]] 10:24, 4 January 2010 (CET) | ||
:Actually, 'units' is updated much slower than 'alive', but independently of that, the point of the examples isn't really to show an "optimal" piece of code, but rather to just demonstrate the syntax. A lot of the examples don't make any sense in a real-life application - they were just chosen because they were brief and got the point across. --[[User:Kronzky|Kronzky]] 16:17, 4 January 2010 (CET) | :Actually, 'units' is updated much slower than 'alive', but independently of that, the point of the examples isn't really to show an "optimal" piece of code, but rather to just demonstrate the syntax. A lot of the examples don't make any sense in a real-life application - they were just chosen because they were brief and got the point across. --[[User:Kronzky|Kronzky]] 16:17, 4 January 2010 (CET) | ||
== "exitWith" inside a "count" loop overwrites the count result == | |||
As I tested my favorite way of using ''count'' in a trigger condition, I got a little confused. So this is what I often do: | |||
<code style="display: block">if({if('''_x fulfills condition''') exitWith {true}; false} '''count''' _array isEqualTo 1) then | |||
<nowiki>{ | |||
//do whatever here | |||
};</nowiki></code> | |||
(Example from ''count'' BIKI page, now corrected) | |||
I just realized that this does not seem to work with ''isEqualTo 1'' anymore as the count now returns '''true''' instead of '''1''' as it used to do. | |||
So it seems, the statement instead has to be something like this: | |||
<code style="display: block">if({if('''_x fulfills condition''') exitWith {'''1'''}; false} '''count''' _array isEqualTo 1) then | |||
<nowiki>{ | |||
//do whatever here | |||
};</nowiki></code> | |||
But I'm wondering why that is as count supposedly counts only elements which have a ''true'' condition. But using an '''exitWith''' inside the condition seems to break through that habit and set the result of the '''count''' as the '''exitWith''' value. | |||
--[[User:Heeeere's_Johnny!|Johnny]] | |||
== Non-bool interior result == | |||
Here is another example to further clarify my note: | |||
<code style="display: block">{ _x createVehicleLocal (getPos player); } count ["Rabbit","Goat","Sheep"];</code> | |||
This throws "Error Type Object, expected Bool" because [[createVehicleLocal]] returns an object. To avoid this a bool can be added at the end of the loop like so: | |||
<code style="display: block">{ _x createVehicleLocal (getPos player); false } count ["Rabbit","Goat","Sheep"];</code> | |||
Alternatively [[forEach]] can be used instead of count. Note exitWith does not return anything inside the loop. If it executes then it exits the loop and the return is outside of the count. | |||
[[User:Ebay|Ebay]] ([[User talk:Ebay|talk]]) 21:55, 22 August 2016 (CEST) |
Latest revision as of 11:52, 11 January 2023
The notes mention using {alive _x} count units to check how many are actually alive, but I don't think 'alive' updates any faster than the group, i.e. "alive unit" returns true until the leader discovers the unit is dead. So, a more accurate count can be obtained with
{damage _x < 1} count units
--nomdeplume 10:24, 4 January 2010 (CET)
- Actually, 'units' is updated much slower than 'alive', but independently of that, the point of the examples isn't really to show an "optimal" piece of code, but rather to just demonstrate the syntax. A lot of the examples don't make any sense in a real-life application - they were just chosen because they were brief and got the point across. --Kronzky 16:17, 4 January 2010 (CET)
"exitWith" inside a "count" loop overwrites the count result
As I tested my favorite way of using count in a trigger condition, I got a little confused. So this is what I often do:
if({if(_x fulfills condition) exitWith {true}; false} count _array isEqualTo 1) then
{
//do whatever here
};
(Example from count BIKI page, now corrected)
I just realized that this does not seem to work with isEqualTo 1 anymore as the count now returns true instead of 1 as it used to do.
So it seems, the statement instead has to be something like this:
if({if(_x fulfills condition) exitWith {1}; false} count _array isEqualTo 1) then
{
//do whatever here
};
But I'm wondering why that is as count supposedly counts only elements which have a true condition. But using an exitWith inside the condition seems to break through that habit and set the result of the count as the exitWith value.
--Johnny
Non-bool interior result
Here is another example to further clarify my note:
{ _x createVehicleLocal (getPos player); } count ["Rabbit","Goat","Sheep"];
This throws "Error Type Object, expected Bool" because createVehicleLocal returns an object. To avoid this a bool can be added at the end of the loop like so:
{ _x createVehicleLocal (getPos player); false } count ["Rabbit","Goat","Sheep"];
Alternatively forEach can be used instead of count. Note exitWith does not return anything inside the loop. If it executes then it exits the loop and the return is outside of the count.