Jump to content
Robustcolor

Return value from function

Recommended Posts

Hi, how do i fetch the _grp01 variable back to my first function? I want to use it later in my waitUntil.

 

Can i fetch the return value only with call or does spawn work too?

 

First function;

private _m1 = [_marker] call Units;

waitUntil {sleep 5; (allPlayers findIf {(_x distance2d _marker) > 400} !=-1)};

{deleteVehicle _x;} forEach units _group;
Second function;

Units = 
{

_grp01 = createGroup [civilian, true];

_unit = _grp01 createUnit [selectRandom ["C_Man_casual_1_F","C_Man_Fisherman_01_F","C_man_p_shorts_1_F","C_Man_UtilityWorker_01_F","C_man_polo_2_F","C_man_polo_5_F","C_Man_casual_6_F","C_Man_casual_4_F"], _marker, [], 10, "FORM"];

};

 

Share this post


Link to post
Share on other sites

https://community.bistudio.com/wiki/Function#Return_Values

//Second function

Units = {
    params["_marker", "_grp01"];
    _grp01 = createGroup [civilian, true];

    _grp01 createUnit [selectRandom ["C_Man_casual_1_F","C_Man_Fisherman_01_F","C_man_p_shorts_1_F","C_Man_UtilityWorker_01_F","C_man_polo_2_F","C_man_polo_5_F","C_Man_casual_6_F","C_Man_casual_4_F"], _marker, [], 10, "FORM"];

    _grp01
};

You can only return values with call.

When making functions it is usually desired that variables inside that function don't change variables that have been possibly named the same, that is why the params command. I also removed the _unit variable as it seems you don't use it inside the function.

 

https://community.bistudio.com/wiki/Introduction_to_Arma_Scripting#Must-read_articles

Some good reading material.

Share this post


Link to post
Share on other sites
5 minutes ago, kauppapekka said:

it is usually desired that variables inside that function don't change variables that have been possibly named the same

yes.

5 minutes ago, kauppapekka said:

that is why the params command.

no thats not what params does.

if you want to prevent overwriting variables you use private in the form of "private _grp01 = .."

 

Share this post


Link to post
Share on other sites

And don't call your function Units. Units is already a command.

Stick to some naming convention to make you functions standout, like..

TAG_fnc_someDescriptiveName

 

Share this post


Link to post
Share on other sites
1 hour ago, kauppapekka said:

Well according to Killzone Kid's note it does double as a private.

it also privately declares the variables yes. But that's not its purpose.

Share this post


Link to post
Share on other sites
2 hours ago, kauppapekka said:

Well according to Killzone Kid's note it does double as a private.

what note?

Share this post


Link to post
Share on other sites
27 minutes ago, killzone_kid said:

what note?

The ancient note you can read if you click on view history and compare 8 nov 2019 and 12 jan 2020 .... and I suppose we are not alone to remember that.

 

Now, in the front BIKI params page, we still can read:

It is a good practice to make your local variables private (through private or params) in order to avoid overwriting a local variable of the same name.

Until further modification?

 

So, @Dedmen & @killzone_kid That could be great to precise if params worth private for avoiding an overwrite of the local variable. I'm not sure the @kauppapekka comment was wrong and yours garble this functionality. Thanks for all the readers.

Share this post


Link to post
Share on other sites
12 minutes ago, pierremgi said:

That could be great to precise if params worth private for avoiding an overwrite of the local variable.

params sets the "parameters" as private variables.

 

but its purpose is to get params out of _this, not to make variables private.

Share this post


Link to post
Share on other sites

How do i fetch the returning value from second function

Second function;

Myunits =
{
params ["_marker","_group"];

group = createGroup [civilian, true];

_group createUnit [selectRandom ["C_Man_casual_1_F","C_Man_Fisherman_01_F"], _marker, [], 10, "FORM"]

_group

};
First function;

_group = [""];

private _m1 = [_marker] call Units;

waitUntil {sleep 5; (allPlayers findIf {(_x distance2d _marker) > 400} !=-1)};

{deleteVehicle _x;} forEach units _group;

Error, expected units not an array.

Share this post


Link to post
Share on other sites
1 hour ago, Robustcolor said:

How do i fetch the returning value from second function

_result = [_marker, _group] call Myunits

 

is first function supposed to call that? if so then you have multiple errors in there.

it calls "Units" but your function is called "Myunits"

it only passes one parameter, but "Myunits" requires two parameters.

also you set _group by default to array of string? wtf? group should be a group, set it to a group by default if you need that.

 

Also in the first one

1 hour ago, Robustcolor said:

group = createGroup [civilian, true];

group is a script command, you cannot have a variable with the same name as a script command

 

also you are missing a semicolon after

1 hour ago, Robustcolor said:

_marker, [], 10, "FORM"]

 

 

wow man this is a mess, uh.. what was the question again? The broken code that can never work and that clearly ignores advice that was given above distracted me.

Share this post


Link to post
Share on other sites
9 hours ago, Robustcolor said:

Error, expected units not an array.

No such error, maybe expected group? Then yes, [""] is not a group, it is array, if you need default group type use grpNull 

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

×