Jump to content
Sign in to follow this  
ASmallDinosaur

How to Target Caller of Function

Recommended Posts

Sorry if the answer for this question is already out there, I searched but couldn't find anything that was helpful, perhaps I wasn't using the right keywords.

Anyway, I'm trying to create a function that changes a unit's loadout, here's what I've got:

//--fnLoadout.sqf
_unit = (_this select 0); 
_loadout = (_this select 1);
_lnit setUnitLoadout _loadout;

//--Init field of unit I want to change the loadout of:
[this,rifleman] execVM "fn_SetLoadout.sqf";

//--And in the init.sqf I have this:
rifleman = [["arifle_Mk20_F","","acc_pointer_IR","optic_ACO_grn",["30Rnd_556x45_Stanag",30],[],""],[],["hgun_ACPC2_F","","","",["9Rnd_45ACP_Mag",9],[],""],["U_I_CombatUniform",[["FirstAidKit",1],["30Rnd_556x45_Stanag",3,30]]],["V_PlateCarrierIA1_dgtl",[["30Rnd_556x45_Stanag",6,30],["9Rnd_45ACP_Mag",2,9],["HandGrenade",2,1],["SmokeShell",1,1],["SmokeShellGreen",1,1],["Chemlight_green",2,1]]],[],"H_HelmetIA","",[],["ItemMap","","ItemRadio","ItemCompass","ItemWatch",""]];

The issue arises when I exec the function, it says "_unit is undefined" or something like that, so i'm assuming that the function cant target the unit that called it. Is there a way to  make this work or another way that would achieve the same goal? (I can't give the unit a variable name because there's a lot of them and they are spawned in dynamically). Thanks

Share this post


Link to post
Share on other sites

Two things:

_lnit setUnitLoadout _loadout;

_Init? Fix this and everything works well, I think.

 

And, using this script is pointless for me. Why not just write

this setUnitLoadout rifleman

to the initialization field?

Share this post


Link to post
Share on other sites

Miss-spelled it when rewriting it into the forums.It doesn't work when it's spelled right however.

You're right though, I don't know why I didn't think of just doing that haha.

 

Just for future reference, as there are other areas in this scenario where I want to use the same technique, is it possible to select the caller with "_unit = (_this select 0);"? I tried a simpler function just to test and I still couldn't get it working: 

//--fn_damage.sqf
_unit = (_this select 0); 
_unit setdamage 1;

//--Init field of unit I want to die:
[this] execVM "fn_damage.sqf";

 

Share this post


Link to post
Share on other sites

You need to add handle as prefix just like:

0 = [this] execVM "fn_damage.sqf" ;
null = [this] execVM "fn_damage.sqf" ;
_nil = [this] execVM "fn_damage.sqf" ;

I'm using _nil personally BTW.

  • Like 1

Share this post


Link to post
Share on other sites
15 hours ago, Sgt. Dennenboom said:

I believe that init.sqf is ran after the object init fields, therefore "rifleman" is not defined when you execVM your script.

For more info: Initialization Order

That was it, just called the function in the init a little after the start of the scenario and the original code now works, thanks a lot (:

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  

×