PDA

View Full Version : Passing 2D array via natives


Depresie
12-12-2017, 03:17
Is there still no way to properly pass 2D arrays via a native ?

new asString[8][64]

asString[0] = "String number one"
asString[1] = "String number two"
asString[2] = "String number three"

cn_send_via_native(asString)




public native_send_via_native(plugin_id, num_params)
{
???
???
}

CrazY.
12-12-2017, 05:27
Search about [get|set]_array.

Depresie
12-12-2017, 05:40
You didn't understand what i am trying to do

Also, is there anyway to get how many times a character appears in a string other than creating a for loop and checking each cell manually ?

klippy
12-12-2017, 06:57
Try with:

native get_array_2d(param, dest[][], size) = get_array;
// To use it:
new myArray[8][64];
get_array_2d(1, myArray, sizeof(myArray) + sizeof(myArray) * sizeof(myArray[]));
// Or define something like this to not have to use that math every time and possibly make a mistake
#define 2D_ARRAY_LENGTH(%0) (sizeof(%0) + sizeof(%0) * sizeof(%0[]))

It's quite hacky but should work.

Also, is there anyway to get how many times a character appears in a string other than creating a for loop and checking each cell manually ?
Should be a new thread. As far as I know, that's the only way. You can use Regex but that will just be slower.

Depresie
12-12-2017, 08:11
Ahm, but how do i retrieve the data from the native ?

Also, this would be an overkill when sending 20 strings right ?


public plugin_natives()
{
register_native("get_array_2d", "native_array_2d")
}

public native_array_2d(plugin_id, num_params)
{
??

}

klippy
12-12-2017, 08:26
You didn't get my post.


#define ARRAY_LENGTH_2D(%0) (sizeof(%0) + sizeof(%0) * sizeof(%0[]))
native get_array_2d(param, dest[][], size) = get_array;

public plugin_natives()
{
register_native("pass_2d_array", "native_pass_2d_array");
}

// native pass_2d_array(2dArray[8][64]);
public native_pass_2d_array(pluginId, paramCount)
{
new myArray[8][64];
get_array_2d(1, myArray, ARRAY_LENGTH_2D(myArray));
}


Be aware that you have to pass an array of [8][64] size or things will go bad. That's why you specify it in the native param.

HamletEagle
12-12-2017, 10:36
Try with:

native get_array_2d(param, dest[][], size) = get_array;
// To use it:
new myArray[8][64];
get_array_2d(1, myArray, sizeof(myArray) + sizeof(myArray) * sizeof(myArray[]));
// Or define something like this to not have to use that math every time and possibly make a mistake
#define 2D_ARRAY_LENGTH(%0) (sizeof(%0) + sizeof(%0) * sizeof(%0[]))


Please no :cry:

klippy
12-12-2017, 10:53
Please no :cry:

Not encouraging the use of that ofc. Also please don't release a plugin using that. You can just use an Array: of strings anyway.

Depresie
12-12-2017, 11:00
Mhmm, i was thinking of creating a string, containing all the other strings
The beginning each string to be noted with &
Send it via native
Then loop through all chars and see how many strings there are ( how many & we find )
Then copy each string to a new 2d array

But i don't know how will that impact the performance
For example creating/sending an array of 1024/2048 chars, then loop through each char.. wouldn't it kill the server ?

What i'm trying to do for better understanding the impact on the performance

1. Create a native to allow the user to play a sequence of sounds on x interval
2. Create a native to allow the user to show a sequence of messages on x interval
3. Create a "new menu system" to play some sounds on opening/selection and modify some text inside it

E1_531G
12-12-2017, 12:47
In your case you just should use CellArray; and send the Array handler via native.

Depresie
12-12-2017, 13:15
So you say that creating a 2d array using ArrayCreate, will allow me to take the data from that array in the second plugin only by specifying it's handle ? and all i have to do is pass the handle index from one plugin to another ?

Plugin 1


new Array:g_ArrayHandle

public plugin_init()
{
g_ArrayHandle = ArrayCreate(64, 1)
}

Plugin 2

new szString[64]
ArrayGetString(g_ArrayHandle, iCell, szString, charsmax(szString))

E1_531G
12-12-2017, 14:17
Yes.
Plug-in 1.
new Array:g_ArrayHandle = ArrayCreate(64, 1)
do_something( g_ArrayHandle ) // or do_something(_:g_ArrayHandle) if "tag mismatch"
Plug-in 2.
public do_something( id, params ) // native
{
new Array:ArrH = Array:get_param(1) // Type (tag) casting, important!
...
}

HamletEagle
12-12-2017, 14:46
Another bad solution is to make 2 natives:
-something that will send to the main plugin how many strings you have in the array. Let's say x
-then the main plugin will receive x native calls, each pushing one string, and adding them to a 2d array

Depresie
12-12-2017, 15:15
What do you think it would be better in performance ?

I.
1. Creating a Dynamic Array
2. Pass the handle through native
3. Retrieve data from the Dynamic Array using the handle


II.
1. Creating an 1024 size array
2. copy all the strings in it with a special char at the beginning of each
3. Loop through 1024 cells, to find the cells that match the special char
5. Then copy the strings into a 2d array

E1_531G
12-12-2017, 15:51
Of course number I.
Handler is only one int number, not 1024 cells.
Also, CellArray works at the module level, which is a bit faster than the plugin level.