Passing any type of variable.
#1

Hello. I am making a saving system, and I want to use 1 function for all types of data (float, integer, string). I don't really have any ideas how to do it, except I saw this stuff in 'setproperty' page on wiki:
Код:
setproperty(.value = 123984334, .string = ":)");
Therefore I could do this:
Код:
stock ini_write(file, wfloat = NAN, wint = NAN, wstring[] = ";;[[;;[[")
{
     if(wfloat == wfloat){ write a float }
     else if(wint == wint){ write an integer }
     else if(strcmp(wstring, ";;[[;;[[") > 0){ write a string }
     return 1;
}

//somewhere
     new Float:posX = 1.2, vehicleid = 245, name[MAX_PLAYER_NAME] = "Bob";
     ini_write(file, .posX);
     ini_write(file, .vehicleid );
     ini_write(file, .name);
But would it work? I can't it test myself right now as I'm not home.

Thanks.
Reply
#2

You can create functions like this by reading this tutorial.
Reply
#3

Quote:
Originally Posted by Cyanide
Посмотреть сообщение
You can create functions like this by reading this tutorial.
I still need to know the type of the variable so I could operate with it properly. I'm making a saving system which obviously will use text to save things, and the tutorial you gave me doesn't help at all, because I still need to use proper specifier for 'format()', ie. I wont know whether %s %d or %f has to be used.
Reply
#4

Why not just make ini_writeString, ini_writeFloat, etc?
Reply
#5

Quote:
Originally Posted by 0x5A656578
Посмотреть сообщение
Why not just make ini_writeString, ini_writeFloat, etc?
1. It's easier to operate with files when there's one function for everything.
2. I have a feature on my server that requires exactly this type of function.
Reply
#6

I've finally got to test the code and atm I got this:
pawn Код:
public OnGameModeInit()
{
    AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
    ini_write(0, .f=0.10111);
    ini_write(0, .i=11222);
    ini_write(0, .s="noooob");
    return 1;
}

stock ini_write(file, Float:f = FLOAT_NAN, i = FLOAT_NAN, s[] = "")
{
    if(f == f)
    {
        printf("%f", f);
    }
    else if(i == i)
    {
        printf("%d", i);
    }
    else
    {
        printf("%s",s);
    }
    return 1;
}
It prints the float and the integer perfectly, but it prints the string ('s') as -1. Any ideas?
Reply
#7

pawn Код:
ini_write(0, .f=0.10111);
ini_write(0, .i=11222);
ini_write(0, .s="noooob");
How is this easier than

pawn Код:
ini_writeFloat(0, 0.10111);
ini_writeInt(0, 11222);
ini_writeString(0, "noooob");
?

You are specifying the argument type in both places anyway
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)