How to get the size of an array from Pawn
#1

How to get the size of an array passed to a native function?

Let's say i have this native definition in the pwn file:
pawn Code:
native Bla(a1[], a2[]);

//then i use like this:
Bla( {1, 13}, {2, 3, 1, 7} );
I need the size of a1 and a2 (2 and 4 in this example), in my plugin. How to do this?

I know a trick to get the size of ONE array if there is only one argument in the native definition, but for 2 arrays as 2 different arguments, i'm stuck!


Edit: ok i can get the size of the first array by doing this:
pawn Code:
static cell AMX_NATIVE_CALL Bla(AMX *amx, cell *p)
{
  printf("sizeof a1 = %d\n", (p[2]-p[1])/4);
  //printf("sizeof a2 = %d\n", ?????);
  return 0;
}
So this is basically all i needed, but if i could get the size of a2 it still would be nice...
Reply
#2

You could just add additional parameters: size1 = sizeof a1, size2 = sizeof a2
Reply
#3

But i don't want this because my function is defined like this, in fact:
pawn Code:
native Bla(...);
Sorry i should have said that in first post. My goal is to make a 'universal' function, which accept undefined amount of arrays and non arrays parameters.
Reply
#4

I don't need the amount of parameters but the size of the arrays i pass to the native function.
Reply
#5

For what i need to do this: floats, ints, arrays of floats and arrays of ints.


Here is an example of what i have actually:

pawn Code:
static cell AMX_NATIVE_CALL GetDistance(AMX *amx, cell *p)
{
  cell *c1 = 0;
  float f1 = 0.0f;
  float f2 = 0.0f;
  float f3 = 0.0f;

  amx_GetAddr(amx, p[1] , &c1);

  switch (p[0]/4) //switch between amount of parameters
  {
    case 2 : //GetDistance(Float:point1[], Float:point2[])
    {
      if ((p[2]-p[1])/4 == 2) //if first array is a 2d point
      {
        f1 = amx_ctof(c1[0]) - amx_ctof(c1[2]);
        f2 = amx_ctof(c1[1]) - amx_ctof(c1[3]);
        return amx_ftoc((f1 = sqrt(f1*f1 + f2*f2)));
      }
      else
      {
        f1 = amx_ctof(c1[0]) - amx_ctof(c1[3]);
        f2 = amx_ctof(c1[1]) - amx_ctof(c1[4]);
        f3 = amx_ctof(c1[2]) - amx_ctof(c1[5]);
        return amx_ftoc((f1 = sqrt(f1*f1 + f2*f2 + f3*f3)));
      }
    }

    case 4 : //GetDistance(Float:x1, Float:y1, Float:x2, Float:y2)
    {
      f1 = amx_ctof(c1[0] ) - amx_ctof(c1[0]-2);
      f2 = amx_ctof(c1[0]-1) - amx_ctof(c1[0]-3);
      return amx_ftoc((f1 = sqrt(f1*f1 + f2*f2)));
    }

    case 6 : //GetDistance(Float:x1, Float:y1, Float:z2, Float:x2, Float:y2, Float:z2)
    {
      f1 = amx_ctof(c1[0] ) - amx_ctof(c1[0]-3);
      f2 = amx_ctof(c1[0]-1) - amx_ctof(c1[0]-4);
      f3 = amx_ctof(c1[0]-2) - amx_ctof(c1[0]-5);
      return amx_ftoc((f1 = sqrt(f1*f1 + f2*f2 + f3*f3)));
    }
  }

  return amx_ftoc((f1 = POS_INFINITY));
}

It can be used like this in Pawn actually:
pawn Code:
native Float:GetDistance({Float,_}:...);

#define x1  0.0
#define y1  0.0
#define z1  0.0
#define x2  10.0
#define y2  10.0
#define z2  10.0

printf("%f", GetDistance(x1, y1, x2, y2));
printf("%f", GetDistance(Float:{x1, y1}, Float:{x2, y2}));
printf("%f", GetDistance(x1, y1, z1, x2, y2, z2));
printf("%f", GetDistance(Float:{x1, y1, z1}, Float:{x2, y2, z2}));

If there is a way to get the size of the arrays (i mean, the size of each parameters in fact) passed into it, i could expand it to allow this:
pawn Code:
GetDistance(x1, y1, Float:{x2, y2});
//or
GetDistance(Float:{x1, y1}, x2, y2);
//and etc..
So what i would like is something like this, in the plugin, for example:
pawn Code:
if (sizeofparam(parameter[2]) == 2) //parameter[2] is an array for a 2d point
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)