Jump to content
Sign in to follow this  
jakkob4682

Function question

Recommended Posts

If I run a function from a different script how do I get the return value from the function? Or do I make the return value global? i.e. instead of _retValue use retValue?

Share this post


Link to post
Share on other sites

the return value can be assigned a local variable in the script you are running it in...

in script A, you have a function defined(someFunction), in script B, you call your function.

_retValue = [whatever parameters] call someFunction;

_retValue is now equal to the return value of someFunction.

To do this as shown above, you must precompile your function before calling

someFunction = compile preprocessFileLineNumbers "folderforfunctions\someFunction.sqf";

Edited by panther42

Share this post


Link to post
Share on other sites

i get who to call the function passing variable to it... what i mean is for an example like

_obj = _this select 0;
retValue = False;
if (argument) then 
{
//what ever code;
retValue = True;
};
retValue

then in another script can I just use

if (retValue == True) then 
{
do something
};

Share this post


Link to post
Share on other sites

Well, you are using global variable in that example. to check retValue would just be If (retValue) then {code here}

Using local variable, in function, you assign local variable in current script like:

_retValue = [object to pass] call functionName;

If (_retValue) then {code here}... since you are checking boolean

Here's an example of function which returns boolean(true/false) from BIS:

/*
File: isAmphibious.sqf
Author: Joris-Jan van 't Land

Description:
Function will return whether or not this vehicle is amphibious.

Parameter(s):
_this select 0: type ID
_this select 1: config entry
*/

//TODO: use config entry function?

private ["_tid", "_entry", "_amphib"];
_tid = _this select 0;
_entry = _this select 1;
_amphib = false;

if (_tid in [0, 1]) then 
{
if (getNumber(_entry >> "canFloat") == 1) then 
{
	_amphib = true;
};
} 
else 
{
if (_tid == 11) then 
{
	_amphib = true;
};
};

_amphib

So, your return is _amphib. Call this function in another script you are currently in with another local variable. You can name same if you want, or another helpful variable name

_isAmphib = [parameters to pass] call isAmphibious;

now check boolean in current script,

If (_isAmphib) then {code here}

make sense?

Edited by panther42

Share this post


Link to post
Share on other sites

so if I had a function to find the nearest town and I wanted to get the town

so if I had a function to find the nearest town and I wanted to use this function in a different script

_grp = _this select 0;

_ret = false;
_nearestTown = locationNull;
_nearestTowns = nearestLocations[leader _grp,["nameCity","nameCapital","nameVillage"];
_nearestGrp = groupNull;
{
if ((side _x==west)&&(leader _x distance player < 200)) then {
	_nearestGrp = _grp;
};
}forEach allGroups;

if (!isNull _nearestGrp) then {
_nearestTown = _nearestTowns select 0;
       _ret = TRUE;
};
_ret

_nTown = [group] call nearestTown;

if (_nTown) then {}?

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  

×