AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved [ANY]How can I use userid to store and retreive a variable? (https://forums.alliedmods.net/showthread.php?t=342218)

Morning 03-18-2023 14:51

[ANY]How can I use userid to store and retreive a variable?
 
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?

PC Gamer 03-18-2023 18:59

Re: [ANY]How can I use userid to store and retreive a variable?
 
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

Bacardi 03-19-2023 04:23

Re: [ANY]How can I use userid to store and retreive a variable?
 
...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.

Morning 03-20-2023 08:33

Re: [ANY]How can I use userid to store and retreive a variable?
 
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 :mrgreen:

alasfourom 03-20-2023 11:49

Re: [ANY]How can I use userid to store and retreive a variable?
 
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);



Morning 03-21-2023 05:41

Re: [ANY]How can I use userid to store and retreive a variable?
 
This looks exactly like what I am looking for! Thanks so much for the help! :twisted:


All times are GMT -4. The time now is 15:43.

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