AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved Copying 2D arrays by natives (https://forums.alliedmods.net/showthread.php?t=287567)

shavit 09-10-2016 08:18

Copying 2D arrays by natives
 
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?

Fyren 09-10-2016 10:10

Re: Copying 2D arrays by natives
 
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).

shavit 09-10-2016 11:40

Re: Copying 2D arrays by natives
 
Quote:

Originally Posted by Fyren (Post 2452751)
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 :)

klippy 09-10-2016 14:23

Re: Copying 2D arrays by natives
 
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)

Fyren 09-10-2016 19:08

Re: Copying 2D arrays by natives
 
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.

BAILOPAN 09-26-2016 18:05

Re: Copying 2D arrays by natives
 
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.

shavit 09-26-2016 21:22

Re: Copying 2D arrays by natives
 
Quote:

Originally Posted by BAILOPAN (Post 2457320)
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 :)

Potato Uno 09-26-2016 21:34

Re: Copying 2D arrays by natives
 
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.


All times are GMT -4. The time now is 11:40.

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