jakkob4682 23 Posted September 22, 2020 Do I need to have them declared before I start a loop in order to use them after? I.e. _array = [];for from loop; _array = what was defined in loop? Share this post Link to post Share on other sites
ZaellixA 383 Posted September 22, 2020 In .sqfs the similar (if not the same) rules as with other languages such as C/C++ and Java apply to scopes and scoped variables. If you declare (and/or define) the array inside the for-loop, then it will be "destroyed" (it goes out-of-scope) after the loop finishes. This means that you will have to declare the array in the same scope you intend to use it (in your case, this is outside the for-loop). For more information please see the documentation. 2 Share this post Link to post Share on other sites
panther42 40 Posted September 22, 2020 @killzone_kid also has a tutorial on variables (three parts) & scopes. In fact, if you have not visited his site, he has a lot of great information available. 2 Share this post Link to post Share on other sites
jakkob4682 23 Posted September 22, 2020 f_markLocations = { _array = ( allMissionObjects "Logic" select{ !( _x isKindOf "Module_F" ) } ) - [ BIS_functions_mainscope ]; _numLogics = count _array; { _return = []; private["_idx","_pos","_count","_location"]; _obj = _array select _forEachIndex; _pos = getPosASL _obj; { _locations = nearestLocations[_pos,["nameVillage","nameCity","nameCityCapital","CityCenter","Hill","Strongpoint"],3000]; _townArray = []; _miscArray = []; _idx = 0; for "_i" from 0 to (count _locations) do { _location = _locations select _i; _pos = locationPosition _location; _str = format ["location%1%2",_idx,_type]; _mrk = createMarker [_str,_pos]; _mrk setmarkerColor "ColorRed"; _mrk setMarkerSize [1,1]; _idx = _idx + 1; { if(type _x != "Hill" and type _x != "Strongpoint") then { _miscArray pushBack _x; _mrk setMarkerType "O_Recon"; } else { _townArray pushBack _x; _mrk setMarkerShape "Ellipse"; _mrk setMarkerSize [50,50]; }; }forEach _locations; }; //hintSilent format ["%1:%2",_count,_numLogics]; }forEach _locations; _return = [_townArray,_miscArray]; }forEach _array; _return }; is not returning anything for a count in either of the indexes for _return is why I'm asking Share this post Link to post Share on other sites
jakkob4682 23 Posted September 22, 2020 never mind.... found it. never mind... still not working Share this post Link to post Share on other sites
jakkob4682 23 Posted September 22, 2020 restructured it and still not getting anything for the return value Spoiler f_markLocations = { _array = ( allMissionObjects "Logic" select{ !( _x isKindOf "Module_F" ) } ) - [ BIS_functions_mainscope ]; _numLogics = count _array; _townArray = []; _miscArray = []; _return = [_townArray,_miscArray]; { _return = []; private["_idx","_pos","_count","_location"]; _obj = _array select _forEachIndex; _pos = getPosASL _obj; _townArray = nearestLocations[_pos,["nameVillage","nameCity","nameCityCapital","CityCenter"],3000]; _idx = 0; for "_i" from 0 to (count _locations - 1) do { _location = _locations select _idx; _pos = locationPosition _location; _str = format ["location%1",_idx]; _mrk = createMarker [_str,_pos]; _mrk setmarkerColor "ColorRed"; _mrk setMarkerShape "Ellipse"; _mrk setMarkerSize (size _location); _idx = _idx + 1; }; _miscArray = nearestLocations [_pos,["StrongpointArea","HILLS"],1500]; _idx = 0; for "_i" from 0 to (count _miscArray - 1) do { _mLoc = _miscArray select _idx; _mPos = locationPosition _mLoc; _str = format ["m_loc%1",_idx]; _mrk = createMarker [_str,_mPos]; _mrk setMarkerColor "ColorRed"; _mrk setMarkerSize [1,1]; _mrk setMarkerType "O_Recon"; _idx = _idx + 1; }; }forEach _array; _return }; Share this post Link to post Share on other sites
pierremgi 4737 Posted September 22, 2020 f_markLocations = { private _logics = ( allMissionObjects "Logic" select{ !( _x isKindOf "Module_F" ) } ) - [ BIS_functions_mainscope ]; private _numLogics = count _logics; private ["_logic","_pos","_count","_locations","_location","_townArray","_miscArray","_mkr"]; private _return = []; { _logic = _logics select _forEachIndex; _pos = getPosASL _logic; _locations = nearestLocations[_pos,["nameVillage","nameCity","nameCityCapital","CityCenter","Hill","Strongpoint"],3000]; _townArray = []; _miscArray = []; { _location = _locations select _forEachIndex; _pos = locationPosition _location; _mrk = createMarker [format ["location%1%2",_forEachIndex,_type],_pos]; _mrk setmarkerColor "ColorRed"; if(type _x != "Hill" and type _x != "Strongpoint") then { _miscArray pushBack _x; _mrk setMarkerType "O_Recon"; } else { _townArray pushBack _x; _mrk setMarkerShape "Ellipse"; _mrk setMarkerSize [50,50]; }; } forEach _locations; _return pushBack [_townArray,_miscArray]; } forEach _logics; _return }; hint str (call f_markLocations); 2 Share this post Link to post Share on other sites
jakkob4682 23 Posted September 22, 2020 1 hour ago, pierremgi said: _return pushBack [_townArray,_miscArray]; so if i'm understanding right... after updating those two arrays, _return also needs to be updated instead of using _return = [_var1,_var2] or [_townArray,_miscArray] in my case? Share this post Link to post Share on other sites
jakkob4682 23 Posted September 22, 2020 should all the variables used in the function be declared private? Share this post Link to post Share on other sites
panther42 40 Posted September 22, 2020 If I'm reading yours correctly, you are overwriting _return each time it runs through forEach _array; You want to "add to" the existing, not overwrite, to keep array of all. Couple of points: @pierremgi script _logic = _logics select _forEachIndex; could also be just _logic = _x _return pushBack [_townArray,_miscArray]; could be _return pushBack [_x,_townArray,_miscArray]; to save reference to logic for the _townArray & _miscArray 2 Share this post Link to post Share on other sites
jakkob4682 23 Posted September 22, 2020 _forEachIndex is something I've recently learned of thanks to MGI, i've just been using indexing in my other functions. Share this post Link to post Share on other sites
jakkob4682 23 Posted September 22, 2020 28 minutes ago, jakkob4682 said: should all the variables used in the function be declared private? ? Share this post Link to post Share on other sites
pierremgi 4737 Posted September 22, 2020 That's better. Global ones are a little bit more demanding. So you can declare it for current scope and sub-scopes, especially before any loop, instead of multiplying that for nuts inside the loop. 2 Share this post Link to post Share on other sites
pierremgi 4737 Posted September 22, 2020 37 minutes ago, panther42 said: @pierremgi script _logic = _logics select _forEachIndex; could also be just _logic = _x yes, my bad (written at 5 AM) Need a coffee. 1 1 Share this post Link to post Share on other sites
jakkob4682 23 Posted September 22, 2020 so by declaring it private at the start it becomes available throughtout the whole function with out having to define a million variables? i.e. i have 10 different variable in the function, and don't want to use objnull, array = [], and etc a million times, i just need to declare a variable private at the start? Also when to use _x and when to use _forEachIndex? Share this post Link to post Share on other sites
panther42 40 Posted September 22, 2020 I suggest you read the previous tutorials I posted, and here's another:Private Special Variables These will help you understand what you inquire about. Much better to understand why things work the way they do, than to say "because person x told me so". The old proverb "Give a man a fish, and you feed him for a day. Teach a man to fish, and you feed him for a lifetime." 2 Share this post Link to post Share on other sites
pierremgi 4737 Posted September 22, 2020 _x is the special variable inside the forEach code and related to the current element of this loop (treating elements of the array). Not specific to forEach, you can use it in select (as example allUnits select {side _x == WEST} ); _forEachIndex is an index (number) specific to forEach command Spoiler I hope this fish will give you the taste for fishing by your own 😊 1 Share this post Link to post Share on other sites