Detecting strings using tagof
#1

Hey,

I was trying to utilise tagof to pass a float/integer or a string value using one function.
It works great for integer and float values, however I have no idea how to check if its a string or if its even possible.

This is the body of the function:

pawn Код:
stock MySQL_SaveCharacterStat(playerid, E_CHARACTER_INFO:stat, field[], {_, Float}:value, type = tagof(value))
Inside the function I'm using switch to check the tag of the value.

Any ideas? or is it even possible to do what I'm trying?
Reply
#2

Nope. As you know, string is an array of chars. Your argument "value" is a single-celled organism, and won't accept string anyway. Tagof characters array will return tag of tagless variable.
Reply
#3

Quote:
Originally Posted by Misiur
Посмотреть сообщение
Nope. As you know, string is an array of chars. Your argument "value" is a single-celled organism, and won't accept string anyway. Tagof characters array will return tag of tagless variable.
Yes I know what is a string.

I am aware of everything you are saying here, but I just wanted to know if there is any way of doing it.
What I showed here is what I currently have and I know that it doesn't support strings that's why I am asking for an alternative way.
Reply
#4

One option would be to use varargs. function(...), with accessing via getarg. Other would require use of pointers, and implementing custom String pointer tag. You've probably seen how (for example) CallLocalFunction handles argument types: you pass the format and use it as info of variable types.
Reply
#5

Actually this is not going to work, I have changed the function because it was done wrong but there is no way to do it unfortunatley.

Thanks for help anyway!
Reply
#6

I actually found a very nice way to do it.

Here is an example to test:

pawn Код:
stock MySQL_SaveCharacterStat(playerid, field[], E_CHARACTER_INFO:stat, format[])
{
    switch(format[0])
    {
        case 'i', 'd': printf("%d", gCharacterInfo[playerid][stat]);
        case 'f': printf("%f", gCharacterInfo[playerid][stat]);
        case 's': printf("%s", gCharacterInfo[playerid][stat]);
    }
}
Tested, working great
Reply
#7

If it's only single parameter

pawn Код:
stock MySQL_SaveCharacterStat(playerid, field[], E_CHARACTER_INFO:stat, format)
{
    switch(format)
    {
        case 'i', 'd': printf("%d", gCharacterInfo[playerid][stat]);
        case 'f': printf("%f", gCharacterInfo[playerid][stat]);
        case 's': printf("%s", gCharacterInfo[playerid][stat]);
    }
}
And you pass the parameter with single quotes.
Reply
#8

Quote:
Originally Posted by Misiur
Посмотреть сообщение
If it's only single parameter

pawn Код:
stock MySQL_SaveCharacterStat(playerid, field[], E_CHARACTER_INFO:stat, format)
{
    switch(format)
    {
        case 'i', 'd': printf("%d", gCharacterInfo[playerid][stat]);
        case 'f': printf("%f", gCharacterInfo[playerid][stat]);
        case 's': printf("%s", gCharacterInfo[playerid][stat]);
    }
}
And you pass the parameter with single quotes.
I thought about doing that, but I thought that it might cause conflicts.
Reply
#9

Nope, there is no reason for any conflict.
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)