Zippie 1 Posted January 24, 2012 I'm having some strange issues with a simple function. Script call: nul = [TargetSign,ShowSign]execVM "initTarget.sqf" TargetSign and ShowSign are object names. initTarget.sqf _target = _this select 0; _monitor = _this select 1; DamHDL = compile preprocessFile "hello.sqf"; _target addEventHandler ["handleDamage",{_handle = [_target,_monitor]call DamHDL}]; hello.sqf hint format["DAT:%1",_this]; Now the problem: The hint in hello.sqf returns any but i expect it to return [TargetSign,ShowSign]. Btw: The eventhandler is being fired correctly and everything else works! Any ideas why? Cheers, Zippie Share this post Link to post Share on other sites
.kju 3245 Posted January 24, 2012 Zippie the variable scope is the problem. Your local variables do not exist in the EH execution space. Check this: http://community.bistudio.com/wiki/Variables#Scope And use forum search: Recently there was a good thread explaining it in detail. Share this post Link to post Share on other sites
Muzzleflash 111 Posted January 24, 2012 The event handler is launched separately by the engine when the event occurs. So it has no "memory" of what was defined in some other branch of execution. When you write: _myVar = 10; _code = {hint str _myVar}; Neither the value or reference to _myVar is inside that code. It is only when it executes the statement that it tries to find _myVar. and since events are launched in a separate branch of execution where no _target or _monitor has been defined the value is 'nil' which when hinted is 'any' since it could have been anything. Share this post Link to post Share on other sites
Zippie 1 Posted January 24, 2012 First of all, thank you for your quick answers! I was expecting something like that.. Had some serious experience with c ;) Is there any way to sort that problem out without using global variables? Share this post Link to post Share on other sites
demonized 20 Posted January 24, 2012 setVariable and getVariable is one way to work around it. Share this post Link to post Share on other sites
Zippie 1 Posted January 24, 2012 I was already trying to do it with getVariable. But it wasn't working because i think that when the initTarget script finishes execution the local variables are beeing erased. Share this post Link to post Share on other sites
demonized 20 Posted January 24, 2012 something like this: _target = _this select 0; _monitor = _this select 1; DamHDL = compile preprocessFile "hello.sqf"; _target setVariable ["monitor", _monitor, false]; _target addEventHandler ["handleDamage",{ _target = _this select 0; _monitor = _target getVariable "monitor"; _handle = [_target,_monitor] call DamHDL; }]; Share this post Link to post Share on other sites
Zippie 1 Posted January 24, 2012 You Sir are my Hero! It's working perfectly! Thank you very much Share this post Link to post Share on other sites
d3lta 10 Posted January 24, 2012 Hi masters, What´s the practical differences between global variables and setVariable command? Best regards Share this post Link to post Share on other sites
Muzzleflash 111 Posted January 24, 2012 setVariable allows you to store stuff on objects so you don't have to use stuff like or "use up" the global namespace: unit1_color, unit2_color or the dynamic version: call compile format ["unit%1_color2, _myUnitNum]. You store the color directly on the unit and can read it back from the unit. The drawback of setvariable for objects is that when the object dies/destroyed all stored variables disappear shortly after. Share this post Link to post Share on other sites
d3lta 10 Posted January 24, 2012 setVariable allows you to store stuff on objects so you don't have to use stuff like or "use up" the global namespace:unit1_color, unit2_color or the dynamic version: call compile format ["unit%1_color2, _myUnitNum]. You store the color directly on the unit and can read it back from the unit. The drawback of setvariable for objects is that when the object dies/destroyed all stored variables disappear shortly after. thanks muzzleflash. Share this post Link to post Share on other sites