Jump to content
Sign in to follow this  
BEAKSBY

Difficulty passing variable through addAction...please help

Recommended Posts

Hi Folks,

I can't figure out why _vehCost in the addAction below is not being passed to it's script. I've initialized it at

_vehCost = 0; in the initServer.sqf and initPlayerLocal.sqf thinking this would solve the issue.

scriptWithaddAction.sqf

_vehType = _this select 0;
_tank addAction [("<t color=""#66FFFF"">" + ("Repair/Flip") +"</t>"), {[_vehCost] execVM "Repair.sqf"}, [], 5, true, true, "", "player distance _target < 5 && !(player in _target)"];

Repair.sqf

private "_vehCost";
_vehCost = _this select 0; 

_veh = "";
_timeForRepair = 2;

_pos  = screenToWorld [0.5,0.5];// land position 
if (!isnull cursortarget) then { _veh = cursortarget}; // name of object
hint format ["Please wait %1 seconds to repair for $ %2",_timeForRepair, _vehCost];
sleep _timeForRepair;
_veh setDamage 0;)

_vehCost returns any

Share this post


Link to post
Share on other sites

Are you aware that variables beginning with and underscore are local to the scope in which they are defined? Let me explain... If you define _vehCost in the file initServer.sqf, your _vehCost variable will only be defined in that script. Let me show you an example of what I mean... First, let's start off with your situation, which involves two different scripts.

script1.sqf

_a = 1;
[] execVM "script2.sqf";

script2.sqf

_b = _a;

In this example, variable _b in script2.sqf would be any, because you are defining it with a variable that does not exist in it's current scope. In the scope of script2, _a is undefined (or, any). Now, this can be easily rectified by passing _a to script2.sqf through execVM's parameters. Like so...

script1.sqf

_a = 1;
[_a] execVM "script2.sqf";

script2.sqf

//_this = the params of execVM (in this case, [_a])
_a = _this select 0;
_b = _a;

In this example, _b would now have a value of 1, since you have passed the value of _a from script1 and redefined it in script2. Also, you may want to look into using global variables, which you would use just like local variables only without the underscore prefix and you can get their value from any scope so long as you are getting the value of the global variable on the same client as it was defined on.

Happy Coding Broseph.

Share this post


Link to post
Share on other sites

Thanks again,

But I thought _vehCost would be passed to Repair.sqf?

But it is still coming up as any, and therefor not being passed like in your example above.

Share this post


Link to post
Share on other sites

Horner is correct about the local variables. The part he did not mention is that the code of an action is executed in a different scope from the function which executed the addAction statement. This code executes completely separately:

[_vehCost] execVM "Repair.sqf"

When the engine sees this statement alone, it cannot possible know the value of _vehCost. The addAction command has a special parameter for arguments, which preserves values attached to the action. The code should be this:

_tank addAction [("<t color=""#66FFFF"">" + ("Repair/Flip") +"</t>"), {_this execVM "Repair.sqf"}, [_vehCost], 5, true, true, "", "player distance _target < 5 && !(player in _target)"]; 

Where _this refers to the arguments of the action being passed to the called code.

On a related note, you might want to compile Repair.sqf into a function if it will be used a lot. This is more efficient and saves the engine time. For example:

// somewhere at the start of the mission
fnc_Repair = compileFinal preprocessFileLineNumbers "Repair.sqf";

// then later from any function
_tank addAction [("<t color=""#66FFFF"">" + ("Repair/Flip") +"</t>"), fnc_Repair, [_vehCost], 5, true, true, "", "player distance _target < 5 && !(player in _target)"]; 

Share this post


Link to post
Share on other sites

Incredible support!

Thanks Horner and Zenophon. Now i understand the addAction notes from the Biki with these examples.

I had to use _this select 3 select 0; in the script that gets the passed variable from the addAction.

This part of the addAction Biki now makes sense to me:

Parameters array passed to the script upon activation in _this variable is: [target, caller, ID, arguments]

target (_this select 0): Object - the object which the action is assigned to

caller (_this select 1): Object - the unit that activated the action

ID (_this select 2): Number - ID of the activated action (same as ID returned by addAction)

arguments (_this select 3): Anything - arguments given to the script if you are using the extended syntax

https://community.bistudio.com/wiki/addAction

Edited by BEAKSBY

Share this post


Link to post
Share on other sites
Horner is correct about the local variables. The part he did not mention is that the code of an action is executed in a different scope from the function which executed the addAction statement.

Yes, exactly. I was simply stating how different scripts are in different scopes from each other. I missed that he was using code in his addaction params ;)

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  

×