Jump to content
Sign in to follow this  
eagledude4

Issue returning in functions

Recommended Posts

I'd like for someone to explain to me why:

getItemArray = {
_return = [];
if (typeName _this == "STRING") then {
	for "_c" from 0 to ((count INV_AllMissionObjects) - 1) do {
		if (((INV_AllMissionObjects select _c) select 0) == _this) then {
			_return = INV_AllMissionObjects select _c;
		};
	};
} else {
	diag_log format ["ERROR: Input type for %1 was %2 and needs to be STRING", _this, typeName _this];
};

_return;
};

works, and:

getItemArray = {
if (typeName _this == "STRING") then {
	for "_c" from 0 to ((count INV_AllMissionObjects) - 1) do {
		if (((INV_AllMissionObjects select _c) select 0) == _this) exitwith {
			INV_AllMissionObjects select _c;
		};
	};
} else {
	diag_log format ["ERROR: Input type for %1 was %2 and needs to be STRING", _this, typeName _this];
};
};

doesn't.

Share this post


Link to post
Share on other sites

but you don't store the result anywhere.

Can you write the main script ?

Share this post


Link to post
Share on other sites

Remove the semicolon ; after the line you want returned .. eg

myFunc = {
_randomItems = ["a","b","c","d","e","f","g"];
if (typeName _this == typeName "") then {
	for "_c" from 0 to ((count _randomItems)-1) do {
		if ((_randomItems select _c) == _this) exitWith {
			_randomItems select _c
		};
	};
}else{
	hint "didnt work muppet";
};
};


"c" call myFunc

Share this post


Link to post
Share on other sites

Wouldnt the ; after "_return" in my first example be wrong then too?

Edited by eagledude4

Share this post


Link to post
Share on other sites
Wouldnt the ; after "_return" in my first example be wrong then too?
Following the syntax rules you would generally think so yes. See Return Value on this page https://community.bistudio.com/wiki/Function. Presume as its expecting a returned value that the last expression is used.

Share this post


Link to post
Share on other sites

Yes, don't put a semicolon after the return variable.

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  

×