Jump to content
Sign in to follow this  
BEAKSBY

How do I pass an Array to a Function

Recommended Posts

HI All,

I'm trying to pass the following array [_unitValue,_textvehicle] to a function but does not read it from the function side:

// Identify vehicle and its value
_textvehicle = [];
for "_i" from  0 to (count veh)-1  do {   // also add the value of vehicle killed 
if (_unitType == ((veh select _i) select 1)) then {_unitValue = (((veh select _i) select 0)/2);   // each unit value is half original price 
_vehicle = getText (configFile >> "cfgVehicles" >> ((veh select _i) select 1) >> "displayname");		             
_picture = getText (configFile >> "cfgVehicles" >> ((veh select _i) select 1) >> "picture");
_textvehicle = parsetext format ["<t size='0.85' ><img size='1' image='%1'/><t size='0.85'> %2", _picture,_vehicle];

//--- Add money to the client 
[ [_unitValue,_textvehicle] , "DNA_fnc_addMoney", _killer ] call BIS_fnc_MP; 

fn_addMoney.sqf

private ["_amountAAA", "_amount", "_textKilled"];
_amountAAA = [ _this, 0, 0, [0] ] call BIS_fnc_param;

hint _amountAAA; // THIS DOES NOT DISPLAY WHEN I TEST

_amount = _amountAAA select 0;
_textKilled = _amountAAA select 1;

//--- Add money to global
DNA_Money = DNA_Money + _amount;
_money = DNA_Money;

// hint You gained _amount   Cash Remaining $ _money    You Killed: _textKilled   // THIS DOES NOT DISPLAY WHEN I TEST
hint parsetext format ["<t size='0.85' align='left'>You gained $</t><t size='0.85' color='#ff0000' align='left'> %1</t><br/>  <t size='0.85' align='left'>Cash Remaining $</t> <t size='0.85' color='#ff0000' align='left'>%2</t>  <br/><t size='0.85' align='left'>You killed: </t><t size='0.85' align='left'>%3</t>",  _amount, _money, _textKilled,];

true

Thanks in-advance.

Share this post


Link to post
Share on other sites

I would try setting _unitValue as private to the main scope of the script.

Declare it private like so:

private ["_unitValue"];

So in the context of your first script it looks like this:

_textvehicle = [];
private ["_unitValue"];
for "_i" from  0 to (count veh)-1  do {   // also add the value of vehicle killed  
if (_unitType == ((veh select _i) select 1)) then {_unitValue = (((veh select _i) select 0)/2);   // each unit value is half original price  
_vehicle = getText (configFile >> "cfgVehicles" >> ((veh select _i) select 1) >> "displayname");                      
_picture = getText (configFile >> "cfgVehicles" >> ((veh select _i) select 1) >> "picture"); 
_textvehicle = parsetext format ["<t size='0.85' ><img size='1' image='%1'/><t size='0.85'> %2", _picture,_vehicle]; 

//--- Add money to the client  
[ [_unitValue,_textvehicle] , "DNA_fnc_addMoney", _killer ] call BIS_fnc_MP;

I think the issue there is you start to use the variable in a different scope to where you use it to pass on to your next script (in the main scope), so it is out of scope. I haven't tested this but everything else looks okay in the context in which you posted it. I haven't looked at the other script though (but it sounds like the error is here anyway as you said).

You can test these things by doing

diag_log format ["%1", _unitValue];

Just before you pass the variable, so you can see what you're passing. I haven't tried it, but it probably returns ANY or <NULL> at the moment.

Share this post


Link to post
Share on other sites

Thanks Das,

FYI, my original script worked without the array, nor the private _unitValue declaration as follows (same as above except for):

//--- Add money to the client 
//   _unitValueAndText = [_unitValue,_textvehicle]; DOES NOT WORK
[ _unitValue, "DNA_fnc_addMoney", _killer ] call BIS_fnc_MP; 

and original fn_addMoney.sqf without array either:

private "_amount";
_amount = [ _this, 0, 0, [0] ] call BIS_fnc_param;

//--- Add money to global
DNA_Money = DNA_Money + _amount;
_money = DNA_Money;


hint parsetext format ["<t size='0.85' align='center'>You gained $</t><t size='0.85' color='#ff0000' align='center'> %1</t><br/> <t size='0.85' align='center'>Cash Remaining $</t> <t size='0.85' color='#ff0000' align='center'>%2</t>", _amount, _money];

However I will still test your suggestion and use the diag_log (this is new to me).

Thanks again.

Share this post


Link to post
Share on other sites
private ["_amountAAA", "_amount", "_textKilled"];
_amountAAA = [ _this, 0, 0, [0] ] call BIS_fnc_param; //_amountAAA becomes _this select 0

hint str _amountAAA; // can not hint a number needs to be a string

_amount = _this select 0; // other wise you were saying params select 0 select 0 which throws an error
_textKilled = _this select 1; // as above

DNA_Money = DNA_Money + _amount;
_money = DNA_Money;

//removed the extra comma after _textKilled
hint parsetext format ["<t size='0.85' align='left'>You gained $</t><t size='0.85' color='#ff0000' align='left'> %1</t><br/>  <t size='0.85' align='left'>Cash Remaining $</t> <t size='0.85' color='#ff0000' align='left'>%2</t>  <br/><t size='0.85' align='left'>You killed: </t><t size='0.85' align='left'>%3</t>",  _amount, _money, _textKilled];

true  

Share this post


Link to post
Share on other sites
HI All,

I'm trying to pass the following array [_unitValue,_textvehicle] to a function but does not read it from the function side:

fn_addMoney.sqf

private ["_amountAAA", "_amount", "_textKilled"];
_amountAAA = [ _this, 0, 0, [0] ] call BIS_fnc_param;

hint _amountAAA; // THIS DOES NOT DISPLAY WHEN I TEST

_amount = _amountAAA select 0;
_textKilled = _amountAAA select 1;

//--- Add money to global
DNA_Money = DNA_Money + _amount;
_money = DNA_Money;

// hint You gained _amount   Cash Remaining $ _money    You Killed: _textKilled   // THIS DOES NOT DISPLAY WHEN I TEST
hint parsetext format ["<t size='0.85' align='left'>You gained $</t><t size='0.85' color='#ff0000' align='left'> %1</t><br/>  <t size='0.85' align='left'>Cash Remaining $</t> <t size='0.85' color='#ff0000' align='left'>%2</t>  <br/><t size='0.85' align='left'>You killed: </t><t size='0.85' align='left'>%3</t>",  _amount, _money, _textKilled,];

true

Thanks in-advance.

Hello,

You seem to be processing the array wrongly:

fn_addMoney.sqf

private ["_unitValue", "_textvehicle"];
_unitValue = [_this, 0, 0, [0]] call BIS_fnc_param;
_textvehicle = [_this, 1, "", [""]] call BIS_fnc_param;

Share this post


Link to post
Share on other sites
Hello,

You seem to be processing the array wrongly:

fn_addMoney.sqf

private ["_unitValue", "_textvehicle"];
_unitValue = [_this, 0, 0, [0]] call BIS_fnc_param;
_textvehicle = [_this, 1, "", [""]] call BIS_fnc_param;

I've tested the above and it returns the number value for _unitValue but the _textvehicle is not displayed.

fn_addMoney.sqf

private ["_unitValue", "_textvehicle"]; 
_unitValue = [_this, 0, 0, [0]] call BIS_fnc_param; 
_textvehicle = [_this, 1, "", [""]] call BIS_fnc_param;  

//--- Add money to global
DNA_Money = DNA_Money + _unitValue;
_money = DNA_Money;

// hint You gained _amount   Cash Remaining $ _money    You Killed: _textKilled   // THIS DOES NOT DISPLAY WHEN I TEST
hint parsetext format ["<t size='0.85' align='left'>You gained $</t><t size='0.85' color='#ff0000' align='left'> %1</t><br/>  <t size='0.85' align='left'>Cash Remaining $</t> <t size='0.85' color='#ff0000' align='left'> %2</t>  <br/><t size='0.85' align='left'>You killed: </t><t size='0.85' align='left'>%3</t>",  _unitValue, _money,_textvehicle];

true

I am sending this to the function:

[ [_unitValue, _textvehicle], "DNA_fnc_addMoney", _killer ] call BIS_fnc_MP;

where _unitValue = _unitValue + _unitsInVehicleSumValue; //---total value of vehicle and units killed inside

and _textvehicle = parsetext format ["<t size='0.85' ><img size='1' image='%1'/><t size='0.85'> %2", _picture,_vehicle];

I tested with hint _textvehicle; //---THIS WORKS and this diplays the picture and display name of the vehicle before sending it the functions.

if (_unitType == ((veh select _i) select 1)) then {_unitValue = (((veh select _i) select 0)/2);   //---each unit value is half original price listed above
_vehicle = getText (configFile >> "cfgVehicles" >> ((veh select _i) select 1) >> "displayname");		             
_picture = getText (configFile >> "cfgVehicles" >> ((veh select _i) select 1) >> "picture");
_textvehicle = parsetext format ["<t size='0.85' ><img size='1' image='%1'/><t size='0.85'> %2", _picture,_vehicle];
//hint _textvehicle;   //---THIS WORKS
};	

Does BIS_fnc_param accept pictures from configFile >> "cfgVehicles" >> ((veh select _i) select 1) >> "picture"); as a dataType?

I also tested it without the picture so _textvehicle is just a string and it still does not seem to be passed as it is not displaying?

Edited by BEAKSBY

Share this post


Link to post
Share on other sites

Sorry, did not notice you are parsing the String. Text is different data type then String, try the following:

private ["_unitValue", "_textvehicle"];
_unitValue = [_this, 0, 0, [0]] call BIS_fnc_param;
_textvehicle = [_this, 1, parsetext "", [parsetext ""]] call BIS_fnc_param;

Share this post


Link to post
Share on other sites
Sorry, did not notice you are parsing the String. Text is different data type then String, try the following:

private ["_unitValue", "_textvehicle"];
_unitValue = [_this, 0, 0, [0]] call BIS_fnc_param;
_textvehicle = [_this, 1, parsetext "", [parsetext ""]] call BIS_fnc_param;

This works, thanks!

Edited by BEAKSBY

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  

×