beako 13 Posted September 1, 2015 Hi Folks, I followed the sticky on Stached event handlers - OnEachFrame, OnPlayerConnected, etc but still couldn't sort this out. Firstly, I'm trying to create a function that will run whenever a unit or vehicle is spawned in the game and, secondly, pass the arguments [_randomSectorDestination, _AIintel, _grp] to the function when calling BIS_fnc_addStackedEventHandler. I've tried the including the following in my scripts whenever I have a unit or vehicle spawn: _addNew = [_key, "onEachFrame", {[_this] spawn BEAKS_fnc_Draw3DNEW}, [_randomSectorDestination, _AIintel, _grp] ] call BIS_fnc_addStackedEventHandler thinking that _this would include the array [_randomSectorDestination, _AIintel, _grp], but it doesn't pass the arguments? I created fn__Draw3DNEW.sqf and added to my descrition.ext class CfgFunctions. Instead, wherever I create a unit or vehicle spawn I have copied and pasted the following, but this seems needlessly repetitive? //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// fn_Draw3DNEW = { _randomSectorDestination = _this select 0; _AIintel = _this select 1; _grp = _this select 2; _enemyArray = []; _distance = 0; _displayName = ""; _picture = ""; _icon = ""; _leader = leader _grp; {if (((side _x) == AIside) && (isFormationLeader _x)) then {_enemyArray pushBack _x}} forEach units _grp; { private ["_private", "_displayName", "_picture", "_unitPos", "_text", "_distance", "_leaderDestination"]; //_unit = _x; //typeOf cursorTarget; //_leaderDestination = _leader getVariable '_randomSectorDestination'; if (vehicle _x != _x) then {_x = vehicle _x}; //_distance = round (_x distance player); _distance = round (_x distance ( _randomSectorDestination)); _displayName = getText (configfile >> "CfgVehicles" >> typeOf _x >> "displayName"); if (isPlayer _randomSectorDestination) then {_randomSectorDestination = name vehicle _randomSectorDestination}; _text = str(parseText format["%1 men %2m to %3",count units _grp, (str _distance), _randomSectorDestination ]); _picture = ""; _icon = ""; if (_x isKindOf "LandVehicle") then { _picture = getText (configfile >> "CfgVehicles" >> typeOf _x >> "picture"); } else { _icon = getText (configFile >> "CfgVehicles" >> typeOf _x >> "Icon"); _picture = getText (configFile >> "CfgVehicleIcons" >> _icon); }; drawIcon3D [ _picture, [1,0,0,1], [(visiblePosition _x) select 0,(visiblePosition _x) select 1,((visiblePosition _x) select 2) + 10], 0.8, 0.5, 0, _text, 0, 0.03, "PuristaMedium" ]; player reveal [_x, 4]; {[_x, 1] showWaypoint "ALWAYS" } forEach allGroups; } forEach _enemyArray; ////hint format ["%1 \n %2m \n _grp: %3", _displayName, (str _distance), _grp]; }; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private ["_addNew", "_time", "_key"]; _time = time; _key = str (_time); // initial addStackedEventHandler for player hunt or sectorDestination _addNew = [_key, "onEachFrame", "fn_Draw3DNEW", [_randomSectorDestination, _info, _grp] ] call BIS_fnc_addStackedEventHandler; Share this post Link to post Share on other sites
jshock 513 Posted September 1, 2015 It's because these variables: _randomSectorDestination, _info, _grp, don't exist in the scope of the addStackedEventHandler call. They exist in the draw function, but not outside of it. Share this post Link to post Share on other sites
beako 13 Posted September 1, 2015 It's because these variables: _randomSectorDestination, _info, _grp, don't exist in the scope of the addStackedEventHandler call. They exist in the draw function, but not outside of it. OK thanks, so then there is no way to pass these variables to addStackedEventHandler ? What are the arguments then in? [key, event, code, arguments] call BIS_fnc_addStackedEventHandler; I thought they were arguments for the code ... such as [_this] spawn BEAKS_fnc_Draw3DNEW? Share this post Link to post Share on other sites
jshock 513 Posted September 2, 2015 The arguements your're passing into the function within the stackedEH call don't exist, so you need to instantiate those variables before adding the stackedEH, then "_this" within the code executed in the stackedEH will actually contain defined values for the draw function to use. Also looking at it a bit further you may want the code that is executed by the stackedEH to look like this: _this spawn BEAKS_fnc_draw3DNEW;Notice the removal of the [ ]. Share this post Link to post Share on other sites
beako 13 Posted September 2, 2015 (edited) Thanks again jshock, The arguements your're passing into the function within the stackedEH call don't exist, so you need to instantiate those variables before adding the stackedEH, then "_this" within the code executed in the stackedEH will actually contain defined values for the draw function to use. I tested it using based on your comment above, by initiating the variables before calling the stackedEH like so: _randomSectorDestination = sector1; _AIintel = 1; _grp = group _soldier; _addNew = [_key, "onEachFrame", {_this spawn BEAKS_fnc_Draw3DNEW}, [_randomSectorDestination, _AIintel, _grp] ] call BIS_fnc_addStackedEventHandler; and fn_Draw3DNEW.sqf _randomSectorDestination = _this select 0; _AIintel = _this select 1; _grp = _this select 2; _enemyArray = []; _distance = 0; _displayName = ""; _picture = ""; _icon = ""; _leader = leader _grp; {if (((side _x) == AIside) && (isFormationLeader _x)) then {_enemyArray pushBack _x}} forEach units _grp; { private ["_private", "_displayName", "_picture", "_unitPos", "_text", "_distance", "_leaderDestination"]; if (vehicle _x != _x) then {_x = vehicle _x}; _distance = round (_x distance ( _randomSectorDestination)); _displayName = getText (configfile >> "CfgVehicles" >> typeOf _x >> "displayName"); if (isPlayer _randomSectorDestination) then {_randomSectorDestination = name vehicle _randomSectorDestination}; _text = str(parseText format["%1 men %2m to %3",count units _grp, (str _distance), _randomSectorDestination ]); _picture = ""; _icon = ""; if (_x isKindOf "LandVehicle") then { _picture = getText (configfile >> "CfgVehicles" >> typeOf _x >> "picture"); } else { _icon = getText (configFile >> "CfgVehicles" >> typeOf _x >> "Icon"); _picture = getText (configFile >> "CfgVehicleIcons" >> _icon); }; drawIcon3D [ _picture, [1,0,0,1], [(visiblePosition _x) select 0,(visiblePosition _x) select 1,((visiblePosition _x) select 2) + 10], 0.8, 0.5, 0, _text, 0, 0.03, "PuristaMedium" ]; } forEach _enemyArray; BUT unfortunately, it still is not working? Edited September 2, 2015 by beako Share this post Link to post Share on other sites
jshock 513 Posted September 2, 2015 It may just be a copy/paste error, but could cause an issue, when defining _grp, your're missing a semicolon at the end of that line. Share this post Link to post Share on other sites
beako 13 Posted September 4, 2015 Thanks for your help, I had to declare the variables outside the scope for the EH. Got it working! Share this post Link to post Share on other sites