sarogahtyp 1109 Posted May 19, 2016 Hey guys,I just crawled the wiki for usefull commands and found apply in it. Correct me if Im wrong but I think its doing nearly the same as forEach if u not need that _forEachIndex variable. Is there another difference which i ve not seen and did anybody measure which command is faster? Share this post Link to post Share on other sites
Fiddi 68 Posted May 19, 2016 As far as I can tell there seem to be an extremely minimal difference, with apply being 0.001 ms faster, with this code: {_x setVelocity [0,0,5]} count [HURON, Kart1, kart2, Quad1, Quad2]; [HURON, Kart1, kart2, Quad1, Quad2] apply {_x setVelocity [0,0,5]}; 1 Share this post Link to post Share on other sites
Greenfist 1863 Posted May 19, 2016 The purpose of apply is that it returns the resulting array, while forEach and count don't. 4 Share this post Link to post Share on other sites
cuel 25 Posted May 19, 2016 Apply is inherited from a concept in functional programming, namely functor. Normally it's called map http://learnyouahaskell.com/making-our-own-types-and-typeclasses#the-functor-typeclass It takes a function from one type to another and a list of one type and returns a list of another type Or in SQF "it takes an array and code, applies code to each element and returns a new array" E.g _playerUIDS = allPlayers apply {getPlayerUID _x}; // ["12345"; "54321"] It can also be chained which is where it's real power comes in, in combination with select (which is often called filter in programming languages) _alivePlayersUID = allPlayers select {alive _x} apply {getPlayerUID _x}; // apply {} select {} apply {}; This can be done indefinitely. So no, apply is not like forEach and count. If you're not interested in the resulting array, use count or forEach 1 Share this post Link to post Share on other sites
sarogahtyp 1109 Posted May 20, 2016 thanks a lot for ur responses. Now I think I ve better understanding of the differences of those commands. @cuel could u explain this line a little bit more please? _alivePlayersUID = allPlayers select {alive _x} apply {getPlayerUID _x}; // apply {} select {} apply {}; I ve problems to track the processing order. But Ill try it :-) Correct me if I m wrong. that line first creates an array of all players (from allPlayers) which r alive and then apply get those players UID and throws it in the array _alivePlayersUID. should be the same as: _alivePlayersUID = allPlayers apply {if(alive _x) then {getPlayerUID _x};}; Share this post Link to post Share on other sites
Greenfist 1863 Posted May 20, 2016 could u explain this line a little bit more please? _alivePlayersUID = allPlayers select {alive _x} apply {getPlayerUID _x}; // apply {} select {} apply {}; I ve problems to track the processing order. But Ill try it :-) Correct me if I m wrong. that line first creates an array of all players (from allPlayers) which r alive and then apply get those players UID and throws it in the array _alivePlayersUID. should be the same as: _alivePlayersUID = allPlayers apply {if(alive _x) then {getPlayerUID _x};}; That's right. The old way before apply and the current select was usually _alivePlayersUID = []; { if (alive _x) then {_alivePlayersUID pushpack (getPlayerUID _x)} } foreach allPlayers; 1 Share this post Link to post Share on other sites
killzone_kid 1333 Posted May 20, 2016 thanks a lot for ur responses. Now I think I ve better understanding of the differences of those commands. @cuel could u explain this line a little bit more please? _alivePlayersUID = allPlayers select {alive _x} apply {getPlayerUID _x}; // apply {} select {} apply {}; I ve problems to track the processing order.But Ill try it :-) Correct me if I m wrong. that line first creates an array of all players (from allPlayers) which r alive and then apply get those players UID and throws it in the array _alivePlayersUID. should be the same as: _alivePlayersUID = allPlayers apply {if(alive _x) then {getPlayerUID _x};}; Do both return the same results? Dead easy to verify, and there will be your answer. Share this post Link to post Share on other sites
sarogahtyp 1109 Posted May 20, 2016 Do both return the same results? Dead easy to verify, and there will be your answer. Thats true but I cant do that at work. I just try to understand things better without testing it. Share this post Link to post Share on other sites
serena 151 Posted May 20, 2016 Do both return the same results? When players 2 and 4 not alive: allPlayers select {alive _x} apply {getPlayerUID _x}; //result is [Player_1_UID, Player_3_UID, Player_5_UID] allPlayers apply {if(alive _x) then {getPlayerUID _x};}; //result is [Player_1_UID, null, Player_3_UID, null, Player_5_UID] 2 Share this post Link to post Share on other sites
cuel 25 Posted May 20, 2016 thanks a lot for ur responses. Now I think I ve better understanding of the differences of those commands. @cuel could u explain this line a little bit more please? _alivePlayersUID = allPlayers select {alive _x} apply {getPlayerUID _x}; // apply {} select {} apply {}; I ve problems to track the processing order. But Ill try it :-) Correct me if I m wrong. that line first creates an array of all players (from allPlayers) which r alive and then apply get those players UID and throws it in the array _alivePlayersUID. should be the same as: _alivePlayersUID = allPlayers apply {if(alive _x) then {getPlayerUID _x};}; Not really, select is used to filter array elements, while apply is normally used to transform the array elements into something else. Select can return less elements while apply will ways return the same amount of elements What I wrote can be explained like this _plrs = allPlayers; _alivePlrs = _plrs select {alive _x}; // we filter out dead players. the resulting array can be smaller _alivePlrsUID = _alivePlrs apply {getPlayerUID _x}; // returns an array of playerUids for each element in the _alivePlrs 1 Share this post Link to post Share on other sites