Clayman 20 Posted March 7, 2011 I've ran into a strange problem with a script I'm working on. Not sure if it should be this way or if something is "glitched". Maybe someone can enlighten me. So, basicly I have a script that selects a random set of vehicle and unit classnames and stores these in three local variables. Then these local variables are passed to a function that creates the vehicle and the units, moves the units into the vehicle and returns the group. Simplified version of the script (initPatrol.sqf): _units = ["TK_Soldier_SL_EP1", "TK_Soldier_EP1", "TK_Soldier_GL_EP1", "TK_Soldier_AR_EP1"]; _crew = ["TK_Soldier_Crew_EP1", "TK_Soldier_Crew_EP1"]; _vehicle = "M113_TK_EP1"; _grp = [_units, _crew, _vehicle] call compile preprocessFile "fn_create.sqf"; Hint format ["_units: %1\n\n_crew: %2\n\n_vehicle: %3\n\n_grp: %4", _units, _crew, _vehicle, _grp]; _units is the classnames of the units in cargo _crew is the classnames of the vehicle crew _vehicle is classname of the vehicle Function to create the units (fn_create.sqf): _u = _this select 0; _c = _this select 1; _v = _this select 2; _p = "pos1"; _grp = [[0,0,0], EAST, _u] call BIS_fnc_spawnGroup; _vehicle = _v createVehicle markerPos _p; _crew = []; _tmp = [[0,0,0], EAST, _c] call BIS_fnc_spawnGroup; _crew = units _tmp; _d = _crew select 0; _d assignAsDriver _vehicle; _d moveInDriver _vehicle; _g = _crew select 1; _g assignAsGunner _vehicle; _g moveInGunner _vehicle; If (count _crew == 3) Then { _o = _crew select 2; _o assignAsCommander _vehicle; _o moveInCommander _vehicle; }; { _x assignAsCargo _vehicle; _x moveInCargo _vehicle; } forEach units _grp; _crew join _grp; deleteGroup _tmp; _grp The function uses the same local variables _crew and _vehicle. But as these are local, it shouldn't conflict with the variables in the first script (initPatrol.sqf). Therefore the Hint at the end should return all the classnames for the variables _units, _crew and _vehicle. But this is only true for _units, while _crew and _vehicle return the actual units (e.g. O 1-1-B:1 etc.) as the variables in fn_create do. And this is pretty much the part I don't understand. Can someone explain to me what's going wrong - either in the script or in my head. :confused: Share this post Link to post Share on other sites
neokika 62 Posted March 7, 2011 Hey, Try the private command. _neo_ Share this post Link to post Share on other sites
sbsmac 0 Posted March 7, 2011 I'm afraid the problem is in your head :) If you don't declare variables 'private' they are not restricted in scope at all so when _crew is seen in fnc_create.sqf it is assumed to be the same "_crew" that was declared in the outer scope of init_patrol.sqf. There is nothing 'magic' about putting variables in different files. //File outer.sqf _a =5 ; call compile preprocessfile "inner.sqf"; hint format ["a is %1",_a] ; //File inner.sqf _a=100; is, to all intents and purposes identical to _a =5; call { _a=100;} ; hint format ["a is %1",_a] ; and in both cases the hint will show 100 unless you use 'private' in the inner block. BTW, squint is extremely good at picking up scope issues and even has a feature where you can right-click on a variable and ask it to show you the variable's scope - quite useful for helping to understand these kinds of issues. In your example above, squint would have warned you that the variables in fn_create.sqf might corrupt those in an outer scope (which is exactly what is happening). Share this post Link to post Share on other sites
Clayman 20 Posted March 7, 2011 Most impressive, that did the trick. Thanks neokika. It even mentions exact this situation on the wiki page. lol. And thanks for the explaination sbsmac. Share this post Link to post Share on other sites