Jump to content
Sign in to follow this  
Zippie

Strange behaviour of function arguments

Recommended Posts

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

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

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

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

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

You Sir are my Hero!

It's working perfectly!

Thank you very much

Share this post


Link to post
Share on other sites

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

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
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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×