Raised This Month: $12 Target: $400
 3% 

Solved Copying 2D arrays by natives


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
shavit
AlliedModders Donor
Join Date: Dec 2011
Location: Israel
Old 09-10-2016 , 08:18   Copying 2D arrays by natives
Reply With Quote #1

I have this:
Code:
native void GetMyArray(any thearray[1D_SIZE][2D_SIZE]);

public int Native_GetMyArray(Handle handler, int numParams)
{
	/* set array in native */
}
And this inside the main plugin, and the one I want to copy the array values to:
Code:
any thearray[1D_SIZE][2D_SIZE];
What I want to achieve, is something like that (in a separate plugin):
Code:
public void OnPluginStart()
{
	any thearray[1D_SIZE][2D_SIZE];
	GetMyArray(thearray);
}
How could I copy the whole array with a native? Would I have to do separate calls per each 1D cell?
__________________
retired

Last edited by shavit; 09-10-2016 at 11:40.
shavit is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 09-10-2016 , 10:10   Re: Copying 2D arrays by natives
Reply With Quote #2

As you've noticed, there's no GetNativeArray2D (or 3D, or 4D). You can't do anything but pass 1D arrays for real arrays. If it's not too inconvenient, you could instead use ADT arrays (and provide helper stocks to convert, maybe).
Fyren is offline
shavit
AlliedModders Donor
Join Date: Dec 2011
Location: Israel
Old 09-10-2016 , 11:40   Re: Copying 2D arrays by natives
Reply With Quote #3

Quote:
Originally Posted by Fyren View Post
As you've noticed, there's no GetNativeArray2D (or 3D, or 4D). You can't do anything but pass 1D arrays for real arrays. If it's not too inconvenient, you could instead use ADT arrays (and provide helper stocks to convert, maybe).
I'll convert 1D cells instead, thanks
__________________
retired
shavit is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 09-10-2016 , 14:23   Re: Copying 2D arrays by natives
Reply With Quote #4

It may be possible to hack around that. Try with this:
PHP Code:
native int GetNativeArray2D(int paramany[][] localint sizecells) = GetNativeArray;

public 
int Native_PassMy2DArray(Handle pluginint numParams)
{
    
int my2DArray[3][6];
    
GetNativeArray2D(1view_as<any>(my2DArray), 6);
    
    for(
int i 03i++) {
        for(
int j 06j++) {
            
PrintToServer("my2DArray[%d][%d] = %d"ijmy2DArray[i][j]);
        }
    }
}

public 
APLRes AskPluginLoad2(Handle myselfbool latechar[] errorint err_max)
{
    
CreateNative("PassMy2DArray"Native_PassMy2DArray);
    
    return 
APLRes_Success;

And in your sub-plugin:
PHP Code:
native void PassMy2DArray(int[][] arr);

public 
void OnPluginStart()
{
    
int myArray[3][6] = {
        {
123456},
        {
789101112},
        {
131415161718}
    };
    
    
PassMy2DArray(myArray);

I'm really interested if that would work, because I have just tested a similar hack on AMXX and it worked.

(please don't kill me for proposing this)

Last edited by klippy; 09-10-2016 at 14:26.
klippy is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 09-10-2016 , 19:08   Re: Copying 2D arrays by natives
Reply With Quote #5

I think that should work as long as you get the size right by including the indirection offsets (as you did). There are cases where you can't do that in general, but I suppose as long as you're certain of the array dimensions this functions as desired.
Fyren is offline
BAILOPAN
Join Date: Jan 2004
Old 09-26-2016 , 18:05   Re: Copying 2D arrays by natives
Reply With Quote #6

This may break horribly in future versions of SP, so I wouldn't recommend doing it. If you want an API for 2D arrays just request one! It shouldn't be hard.
__________________
egg
BAILOPAN is offline
shavit
AlliedModders Donor
Join Date: Dec 2011
Location: Israel
Old 09-26-2016 , 21:22   Re: Copying 2D arrays by natives
Reply With Quote #7

Quote:
Originally Posted by BAILOPAN View Post
This may break horribly in future versions of SP, so I wouldn't recommend doing it. If you want an API for 2D arrays just request one! It shouldn't be hard.
Thanks BAILOPAN, I'll file a request on Bugzilla
__________________
retired
shavit is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 09-26-2016 , 21:34   Re: Copying 2D arrays by natives
Reply With Quote #8

That's interesting because there are functions like SortCustom2D that accept a double array. Maybe C++ has a better grip on native params (cell_t *params) than pawn does.
Potato Uno 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 12:40.


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