How to make optional argument in some function?
#1

My question is, how i can make that some agument can be negligible.

I trying to make fnc 'formatEx' - example of what i trying to make:

pawn Код:
formatEx(playerid, cells, text[], argument1, argument2, argument3)
{
      new
          string[cells];
      format(string, sizeof string, text, argument1, argument2, argument3);
      SendClientMessage(playerid, -1, string);
      return true;
}
BUT! Sometimes I will not use 3 arguments, maybe more or maybe less
SO how i can make that arguments can be negligible? - Example if we use this 'formatEx' fnc in this example:

pawn Код:
formatEx(playerid, 128, "%s %s %d %d %i %s %d",FNC, FNC, FNC, FNC, FNC, FNC);
Ofc it won't work :S

Please, can someone give me some example how to do this ( if you understand me ) :-/
Reply
#2

Here is an example:
pawn Код:
stock GivePlayerWeaponEx(playerid, ...) //By Peter
{
    new Count = numargs();
    for(new i = 1; i < Count; i += 2)
    {
        new WeaponID = getarg(i);
        new Ammo = getarg((i+1));
        if(WeaponID > 0 && WeaponID < 47)
        {
            GivePlayerWeaponAlt(playerid, WeaponID, Ammo);
        }
    }
    return 1;
}
Reply
#3

I think if you're using floats then you need it like this
Function(Arg1,{Float,_}:...)

just ask if you need help dealing with the optional args
Reply
#4

I Make this:

pawn Код:
formatForPlayer(playerid, cells, const balkan[], const english[], ...)
{
    new
       ArgumentiCount = numargs(),
       string[2][sizeof (cells)];

    for(new i = 1; i < ArgumentiCount; i += 5)
    {
        new
           arg1 = getarg(i),
           arg2 = getarg((i+1)),
           arg3 = getarg((i+1)),
           arg4 = getarg((i+1)),
           arg5 = getarg((i+1));

        if(IsPlayerLanguage(playerid, JEZIK_BALKAN))
        {
            format(string[0], cells, balkan, arg1, arg2, arg3, arg4, arg5);
            SCMEx(playerid, string[0]);
            return true;
        }
        else if(IsPlayerLanguage(playerid, JEZIK_ENGLISH))
        {
            format(string[1], cells, english, arg1, arg2, arg3, arg4, arg5);
            SCMEx(playerid, string[1]);
            return true;
        }

    }
    return true;
}
And when i use this:

pawn Код:
formatForPlayer(playerid, 128, "BALKAN TEXT: %s", "ENGLISH TEXT: %s", GetName(playerid));
it won't work, i don't get any error/warnning but it won't work....

Can you give me some help?
Reply
#5

I tried writing this long tutorial about it, but it would of been too confusing here's what you'll need
getarg(x,y) - this gets an argument from a function
first parameter: position of the argument in relation to all other arguments
second parameter is if its an array then it gets that index. (this is optional)

Function(123,"hola",1.22);
using getarg for a string
pawn Код:
new tstr[29];
for(new j=0;j<sizeof(tstr);j++)
{
    tstr[j] = getarg(1,j);
}
print(tstr);
using getarg on a decimal
pawn Код:
getarg(0);
using getarg on a float
pawn Код:
Float:getarg(2);
numargs() - count total of all arguments
using numargs() in Function(123,"hola",1.22); will return 3 because there are 3 arguments

if you want a working example just look at the cini include I made in my sig
Reply
#6

I am very intrested in this formatEx also...
Reply
#7

Quote:
Originally Posted by cessil
Посмотреть сообщение
I tried writing this long tutorial about it
I'd you've got a lot of spare time left, please try it one mother time D:
Reply
#8

Quote:
Originally Posted by cessil
Посмотреть сообщение
I tried writing this long tutorial about it, but it would of been too confusing here's what you'll need
getarg(x,y) - this gets an argument from a function
first parameter: position of the argument in relation to all other arguments
second parameter is if its an array then it gets that index. (this is optional)

Function(123,"hola",1.22);
using getarg for a string
pawn Код:
new tstr[29];
for(new j=0;j<sizeof(tstr);j++)
{
    tstr[j] = getarg(1,j);
}
print(tstr);
using getarg on a decimal
pawn Код:
getarg(0);
using getarg on a float
pawn Код:
Float:getarg(2);
numargs() - count total of all arguments
using numargs() in Function(123,"hola",1.22); will return 3 because there are 3 arguments

if you want a working example just look at the cini include I made in my sig
I think you should make a tutorial about it.
Reply
#9

There is an 'old' (I say old because the last reply there is since 2010) tutorial about this, might help:

https://sampforum.blast.hk/showthread.php?tid=77000
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)