PDA

View Full Version : Detecting strings using tagof


Maxips2
21/03/2013, 01:30 PM
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:

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?

Misiur
21/03/2013, 01:33 PM
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.

Maxips2
21/03/2013, 01:38 PM
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.

Misiur
21/03/2013, 01:42 PM
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.

Maxips2
21/03/2013, 02:01 PM
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!

Maxips2
21/03/2013, 02:10 PM
I actually found a very nice way to do it.

Here is an example to test:


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 :D

Misiur
21/03/2013, 02:21 PM
If it's only single parameter


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.

Maxips2
21/03/2013, 02:22 PM
If it's only single parameter


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.

Misiur
21/03/2013, 02:28 PM
Nope, there is no reason for any conflict.