AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Setting default cookie value (https://forums.alliedmods.net/showthread.php?t=333120)

Blinx 06-20-2021 11:21

Setting default cookie value
 
Is there a way to set the default cookie value for a player who is joining for the first time/is joining for the first time since this cookie was added? I'd like my cookie value to default to "1" and I don't see how to do that. Thanks.

Psyk0tik 06-20-2021 11:55

Re: Setting default cookie value
 
I'm wondering this as well. What I've done is just store a default value to my variable that's supposed to hold the player's cookie value if they're joining for the first time.

You can do this by setting the default value in the "player_connect" event since that always fires before "OnClientCookiesCached". Then inside "OnClientCookiesCached" you can check whether the stored cookie value of the client is empty to determine if you should overwrite the default value that the client was assigned when they initially connected to the server.

PHP Code:

public void OnPluginStart()
{
    
HookEvent("player_connect"vEventPlayerConnectEventHookMode_Pre);
}

public 
void vEventPlayerConnect(Event event, const char[] namebool dontBroadcast)
{
    
int client GetClientOfUserId(event.GetInt("userid"));
    
g_iCookieData[client] = 1// assign default value (assuming this player doesn't have a previous cookie yet since we cannot retrieve their cookies at the time of this event)
}

public 
void OnClientCookiesCached(int client)
{
    
char sValue[3];
    
g_hCookie.Get(clientsValuesizeof(sValue));
    if (
sValue[0] != '\0'// only overwrite if cookie stored a previous value (not empty)
    
{
        
g_iCookieData[client] = StringToInt(sValue);
    }


After doing this, you can store the value of "g_iCookieData" so the client will have their cookie value saved.

Blinx 06-20-2021 13:31

Re: Setting default cookie value
 
Thank you for this, looks like an elegant solution.

MAGNAT2645 06-20-2021 15:05

Re: Setting default cookie value
 
You can just check if cookie value is empty and then set your default.
Code:

public void OnClientCookiesCached(int client) {
        char szValue[4];
        g_hCookie.Get( client, szValue, sizeof szValue );
        if ( szValue[0] == '\0' ) {
                g_iCookieValue[client] = 5;
                g_hCookie.Set( client, "5" );
        } else {
                g_iCookieValue[client] = StringToInt( szValue );
        }
}



All times are GMT -4. The time now is 12:25.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.