Getting argument type?
#1

Is there any way you can possibly identify an argument's type. Currently i am only working on single index arguments, no arrays.

So if i have a function like this:
pawn Код:
MyFunc({_, Float}:...)
{
}
How can i do such thing:
pawn Код:
MyFunc({_, Float}:...)
{
    if (its only a number)
    {
    }
    else if (its only a float)
    {
    }
    else //its a string
    {
    }
}
Reply
#2

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

https://sampwiki.blast.hk/wiki/Keywords:Operators#tagof
Reply
#3

pawn Код:
// ** INCLUDES

#include <a_samp>

// ** MAIN

main()
{
    print("Loaded \"argument_type.amx\".");

    Function(""); // "2" = integer, "2.0" = float, "test" = string
}

// ** CALLBACKS

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

// ** FUNCTIONS

stock Function({_, Float}:...)
{
    new string[144], bool:character = false, bool:point = false, bool:number = false;
    for(new i = 0, j = sizeof(string); i < j; i ++)
    {
        string[i] = getarg(0, i);
    }

    for(new i = 0, j = strlen(string); i < j; i ++)
    {
        if(!character)
        {
            switch(string[i])
            {
                case 'A'..'z':
                {
                    character = true;
                }  
            }
        }

        if(!point)
        {
            switch(string[i])
            {
                case '.':
                {
                    point = true;
                }
            }
        }

        if(!number)
        {
            switch(string[i])
            {
                case '0'..'9':
                {
                    number = true;
                }
            }
        }
    }

    if(character)
    {
        print("string");
    }
    else if(number && point)
    {
        print("float");
    }
    else
    {
        print("integer");
    }
    return 1;
}
Remember to put the arguments in double quotes.
Reply
#4

tagof only works for elements with TAGS, integer and strings doesn't have one.

So how can we detect those 2?

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
Remember to put the arguments in double quotes.
That's what i don't want. I need to input any type and then detect internally. (idk if its possible in PAWN)
Reply
#5

Try:
PHP код:
if(tagof(var) == tagof(_:)) 
Reply
#6

Quote:
Originally Posted by Vince
Посмотреть сообщение
Try:
PHP код:
if(tagof(var) == tagof(_:)) 
Doesn't work for numbers without (_:) tag.
Reply
#7

There is really no way to detect it you need to pass the format of argument like in functions like mysql_tquery, where you pass the format of variable to be passed to callback as a string.
tagof doesn't works with getarg directly it gives error.
For example:
PHP код:
tagof(getarg(0)); // gives error won't compile 
So best option is to make your function something like
PHP код:
//example call to function someFunction("dfd", 3, 45, 55.5, 8)
stock someFunction(args[],  sizeof(args), ...)
{
     for(new 
0si++)
     {
         switch(
getarg(0i))
         {
             case 
'd':{ /* interger */ }
             case 
'f':{ /* float */ }
             
//so on
         
}
     }

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)