_foley 192 Posted April 23, 2022 @Harzach Not my code 😛 Quote I am clueless and it hurts my ego to ask for a ready-to-go solution, so I would like to ask for a bit clearer explanation on where the heck I would put this total thing. The location is good - after the filtering, before the hint. Start with _sumScore = 0. Then, inside the forEach, update _sumScore to be equal _sumScore + value of the current item. 1 Share this post Link to post Share on other sites
Harzach 2517 Posted April 23, 2022 Just now, _foley said: Not my code 😛 Oops! Sorry 🤐 Share this post Link to post Share on other sites
Dj Rolnik 29 Posted April 23, 2022 3 hours ago, Harzach said: It's already looping/iterating through the array (that's what forEach does), but I think maybe @_foley forgot to add each new value to _sumScore: _sumScore = {_sumScore + (_x select 2)} forEach _presentItems; Well, I added the previous line but it was only summing the last item on the list so it was missing something. Adding what you provided spews an error that the _sumScore is undefined and when the chest1 was empty, the hint displayed a scalar NaN string as an error. Share this post Link to post Share on other sites
Dj Rolnik 29 Posted April 23, 2022 // Calculate item values _presentItems = _lootValues apply { _itemName = _x; _itemValue = _y; _itemCount = {_x isEqualTo _itemName} count magazineCargo chest1; _itemDisplayName = _lootNames get _x; [_itemName, _itemCount, _itemCount * _itemValue, _itemDisplayName] }; // Filter out missing items _presentItems = _presentItems select {_x select 1 > 0}; _sumScore = 0; _sumScore = {_sumScore + (_x select 2)} forEach _presentItems; Two problems: - It does not sum all the items in the chest, only displays the last item - if there are no items in the chest, it says <null> and says the _sumScore is undefined. Share this post Link to post Share on other sites
Harzach 2517 Posted April 23, 2022 Like @_foley said, you need to declare it earlier in your script as _sumScore = 0; Share this post Link to post Share on other sites
Dj Rolnik 29 Posted April 23, 2022 1 minute ago, Harzach said: Like @_foley said, you need to declare it earlier in your script as _sumScore = 0; It is declared ^^" Share this post Link to post Share on other sites
sarogahtyp 1108 Posted April 24, 2022 6 hours ago, Dj Rolnik said: _sumScore = {_sumScore + (_x select 2)} forEach _presentItems; has to be { _sumScore = _sumScore + (_x select 2) } forEach _presentItems; 2 Share this post Link to post Share on other sites
Dj Rolnik 29 Posted April 24, 2022 Ok yes, that was it. Gosh I didn't think about bracketing the whole thing, my bad there... I did not fully understand the idea of addition of a variable to itself, you know, the x = x + something. I think I get it now: it simply returns the _itemCount * _itemValue for the corresponding item, then runs it again for all other items in the _presntItems array except the variable _sumScore is already a new value based on the previous calculation so it adds up the new corresponding points value. Thank you for that guys, I really appreciate it. I know I may be a slow learner, but I am really trying ">.> Share this post Link to post Share on other sites
Harzach 2517 Posted April 24, 2022 2 hours ago, Dj Rolnik said: my bad there Nah, I gave you bad code. I need to not Arma when I'm tired. Glad it's starting to come together for you! Share this post Link to post Share on other sites
Dj Rolnik 29 Posted April 26, 2022 Yo, Coming back at you with a different topic now. I am looking to convert the results of the following code into an array. I am planning to select nine random items from the below array and from then I will pull those items for other purposes. I found the bottom piece of code done for a similar purposes but it was displaying the results as systemChat messages, while I need an array of results. I tried using the toArray command but to no avail. I also tried using the compile command but no luck as well. Any suggestions on how to approach it? _mercTargets = [ loner_1, loner_2, loner_3, loner_4, loner_5, loner_6, loner_7, loner_8, loner_9, loner_10, loner_11, loner_12, duty_1, duty_2, duty_3, duty_4, bandit_1, bandit_2, bandit_3, bandit_4, sky_1, sky_2, sky_3 ]; for "_i" from 0 to 8 do { if (count _mercTargets == 0) exitWith {}; private _rindex = floor(random (count _mercTargets)); private _randSel = _mercTargets # _rindex; toArray hint str (_randSel); _mercTargets deleteAt _rindex; }; Cheers! Share this post Link to post Share on other sites
sarogahtyp 1108 Posted April 26, 2022 private _mercTargets = [ loner_1, loner_2, loner_3, loner_4, loner_5, loner_6, loner_7, loner_8, loner_9, loner_10, loner_11, loner_12, duty_1, duty_2, duty_3, duty_4, bandit_1, bandit_2, bandit_3, bandit_4, sky_1, sky_2, sky_3 ]; private _randSel = []; // do 9 times for "_i" from 1 to 9 do { private [ "_temp", "_d" ]; // select one element randomly _temp = selectRandom _mercTargets; //find the selected element in array and delete it (there are many other ways doing this) _d = _mercTargets deleteAt ( _mercTargets findIf { _x isEqualTo _temp }; ); //push the selected element into an array _d = _randSel pushBack _temp; }; // now you have the array _randSel which contains 9 random elements out of _mercTargets not tested cause I ve no arma available this week 1 Share this post Link to post Share on other sites
Larrow 2822 Posted April 26, 2022 private _randSel = []; // do 9 times for "_i" from 1 to 9 do { // private _index = floor random count _mercTargets; // private _selected = _mercTargets deleteAt _index; // _randSel pushBack _selected; //Or just... _randSel pushBack ( _mercTargets deleteAt floor random count _mercTargets ); }; // now you have the array _randSel which contains 9 random elements out of _mercTargets private _msg = "Selected...\n"; { _msg = format[ "%1%2\n", _msg, _randSel # _forEachIndex ]; }forEach _randSel; hint _msg; 1 Share this post Link to post Share on other sites
Dj Rolnik 29 Posted April 26, 2022 Yes, both work just fine, thank you gentlemen! Didn't think it would be so simple. I am having some trouble figuring out what the heck happened though and I'd like to understand. So what I get is that the bracketed part deletes an element of the _mercTargets array selected randomly by the latter part of the bracket. The pushBack is confusing to me, but what I get is it pushes the removed item to the back of the array (?). That is then done 9 times so I understand that in the end the array still contains the same amount of items, but simply rearranged, so how does it produce just nine items? Thanks! Share this post Link to post Share on other sites
Larrow 2822 Posted April 26, 2022 31 minutes ago, Dj Rolnik said: bracketed part deletes an element of the _mercTargets array selected randomly by the latter part of the bracket. it pushes the removed item to the back of the array correct 31 minutes ago, Dj Rolnik said: in the end the array still contains the same amount of items What array? Neither of the arrays contains their original count. _randSel starts with nothing and _mercTargets does not contain its original items as deleteAt actually deletes the item at index. 31 minutes ago, Dj Rolnik said: so how does it produce just nine items? As the for loop iterates nine times, each time picking an item out of _mercTargets and adding it to the end of _randSel. You seem to understand this in your previous sentence so perhaps I'm not understanding your question. 1 Share this post Link to post Share on other sites
Dj Rolnik 29 Posted April 26, 2022 No, I get it now. I got a bit confused and forgot about the fact that the _randSel starts as empty and mixed up both arrays, my bad! I think I got it now, it sure is clever. Thank you Larrow 🙂 Share this post Link to post Share on other sites
dreadedentity 278 Posted April 27, 2022 ...but my monkey brain can only come up with so many ways to say _pos and _dir just kidding; another benefit of private is that it allows us to make recursive functions in SQF which wouldn't really be possible otherwise due to it's nature Share this post Link to post Share on other sites
Dj Rolnik 29 Posted May 20, 2022 Hey y'all, It's been a while since I've posted. I have been chugging along with the project, and mananged to get it pretty close to what I have in mind. I am currently struggling with randomizing some stuff spawning. What I want to do is: 1. Spawn a random anomaly module on a randomly selected marker 2. Have the anomaly spawn in a certain random vicinity from the marker (say +/- 15m, not on its exact position) I have done something like this before but I am mingling some things and cannot get it fully to work this time. What I have is the below, and when I run it, it only spawns in THREE anomalies instead of FIVE, and they always spawn on the exact position of the marker. I am pretty certain that the fact only three are spawning is due to the order in which code is being run, but the random placement, I have not yet been able to figure out. Thanks for your help in advance! // Anomaly placement markers anomaly_spawners = [ "ra_1", "ra_2", "ra_3", "ra_4", "ra_5" ]; // Available Anomalies anomalies = [ "anoamly_moduleElectra", // Electra "anoamly_moduleBurner", // Burner "anoamly_moduleFruitPunch", // Fruit Punch "anoamly_moduleMeatgrinder", // Meatgrinder "anoamly_moduleSpringboard" // Springboard ]; // Generating random anomalies losowe_anomalie = createGroup sideLogic; { randomAnomalies = selectRandom anomalies; randomSpawners = selectRandom anomaly_spawners; randomAnomalies createUnit [ getMarkerPos randomSpawners, losowe_anomalie, "this setVariable ['BIS_fnc_initModules_disableAutoActivation', false, true];" ]; anomaly_spawners deleteAt (anomaly_spawners find randomSpawners) } forEach anomaly_spawners; Share this post Link to post Share on other sites
_foley 192 Posted May 20, 2022 It looks like you threw every command you had at the anomaly_spawners 😀 You need to clarify what you mean by "Spawn a random anomaly module on a randomly selected marker". Do you want exactly one anomaly at each of the 5 markers or should it be possible for many to spawn on the same marker? Do you want each anomaly type to spawn exactly once or should it be possible to spawn the same type many times? Regarding spawning units in random vicinity of a marker, you can take the marker position (using getMarkerPos) and then pick a point that's in random direction and a random distance away from it. Just so happens that this is exactly what "Syntax 3" in getPos allows you to do when you combine it with random 1 Share this post Link to post Share on other sites
Dj Rolnik 29 Posted May 20, 2022 18 minutes ago, _foley said: It looks like you threw every command you had at the anomaly_spawners 😀 You need to clarify what you mean by "Spawn a random anomaly module on a randomly selected marker". Do you want exactly one anomaly at each of the 5 markers or should it be possible for many to spawn on the same marker? Do you want each anomaly type to spawn exactly once or should it be possible to spawn the same type many times? Regarding spawning units in random vicinity of a marker, you can take the marker position (using getMarkerPos) and then pick a point that's in random direction and a random distance away from it. Just so happens that this is exactly what "Syntax 3" in getPos allows you to do when you combine it with random Yeah, sorry, let me clear it up a bit. I want each marker to spawn one of the five anomalies. There will probably a few dozens of those markers but for all of them I want to have only one anomaly, randomly selected from the array of five. I mean "anomaly module" because they are in fact modules in the editor, not an object, so I cannot use createVehicle. Share this post Link to post Share on other sites
Dj Rolnik 29 Posted May 23, 2022 Ok, fixed it myself while I was brainstorming this at work >.<". I was executing the code on the array and at the same time removing items from it, so it executed on one marker, removed one marker, executed on the next marker, removed the next one, and only one was left, so it just executed - this gave me three successful spawns. I simply added a separate array, the exact same as the original marker array but named different and executed the spawning code on each element of THAT NEW array. // Anomaly placement markers anomaly_spawners_main = [ "ra_1", "ra_2", "ra_3", "ra_4", "ra_5" ]; anomaly_spawners = [ "ra_1", "ra_2", "ra_3", "ra_4", "ra_5" ]; // Available anomalies anomalies = [ "anoamly_moduleElectra", // Electra "anoamly_moduleBurner", // Burner "anoamly_moduleFruitPunch", // Fruit Punch "anoamly_moduleMeatgrinder", // Meatgrinder "anoamly_moduleSpringboard" // Springboard ]; // Generating random anomalies losowe_anomalie = createGroup sideLogic; { randomAnomalies = selectRandom anomalies; randomSpawners = selectRandom anomaly_spawners; randomAnomalies createUnit [ (getMarkerPos randomSpawners) getPos [random 15, random 359], // <- THIS! losowe_anomalie, "this setVariable ['BIS_fnc_initModules_disableAutoActivation', false, true];" ]; anomaly_spawners deleteAt (anomaly_spawners find randomSpawners) } forEach anomaly_spawners_main; Now I just need to randomize the radius from the marker. I actually did that just as you suggested @_foley. Took me like a minute. That was satisfying! Thanks! ❤️ 1 Share this post Link to post Share on other sites
Larrow 2822 Posted May 23, 2022 (edited) Or for area markers... Spoiler anomaly_spawners = [ "ra_1", "ra_2", "ra_3", "ra_4", "ra_5" ]; // Available anomalies anomalies = [ "anoamly_moduleElectra", // Electra "anoamly_moduleBurner", // Burner "anoamly_moduleFruitPunch", // Fruit Punch "anoamly_moduleMeatgrinder", // Meatgrinder "anoamly_moduleSpringboard" // Springboard ]; // Generating random anomalies _losowe_anomalie = createGroup sideLogic; for "_i" from 1 to count anomaly_spawners do { _randomSpawner = anomaly_spawners deleteAt ( floor random count anomaly_spawners ); _pos = [ _randomSpawner ] call BIS_fnc_randomPosTrigger; _randomAnomalie createUnit [ _pos, _losowe_anomalie, "this setVariable ['BIS_fnc_initModules_disableAutoActivation', false, true];" ]; }; Edited May 23, 2022 by Larrow Fixed script typo 1 Share this post Link to post Share on other sites
Dj Rolnik 29 Posted May 23, 2022 Aaaahhh, I knew that there was a better and more optimized way of doing this and Larrow would be the one to show it. Damn you, sqf wizard! (in a positive sense!) 1 Share this post Link to post Share on other sites