A way to reference array
#1

Is there a way to reference an array?
Reply
#2

Hi T0pAz,

Reference an array, I assume you mean retrieve a variable from an array?

Cheers,

TJ
Reply
#3

Quote:
Originally Posted by TTJJ
Посмотреть сообщение
Hi T0pAz,

Reference an array, I assume you mean retrieve a variable from an array?

Cheers,

TJ
I want to do something like this.

pawn Код:
public Somthing(&arr[])
Reply
#4

Hi T0pAz,

Ok, there is no need to use the & symbol in this instance. So if you want to pass an array to a function, the following would be a good example:
pawn Код:
public OnGameModeInit()
{

    new arr[10];
    ProcessMyArray(arr);

}

public ProcessMyArr(array[])
{

    //Do something...

}
I believe that is what you are looking for?

Cheers,

TJ
Reply
#5

Quote:
Originally Posted by TTJJ
Посмотреть сообщение
Hi T0pAz,

Ok, there is no need to use the & symbol in this instance. So if you want to pass an array to a function, the following would be a good example:
pawn Код:
public OnGameModeInit()
{

    new arr[10];
    ProcessMyArray(arr);

}

public ProcessMyArr(array[])
{

    //Do something...

}
I believe that is what you are looking for?

Cheers,

TJ
But I tend to not use format.
Reply
#6

arrays are more like strings in pawn so do it like string.
example:
str[8]="hello";
str[3] would be l.
so if hello[12] is an array try
pawn Код:
return hello;
Reply
#7

Quote:
Originally Posted by eesh
Посмотреть сообщение
arrays are more like strings in pawn so do it like string.
example:
str[8]="hello";
str[3] would be l.
so if hello[12] is an array try
pawn Код:
return hello;
Arrays cannot be returned.
Reply
#8

Hi Again,

The way I return formatted strings is the following:
pawn Код:
public doIt(str[],length)
{

    new stuff = 1;
    return format(str,length,"%d",stuff);

}

public OnPlayerConnect(playerid)
{

    new str[32];
    doIt(str,32);

}
That may be of assistance..

Cheers,

TJ
Reply
#9

You don't need to reference arrays..

Because, In simple words, when you pass an array to a function, the changes occur to the same variable (mem address).

But for Ints, Floats, when you pass to a function, they don't change themselves, they transfer their 'values'.
So, if you want to make changes to their actually values, you need to pass them as references..

pawn Код:
#include <a_samp>

public OnFilterScriptInit()
{
    new myINT = 47, Float:myFLT = 2.4751;
   
    DoubleUp_NoRef( myINT, myFLT );
    printf("%i %f", myINT, myFLT);
    //prints: 47 2.4751
    //No changes to our local variables.
   
    DoubleUp( myINT, myFLT );
    printf("%i %f", myINT, myFLT);
    //prints: 94 4.95
    //The values of our local variables has changed.
   
    new myARRAY[32] = "sometext";
   
    DoubleUpArray( myARRAY );
    printf("%s", myARRAY);
    //prints: sometextsometext
    //Even though the Function didn't have referrence arrays
    //The local variable has changed it's content.
   
    return true;
}

stock DoubleUp( &INT, &Float:FLT )
{
    INT *= 2;
    FLT *= 2;
    return true;
}

stock DoubleUp_NoRef( INT, Float:FLT )
{
    INT *= 2;
    FLT *= 2;
    return true;
}

stock DoubleUpArray( Array[], size = sizeof Array )
{
    strcat(Array, Array, size);
    return true;
}
Reply
#10

Quote:
Originally Posted by iPLEOMAX
Посмотреть сообщение
You don't need to reference arrays..

Because, In simple words, when you pass an array to a function, the changes occur to the same variable (mem address).

But for Ints, Floats, when you pass to a function, they don't change themselves, they transfer their 'values'.
So, if you want to make changes to their actually values, you need to pass them as references..

pawn Код:
#include <a_samp>

public OnFilterScriptInit()
{
    new myINT = 47, Float:myFLT = 2.4751;
   
    DoubleUp( myINT, myFLT );
   
    printf("%i %f", myINT, myFLT);
   
    new myARRAY[32] = "sometext";
   
    DoubleUpArray( myARRAY );
   
    printf("%s", myARRAY);
   
    return true;
}

stock DoubleUp( &INT, &Float:FLT )
{
    INT *= 2;
    FLT *= 2;
    return true;
}

stock DoubleUpArray( Array[], size = sizeof Array )
{
    strcat(Array, Array, size);
    return true;
}
Thanks man!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)