Sscanfiy this - Code Help
#1

Hi

I don't see lots of lines like this in others code. Is there an easier way of setting the DB data into the playerVariables enum maybe using sscanf.

Help please!

pawn Код:
public LoadPlayerData(playerid, account[])
{
    new rows, fields;
    cache_get_data(rows, fields);

    if(rows) {

        cache_get_row(0, 0, szQueryOutput); // pInternalID
        playerVariables[playerid][pInternalID] = strval(szQueryOutput);
       
        playerVariables[playerid][pLogged] = 1;
       
        cache_get_row(0, 3, szQueryOutput);
        playerVariables[playerid][pSkin] =  strval(szQueryOutput);
       
        cache_get_row(0, 4, szQueryOutput);
        playerVariables[playerid][pMoney] = strval(szQueryOutput);
       
        cache_get_row(0, 5, szQueryOutput);
        playerVariables[playerid][pBank] = strval(szQueryOutput);
       
        cache_get_row(0, 6, szQueryOutput);
        playerVariables[playerid][pSpawnX] = floatstr(szQueryOutput);
       
        cache_get_row(0, 7, szQueryOutput);
        playerVariables[playerid][pSpawnY] = floatstr(szQueryOutput);
       
        cache_get_row(0, 8, szQueryOutput);
        playerVariables[playerid][pSpawnZ] = floatstr(szQueryOutput);
       
        cache_get_row(0, 9, szQueryOutput);
        playerVariables[playerid][pSpawnA] = floatstr(szQueryOutput);
       
        cache_get_row(0, 10, szQueryOutput);
        playerVariables[playerid][pWs1] = strval(szQueryOutput);
       
        cache_get_row(0, 11, szQueryOutput);
        playerVariables[playerid][pWa1] = strval(szQueryOutput);
       
        cache_get_row(0, 12, szQueryOutput);
        playerVariables[playerid][pWs2] = strval(szQueryOutput);
       
        cache_get_row(0, 13, szQueryOutput);
        playerVariables[playerid][pWa2] = strval(szQueryOutput);
       
        cache_get_row(0, 14, szQueryOutput);
        playerVariables[playerid][pWs3] = strval(szQueryOutput);
       
        cache_get_row(0, 15, szQueryOutput);
        playerVariables[playerid][pWa3] = strval(szQueryOutput);
       
        cache_get_row(0, 16, szQueryOutput);
        playerVariables[playerid][pHasDrivingLicence] = strval(szQueryOutput);
       
        cache_get_row(0, 17, szQueryOutput);
        playerVariables[playerid][pHasBoatLicence] = strval(szQueryOutput);
       
        cache_get_row(0, 18, szQueryOutput);
        playerVariables[playerid][pHasPilotLicence] = strval(szQueryOutput);
       
        cache_get_row(0, 19, szQueryOutput);
        playerVariables[playerid][pHasFishingLicence] = strval(szQueryOutput);
       
       
        SetSpawnInfo(playerid,0,playerVariables[playerid][pSkin],playerVariables[playerid][pSpawnX],playerVariables[playerid][pSpawnY],playerVariables[playerid][pSpawnZ],playerVariables[playerid][pSpawnA],-1,-1,-1,-1,-1,-1);
        SpawnPlayer(playerid);
        GivePlayerMoney(playerid,playerVariables[playerid][pMoney]);

        format(szString, sizeof(szString), "[SERVER]: Welcome back to Bayside RP, %s! Please follow the rules.", account);
        SendClientMessage(playerid, COLOR_GREEN, szString);
       
        TogglePlayerSpectating(playerid, false);
    }
    else
    {
        format(szString, sizeof(szString), "{FF0000}Sorry, %s!", account);
        ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, szString, "{FF0000}The password is WRONG!\n{FFFFFF}Enter your password below:", "Login", "Cancel");
    }

    return 1;
}
Reply
#2

Lines don't matter! What would you prefer, something that is done faster but has few more lines or the opposite?!

By the way, you can reduce the lines by using cache_get_row_(int/float).

pawn Код:
// An example:
playerVariables[playerid][pInternalID] = cache_get_row_int(0, 0);
// Instead of:
cache_get_row(0, 0, szQueryOutput); // pInternalID
playerVariables[playerid][pInternalID] = strval(szQueryOutput);
Reply
#3

Quote:
Originally Posted by Dwane
Посмотреть сообщение
Lines don't matter! What would you prefer, something that is done faster but has few more lines or the opposite?!

By the way, you can reduce the lines by using cache_get_row_(int/float).

pawn Код:
// An example:
playerVariables[playerid][pInternalID] = cache_get_row_int(0, 0);
// Instead of:
cache_get_row(0, 0, szQueryOutput); // pInternalID
playerVariables[playerid][pInternalID] = strval(szQueryOutput);
Those functions aren't in R7. You can however refer to this to get cache_get_row_int:

http://forum.sa-mp.com/showpost.php?...&postcount=935
Reply
#4

Quote:
Originally Posted by SuperViper
Посмотреть сообщение
Those functions aren't in R7. You can however refer to this to get cache_get_row_int:

http://forum.sa-mp.com/showpost.php?...&postcount=935
He didn't mention if he's using R7 or the custom one (r15). I'd recomment to use r15, since it's faster than getting the data, storing it to a variable and then using strval to convert it.
Reply
#5

R15 isn't as stable as R7 and it offers minimal new features.
Reply
#6

I'm using R15 and I have absolutely no problem with it. It's not as stable as R7, but it offers features even if they're minimal. I'd prefer to use an updated version!

Quote:

* Added cache_get_row_int and cache_get_row_float.
* More security added to internal string handling to prevent buffer overflows.
* Fixed crash issue on gamemode restart (gmx).
* A few more internal issues cleared up.

Reply
#7

Im using R15 I think, because I can use cache_get_row_float so I must be right??

I guess if it works, I just don't seem to see lots of repetitive code like that. Besides im lazy if I can code less to get more I will.
Reply
#8

I'm no expert with mySQL, but I'm fairly certain a string with delimiters is returned like so:

SELECT Cash, Score FROM players WHERE blah blah

1337|69|blah|blah|blah

Why not use sscanf on that with '|' as the delimiter?
Reply
#9

Caching works differently.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)