Jump to content
  • 0

Question

Posted

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?

4 answers to this question

Recommended Posts

Posted

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
Posted
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
Posted

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

  • Upvote 2

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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...