Raised This Month: $12 Target: $400
 3% 

Setting default cookie value


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Blinx
Senior Member
Join Date: Oct 2013
Old 06-20-2021 , 11:21   Setting default cookie value
Reply With Quote #1

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.
Blinx is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 06-20-2021 , 11:55   Re: Setting default cookie value
Reply With Quote #2

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.
__________________
Psyk0tik is offline
Blinx
Senior Member
Join Date: Oct 2013
Old 06-20-2021 , 13:31   Re: Setting default cookie value
Reply With Quote #3

Thank you for this, looks like an elegant solution.
Blinx is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 06-20-2021 , 15:05   Re: Setting default cookie value
Reply With Quote #4

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 );
	}
}
__________________
MAGNAT2645 is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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