[Tutorial] ClientPrefs
Alrighty, so using ClientPrefs is pretty straight forward. However, not a lot of plugins include support for it. Hopefully this helps someone.
The first thing you need to determine is what you need to store. Is it just a simple on off value? A name? In this case, let's store a simple bool. PHP Code:
PHP Code:
PHP Code:
We now need to define our CookieMenuHandler. The handler is pretty straight forward if you've used the menu API. If not, you may need to brush up on Menus. In this specific case, we only need to worry about the CookieMenuAction_SelectOption. We need to simply cover setting the boolean so our internal state holds consistent. The major failure of the API is when the setting changes, buffer is always "", which makes me sad, but oh well. PHP Code:
PHP Code:
Last but not least, the magical OnClientCookiesCached forward. Here you're just reading a value out of a cookie. You can definitely register multiple cookies, or no cookies. OnClientCookiesCached has no defined order, and can happen before or after OnClientPostAdminCheck. If you have a protected admin cookie, you're going to need to have your own custom handler, which is beyond the scope of this tutorial (at the moment). In our case, we're reading a simple one byte value from a String. In our case, we're going to use 8 bytes. PHP Code:
EDIT: The final Plugin Looks Like: PHP Code:
|
Re: [Tutorial] ClientPrefs
Thank you... learned something new about SetCookiePrefabMenu
|
Re: [Tutorial] ClientPrefs
Thanks :)
The way you loop through players is unique :O |
Re: [Tutorial] ClientPrefs
Appreciate the example! Any quick leads on how to have a specific message pop up when the client select on or off in the menu? Can't seem to find much after searching a bit.
|
Re: [Tutorial] ClientPrefs
Quote:
|
Re: [Tutorial] ClientPrefs
Quote:
Spoiler
|
Re: [Tutorial] ClientPrefs
Quote:
|
Re: [Tutorial] ClientPrefs
I don't get it... Why my cookie are always lost when I reload my plugin and players are in game ?
I can restart my server and player have their correct cookie [OK] I can restart map and player have their correct cookie [OK] I reload my plugin and players are in game, they lost their cookies !!! [X] NOTE: I didn't understand everything you wrote, so I'm not sure if you speak about this... Original thread |
Re: [Tutorial] ClientPrefs
OnClientCookiesCached will only fire when they connect. If you yeload the plugin, you need to loop over the players and do AreClientCookiesCached in OnPluginStart... Look at the example.
|
Re: [Tutorial] ClientPrefs
Yep, I did, but cookies still lost.
|
Re: [Tutorial] ClientPrefs
Quote:
|
Re: [Tutorial] ClientPrefs
(plugin bugged. removed.)
|
Re: [Tutorial] ClientPrefs
Try to simply run OnClientPutInServer() for every InGame client OnPluginStart() rather than L33-37. EDIT nvm; misread code; didn't notice at 2 places you were getting client cookies |
Re: [Tutorial] ClientPrefs
Quote:
Quote:
EDIT: Unrelated to ClientPrefs, you should return Plugin_Stop in your Timer, otherwise you're leaking timers. Your plugin will eventually auto-unload after enough clients connect, from running out of Handles. You should also pass the flag TIMER_FLAG_NO_MAPCHANGE, as all of your timers should die then anyways. |
Re: [Tutorial] ClientPrefs
...
Ok Mabye you are writing over them. Are you by chance using onclientputinserver to initialize stuff! Because careclientcookiescached can happen before or after that. You may want to use onclientdonnected instead, to pre-initialize vars while you wait for cookies to load in. It may be that they are loading, but you are overwriting them with junk data later. Edit:didnt see this second page, but looks like is still probly relevant. |
Re: [Tutorial] ClientPrefs
Oka, thanks for help. I updated this plugin.
|
Re: [Tutorial] ClientPrefs
Does clientprefs keep track of which plugin assigned which cookie? Or should plugin authors be careful to ensure that the cookie name is unique?
|
Re: [Tutorial] ClientPrefs
Quote:
Quote:
Code:
|
Re: [Tutorial] ClientPrefs
Good to know. I recently saw a plugin (not on this site) that named its cookie "cookie." Was wondering if this might conflict with any other plugin with the same non-descriptive cookie name. Now I know it will lol
|
Re: [Tutorial] ClientPrefs
Should also mention that cookies should not be used to store stats, as in time played. It makes it nearly impossible to reset all the players in that cookie back to 0, etc. Should only be used as preferences.
|
Re: [Tutorial] ClientPrefs
Quote:
|
Re: [Tutorial] ClientPrefs
Quote:
(Actually, your plugin already covers the one exception to that rule) |
Re: [Tutorial] ClientPrefs
I tend to avoid making cookies for FakeClients because well... they'll never use them.
|
Re: [Tutorial] ClientPrefs
Well, my cookies are restored when I'm on my server and I manually reload the plugin (sm plugins reload XXXX or sm plugins unload XXXX + sm plugins load XXXX) BUT are not restored when I join the server.
NOTE: I don't think it's a "late load", because I have put the GetClientCookie() code when OnClientCookiesCached(). Also, my cookies are saved when player leave server (they are saved OnPluginEnd() too.). Can someone explain me what I have done wrong ? Should I give you my code ? |
Re: [Tutorial] ClientPrefs
Quote:
|
Re: [Tutorial] ClientPrefs
Quote:
|
Re: [Tutorial] ClientPrefs
Quote:
I already made a post for this, but it seems none saw it, or was able to fix it (or just wasn't interested). Quote:
Not sure if it's what you are asking for but, yes, it's called. I have print some debug text on OnClientCookiesCached() and GetClientCookies work for my other cookies. EDIT: Really ? No one wanna give me a hint ? Damn it ! |
Re: [Tutorial] ClientPrefs
I think this example have little mistake,
in cookie menu handler - CookieMenuAction_SelectOption action happens when you pick option "TestCookie" from cookie menu. So, you are picking old cookie value on OnClientCookiesCached(), So so, cookie OnOff integer value get change and saved AFTER you have choose menu option "On/Off". So so so, you need choose twice "on/off" option from menu to update rigth value in global variable g_bClientPreference[client]. how ever, I do have SourceMod Version: 1.5.2, has this feature changed for now ?? *edit could problem be SetCookiePrefabMenu ? bad timing |
Re: [Tutorial] ClientPrefs
Quote:
|
Re: [Tutorial] ClientPrefs
Quote:
OnPluginStart -> For 1 < i <= MaxClients > IsClientInGame(i) && AreClientCookiesCached(i) > OnClientCookiesCached(i) Here's another 'gotcha' the plugin tutorial forgot. BAD PHP Code:
GOOD PHP Code:
|
Re: [Tutorial] ClientPrefs
It should be documented somewhere that the maximum length of a cookie is 100 characters. I'd like to see it in the API documentation, but here would be good also.
|
Re: [Tutorial] ClientPrefs
Sorry, but
is this part optional? Code:
g_bClientPreference[client] = (sValue[0] != '\0' && StringToInt(sValue)); |
Re: [Tutorial] ClientPrefs
Quote:
|
Re: [Tutorial] ClientPrefs
Quote:
Doc: Quote:
|
Re: [Tutorial] ClientPrefs
Quote:
|
Re: [Tutorial] ClientPrefs
Why do you use Private cookies when it clearly looks like you're trying to make the cookie available for all? Why not Public cookie?
|
Re: [Tutorial] ClientPrefs
Quote:
This selection configures whether the user can change the cookie value manually, or whether they can even see the current value via sm_cookies. CookieAccess_Public means that the user can insert any string into the cookie by using sm_cookies. CookieAccess_Restricted means that the user can use sm_cookies to view the raw value of the cookie as stored in the database, but cannot change it. CookieAccess_Private means that the cookie will not show up in the sm_cookies list, and cannot be viewed or changed. The value of the cookie is instead exclusively changed through other means, such as a menu or something. The vast majority of cookies will be private. There is almost no reason to do anything else. Any cookie that takes a user-defined string would probably be better off having a separate, dedicated command to changing it and reading it. I really can't think of any valid reason to use anything other than CookieAccess_Private. |
Re: [Tutorial] ClientPrefs
Quote:
|
Re: [Tutorial] ClientPrefs
Quote:
|
Re: [Tutorial] ClientPrefs
https://forums.alliedmods.net/showthread.php?t=309777
General thoughts about using ClientPrefs for saving EXP or score a.k.a progress in a server? |
| All times are GMT -4. The time now is 22:25. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.