Raised This Month: $ Target: $400
 0% 

Array returned by a function


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
GoLoT
New Member
Join Date: Nov 2008
Old 06-23-2012 , 10:29   Array returned by a function
Reply With Quote #1

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.
GoLoT is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 06-23-2012 , 10:49   Re: Array returned by a function
Reply With Quote #2

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....
__________________
What an elegant solution to a problem that doesn't need solving....

Last edited by Liverwiz; 06-23-2012 at 10:51.
Liverwiz is offline
GoLoT
New Member
Join Date: Nov 2008
Old 06-23-2012 , 10:59   Re: Array returned by a function
Reply With Quote #3

Quote:
Originally Posted by Liverwiz View Post
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.
GoLoT is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-23-2012 , 11:44   Re: Array returned by a function
Reply With Quote #4

Quote:
Originally Posted by Liverwiz View Post
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).
__________________

Last edited by fysiks; 06-23-2012 at 11:45.
fysiks is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 06-23-2012 , 12:14   Re: Array returned by a function
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
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.
__________________
What an elegant solution to a problem that doesn't need solving....
Liverwiz is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-23-2012 , 12:25   Re: Array returned by a function
Reply With Quote #6

Quote:
Originally Posted by Liverwiz View Post
Irrelevant semantics.
It's only semantics if you are talking to someone who doesn't know the difference.
__________________
fysiks is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 06-23-2012 , 12:42   Re: Array returned by a function
Reply With Quote #7

Use dynamic natives and pass the array as an argument.

http://www.amxmodx.org/funcwiki.php?go=func&id=835
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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