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

Solved [ANY]How can I use userid to store and retreive a variable?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Morning
Member
Join Date: May 2021
Old 03-18-2023 , 14:51   [ANY]How can I use userid to store and retreive a variable?
Reply With Quote #1

I want to store a variable for a player, but I want to access it across map changes. At the moment I am using client as an array index, but it can change.

From my understanding, a player userid will be unique for the lifetime of the game server, so it seems like I need to somehow use that. Would this involve storing userid first in an array, and then using that to get a new client number after a map change? Does anyone have any working code of this so I can see how it's done?

Last edited by Morning; 03-21-2023 at 05:43.
Morning is offline
PC Gamer
Veteran Member
Join Date: Mar 2014
Old 03-18-2023 , 18:59   Re: [ANY]How can I use userid to store and retreive a variable?
Reply With Quote #2

You may wish to consider using Client Preferences. It writes a cookie on the client machine that can be read by your code whenever you need it. Here's a good tutorial: https://forums.alliedmods.net/showthread.php?t=228244
PC Gamer is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 03-19-2023 , 04:23   Re: [ANY]How can I use userid to store and retreive a variable?
Reply With Quote #3

...cookie is saved on server, sqlite database file in ...sourcemod/data/

But use that system only for saving settings, not for ban/rank/stats system.

Sqlite is second way to store anything data.

Third one would be keyvalues, but it could cause headcache.


- client index is entity index, it is like player slot on server.
- userid is like ticket number, when player reconnect to server he get new ticket number.
- each player have steamid, you find player with that in any day.
__________________
Do not Private Message @me

Last edited by Bacardi; 03-19-2023 at 04:29.
Bacardi is offline
Morning
Member
Join Date: May 2021
Old 03-20-2023 , 08:33   Re: [ANY]How can I use userid to store and retreive a variable?
Reply With Quote #4

Currently one reason is for storing session stats which just get reset / discarded at the end of the session and not stored in database - I'm running L4D2, and a campaign session usually consists of 4 mapchanges and clients can get moved around slots. I'm firstly looking to fix this.

There would be other small things too, and I could use clientprefs or database, but I'm wary of adding more database calls than I really need because it can cause lag because I dont have good I/O speed and I don't use threaded query. If clientprefs is threaded that could be okay but otherwise it would be good to keep things in memory.

I will try to use multidimensional array where I store and update userid, client, ect. If that fails I will try database, I guess.

But yeah, if anyone has code of a fast and simple way to do it in memory, that would be very helpful!

Thanks all for the help

Last edited by Morning; 03-20-2023 at 08:36.
Morning is offline
alasfourom
Senior Member
Join Date: Feb 2022
Location: Saudi Arabia
Old 03-20-2023 , 11:49   Re: [ANY]How can I use userid to store and retreive a variable?
Reply With Quote #5

I would say using StringMap would be the best for you as previously mentioned by "ddhoward ", I cant find his snippet now, but here how it looks like, using it to store points and reset at the start of next chapter
You can reset clients stats every round, or every map according to your need

PHP Code:
#include <sourcemod>
#include <sdkhooks>
#include <sdktools>

#pragma semicolon 1
#pragma newdecls required

int g_iPoints [MAXPLAYERS+1];
StringMap g_smCampaign;

public 
void OnPluginStart()
{
    
RegConsoleCmd("sm_mypoints"Command_MyPoints"Test To Show My Points");
    
RegConsoleCmd("sm_addpoints"Command_AddPoints"Test To Add 5 Points");
    
    
g_smCampaign = new StringMap();
}

public 
void OnMapStart()
{
    
// Clear all clients points at start of every map
    
g_smCampaign.Clear();
}

Action Command_MyPoints(int clientint args)
{
    
//Display points command
    
PrintToChat(client"Your Points: %d"g_iPoints[client]);
    return 
Plugin_Handled;
}

Action Command_AddPoints(int clientint args)
{
    
//Add points command
    
g_iPoints[client] += 5;
    return 
Plugin_Handled;
}

public 
void OnClientAuthorized(int client, const char[] auth)
{
    if (
IsFakeClient(client))return;
    
    
int StoredPoints;
    
//If clients have been here before, give them their stored points
    
if(g_smCampaign.GetValue(authStoredPoints)) g_iPoints[client] = StoredPoints;
    
    
// Else reset their points to what you need like 0 etc
    
else g_iPoints[client] = 0;
}

public 
void OnClientDisconnect(int client)
{
    if (
IsFakeClient(client)) return;
    
    
//Save clients points when disconnecting
    
char auth[32];
    
GetClientAuthId(clientAuthId_Steam2authsizeof(auth));
    
g_smCampaign.SetValue(authg_iPoints[client], true);

__________________

Last edited by alasfourom; 03-20-2023 at 12:02.
alasfourom is offline
Morning
Member
Join Date: May 2021
Old 03-21-2023 , 05:41   Re: [ANY]How can I use userid to store and retreive a variable?
Reply With Quote #6

This looks exactly like what I am looking for! Thanks so much for the help!
Morning 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 20:55.


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