Jump to content
  • 0

return array from function


DeadlySilence

Question


  • Group:  Members
  • Topic Count:  7
  • Topics Per Day:  0.00
  • Content Count:  181
  • Reputation:   53
  • Joined:  04/07/13
  • Last Seen:  

Hello,

today I wanted to make a function which returns all IDs of equipped items. The function looks like this:

function	script	getEquip	{
	// get all equipped item IDs
	.@idx = 0;
	for (set .@i, 1; .@i < 11; set .@i, .@i + 1) {
		if (-1  != getequipid(.@i)) {
			setarray .@items[.@idx], getequipid(.@i);
			set .@idx, .@idx + 1;
		}
	}
	return .@items;
}

It's all fine and .@items contains all neccessary IDs.

But when I call the function like this:

.@a = callfunc("getEquip");

.@a contains only a scalar value (the ID of the first item, I guess), instead of the array with all the IDs.

 

Could someone please tell me how to porperly return an array?

Link to comment
Share on other sites

4 answers to this question

Recommended Posts


  • Group:  Members
  • Topic Count:  9
  • Topics Per Day:  0.00
  • Content Count:  379
  • Reputation:   304
  • Joined:  11/10/11
  • Last Seen:  

1) (Euphy ways):

function	script	getEquip	{

// get all equipped item IDs

for (set .@i, 1; .@i < 11; set .@i, .@i + 1) {

if (-1 != getequipid(.@i)) {

set @items[.@idx], getequipid(.@i);

set .@idx, .@idx + 1;

}

}

return @items;

}

callfunc("getEquip");

// @items

2) Your way:
function	script	getEquip	{

// get all equipped item IDs

for (set .@i, 1; .@i < 11; set .@i, .@i + 1) {

if (-1 != getequipid(.@i)) {

set .@items[.@idx], getequipid(.@i);

set .@idx, .@idx + 1;

}

}

return .@items;

}

copyarray .@items, callfunc("getEquip"), 128;

3) By reference:
function	script	getEquip	{

// get all equipped item IDs

for (set .@i, 1; .@i < 11; set .@i, .@i + 1) {

if (-1 != getequipid(.@i)) {

set getelementofarray( getarg(0), .@idx), getequipid(.@i);

set .@idx, .@idx + 1;

}

}

return @items;

}

callfunc("getEquip", .@items);

  • Upvote 4
Link to comment
Share on other sites


  • Group:  Developer
  • Topic Count:  10
  • Topics Per Day:  0.00
  • Content Count:  2407
  • Reputation:   613
  • Joined:  07/05/12
  • Last Seen:  

return .@items;

is similar to

return .@items[0];

 

 

Could someone please tell me how to porperly return an array?

A function can only return one value.

 

I suggest to convert your numeric array to string array, return the concatenate array (Implode), then use Explode outside the function to retrieve the values.

  • Upvote 3
Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  72
  • Topics Per Day:  0.02
  • Content Count:  2997
  • Reputation:   1130
  • Joined:  05/27/12
  • Last Seen:  

Alternatively, you can set a temporary character array (@var) instead of scope and clear it afterwards (probably more efficient than string manipulation).

  • Upvote 2
Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  7
  • Topics Per Day:  0.00
  • Content Count:  181
  • Reputation:   53
  • Joined:  04/07/13
  • Last Seen:  

Thank you for your help, I got it working now :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...