AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Array returned by a function (https://forums.alliedmods.net/showthread.php?t=188177)

GoLoT 06-23-2012 10:29

Array returned by a function
 
Hi. I've started a project and need to access an array from a different plugin to display a menu with options from that array. The only way I can think of is using callfunc to return that array.

I have plugin A with a function that returns the array.
Code:
public function() {   return someArray }

I have plugin B that uses callfunc to call the function in plugin A and now I need to copy/save/access the array returned in callfunc_end(). With integers is easy, I just set new variable = callfunc_end() but since it is returning an array this method doesn't work. I tried with copy(dest[], sizeof(dest), callfunc_end()) but I get an "Argument type mismatch" error on the callfunc_end() argument, where it expects an array.
Code:
new array[25] callfunc_begin("function", "pluginA.amxx") copy(array, sizeof(array), callfunc_end()) //Doesn't work
Afaik there are no pointers in pawn and the array returned isn't a copy but a reference/handle. How do you access the referenced array in callfunc_end()? And if there is a better way to access the array, please let me know.

Thanks in advance.

Liverwiz 06-23-2012 10:49

Re: Array returned by a function
 
In fact there ARE pointers in pawn. And that is how arrays are returned throughout the language. Example:

Code:
public arrayFunc(input[], inputSize, &outputArray[]) {     for(new i=0, i<inputSize; i++)     {         outputArray[i] = input[i] * 2     }     return 1 }

This will multiply each value in the input array and store it into the outputArray. Where you can then use outputArray to your needs.

Simply put: you need to have the return array in the parameters of the function. as shown in formatex, copy, get_players, etc etc etc
Hope i helped....

GoLoT 06-23-2012 10:59

Re: Array returned by a function
 
Quote:

Originally Posted by Liverwiz (Post 1734465)
Simply put: you need to have the return array in the parameters of the function. as shown in formatex, copy, get_players, etc etc etc
Hope i helped...

Yup, that helped a lot. Thanks. As a quick fix I sent the array in plugin B with callfunc_push_array() and copied the array in plugin A into that new array.

fysiks 06-23-2012 11:44

Re: Array returned by a function
 
Quote:

Originally Posted by Liverwiz (Post 1734465)
In fact there ARE pointers in pawn. And that is how arrays are returned throughout the language.

Arrays are never "returned," they are passed by reference. Also, you should have a length argument for the target array to prevent overflow (in general).

Liverwiz 06-23-2012 12:14

Re: Array returned by a function
 
Quote:

Originally Posted by fysiks (Post 1734500)
Arrays are never "returned," they are passed by reference. Also, you should have a length argument for the target array to prevent overflow (in general).

Irrelevant semantics. And knowing how the function operates, its unnecessary to pass a length for output, because you know that it will just multiply each cell by 2, thus making them the same amount of cells. But, yes, technically you should also have a size passed on the output.

fysiks 06-23-2012 12:25

Re: Array returned by a function
 
Quote:

Originally Posted by Liverwiz (Post 1734513)
Irrelevant semantics.

It's only semantics if you are talking to someone who doesn't know the difference.

ConnorMcLeod 06-23-2012 12:42

Re: Array returned by a function
 
Use dynamic natives and pass the array as an argument.

http://www.amxmodx.org/funcwiki.php?go=func&id=835


All times are GMT -4. The time now is 06:14.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.