Jump to content
Sign in to follow this  
-LTac- Imperator

Passing a Local Variable into a addMissionEventHandler "draw3d" EVH

Recommended Posts

I'm having a little bit of a quandry with local variables being passed into an event handler declared within a function.

ltac_fnc_drawIcon = {
    private ["_unit"];
    _unit = _this select 0;

   addMissionEventHandler ["Draw3D", {
drawIcon3D ["", [1,0,0,1], getPos _unit, 0, 0, 0, "", 1, 0.05, "PuristaMedium"]; 
   }];

};

I receive an error 0 parameters passed, 3 required. I know that any variables declared within {} or within [] spawn {} are local to that function's scope (since it is essentially an anonymous function - called asynchronously). That also means if you try to pass a locally declared variable from within a function to a code block or a spawn {} it cannot access it because scope does not carry forward.

Is there a way to pass the variable into the addMissionEventHandler or forEachFrame {} method without using a global variable? - Also to clarify a global variable within a script file is global to that single file or to all script files that are available throughout the mission?

Thanks for the help in advance.

Share this post


Link to post
Share on other sites

I wonder if you can call a script that returns a variable?

might make it too slow for you unless it is preprocessed?!

Edited by Kunsa
Nosaer rof Gnitide

Share this post


Link to post
Share on other sites
I wonder if you can call a script that returns a variable?

might make it too slow for you unless it is preprocessed?!

Kunsa thanks for the response, seems like a bit of a hacky solution - but I have a feeling that anything I do in this case is going to be hacky without going to global variables - the problem is this is going to be called many times throughout a mission - I want to make it as high speed - low drag programming wise as possible.

Share this post


Link to post
Share on other sites

Format the command as a string and then call compile it. e.g something like

ltac_fnc_drawIcon = {
    private ["_unit"];
    _unit = _this select 0;

   call compile format ["addMissionEventHandler ['Draw3D', {
drawIcon3D ['', [1,0,0,1], getPos %1, 0, 0, 0, '', 1, 0.05, 'PuristaMedium']; 
   }];",_unit];

};

Also to clarify a global variable within a script file is global to that single file or to all script files that are available throughout the mission?
To all.
  • Like 1

Share this post


Link to post
Share on other sites
Also to clarify a global variable within a script file is global to that single file or to all script files that are available throughout the mission?

Global variables are global to the computer running the script. If the server runs it then only the server knows about the variable unless publicvariable is used.

  • Like 1

Share this post


Link to post
Share on other sites
Format the command as a string and then call compile it. e.g something like

ltac_fnc_drawIcon = {
    private ["_unit"];
    _unit = _this select 0;

   call compile format ["addMissionEventHandler ['Draw3D', {
drawIcon3D ['', [1,0,0,1], getPos %1, 0, 0, 0, '', 1, 0.05, 'PuristaMedium']; 
   }];",_unit];

};

To all.

Larrow, you are a genius! Thank you!

Share this post


Link to post
Share on other sites

Compiling a string wont help here, i'm afraid. As you can imagine, getPos %1 would get replaced by unit object _as_string_. In such case, there is no way to "getPos" from "99499600# 1770844: apc_wheeled_02_rcws_f.p3d":

Error in expression <awIcon3D ['', [1,0,0,1], getPos 99499600# 1770844: apc_wheeled_02_rcws_f.p3d, 0,>

Error position: <# 1770844: apc_wheeled_02_rcws_f.p3d, 0,>

Error Missing ]

Any other object type would cause the same problem. Only way to do that is to feed in position array and relocate getPos outside of the string. Only downside of that would be that position coordinates would always be the same ;).

In other words, we are back to square 1 => don't compile and keep on polluting global variables space :mad:

---------- Post added at 09:48 ---------- Previous post was at 09:09 ----------

Oh well, there is one "cheat" you can try out. As long as you are playing ONLY with strings, everything is just fine. With that in mind, for objects there are netid and objectfromnetid functions available to be abused:

call compile format ["_myfancyid = addMissionEventHandler ['Draw3D', {drawIcon3D ['pic_of_my_convertible.paa', [1,1,1,1], getPos (objectFromNetId '%1'), 1, 1, 0, 'dont drive me on a rainy day', 1, 0.03, 'PuristaMedium'];}];", netId _unit];

Of course there is performance impact but at least this way you can minimize variable scope as much as possible. You decide which one is better.

Edited by [NoMa]Bansku
  • Like 2

Share this post


Link to post
Share on other sites

THANK YOU -NoMA-Bansku!!!

 

THANK YOUR FOR EVER!!

 

YOU ENDED MY HOURS OF SUFFERING!!

 

🙂 🙂 🙂

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  

×