Need help with getarg() and arrays
#1

Hello!

So, I was writing a new include that has a function that uses getarg() to get array/string passed by a player, but the problem is only first letters are being showed, e.g. in player messages! Example code:

pawn Код:
stock myfunc(...)
{
     new msg[100];
     format(msg,sizeof(msg),"%s",getarg(0)); //to avoid error 008: must be a constant expression
     SendClientMessageToAll(-1,msg);
     return 1;
}
And if I do ... :

pawn Код:
public OnPlayerConnect(playerid)
{
     myfunc("Hello and welcome!");
     return 1;
}
... only letter "H" shows in message!

I saw that there's an optional parameter named "index", tried changing it so it wouldn't be 0, but then nothing shows!

Now, I'm using way more arrays and arguments, but I hope someone can clear this out for me on this simple example!

Thanks in advance!
Regards!
Reply
#2

That's because getarg will return only one index from the argument (hence the index parameter in getarg())

You will have to loop through all the arguments, then all the indexes of each and every argument, and then assign the values to the cells of the string (array)

Something like this:

pawn Код:
stock myfunc(...)
{
    for( new i = 0, j = numargs(); i < j; i ++ ) // Loop through all the arguments
    {
        new
            msg[ 128 ]
        ;
        for( new k = 0; getarg( i, k ) != 0; k ++ ) // Loop through all the argument(i)'s indexes
        {
            msg[ k ] = getarg( i, k ); // Assign character to string (array)

                    // As I explained above: getarg( i, k ) will return the index 'k' of the argument 'i'.
                    // Each time this loops it will return one character.
        }
        SendClientMessageToAll( -1, msg );
    }
    return true;
}
Reply
#3

Thanks for clearing it out for me, works now!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)