Raised This Month: $32 Target: $400
 8% 

Solved get_user_authid bug?


Post New Thread Reply   
 
Thread Tools Display Modes
deprale
Senior Member
Join Date: Oct 2018
Location: Leeds
Old 08-01-2021 , 07:43   Re: get_user_authid bug?
Reply With Quote #11

Quote:
Originally Posted by HamletEagle View Post
You can use a trie for this. Take a step back and try to understand what you are doing because it's a complete mess right now. You have no space reserved to hold the steam id, g_UserMoney, g_UserFrags, g_UserDeaths are arrays when they should be integers. money, frags, deaths are numbers and they are already associated with a player when you do "g_szAuthID[id]" so there is no reason to have them be arrays, which would essentially translate to g_szAuthID[id][g_UserMoney][id].

Try to picture the layout of your arrays in memory. But in the meantime, here's an example template using tries:

PHP Code:
#include <amxmodx>

enum _:playerData
{
    
money,
    
frags,
    
deaths
}

new 
Trie:playerDataTrie

public plugin_init()
{
    
playerDataTrie TrieCreate()
}

public 
plugin_end()
{
    
TrieDestroy(playerDataTrie)
}

saveUserData(id)
{
    new 
data[playerData]
    
data[money]  = //retrieve money
    
data[frags]  = //retrieve frags
    
data[deaths] = //retrieve deaths

    
new steamId[35]
    
get_user_authid(idsteamIdcharsmax(steamId))

    
TrieSetArray(playerDataTriesteamIddatasizeof data)
}

loadUserData(id)
{
    new 
steamId[35]
    
get_user_authid(idsteamIdcharsmax(steamId))

    if(
TrieKeyExists(playerDataTriesteamId))
    {
        new 
data[playerData]
        
TrieGetArray(playerDataTriesteamIddatasizeof data)

        
//set money to data[money]
        //set frags to data[frags]
        //set deaths to data[deaths]
    
}

The beauty of a trie is that it allows you to store (key, value) pairs where the key is a string. Intuitively, it is as if you were able to do g_szAuthID["STEAM_:...."], which is to index an array by using a string instead of an integer("id").
Thanks makes sense, I'll try working with your example template, also isn't it better to create the trie in plugin_precache? I read it somewhere on the forums I'm pretty sure, but you'd know better, just asking
__________________
deprale is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 08-01-2021 , 07:56   Re: get_user_authid bug?
Reply With Quote #12

Let me know if you get stuck or don't understand something in the template I provided. If you want some more intuition about what it does read below:
Inside saveUserData I retrieved the steamid and then use it to index the trie/save the data inside the trie. What this does is basically create an association like steamid -> (money, frags, deaths).
Inside loadUserData, I also retrieve the steamid and query the trie, asking the following "give me the data I saved for the player with this steamid". The trie will return the money, frags and deaths that we previously saved.
If you don't want to restore frags(let's say) then simply ignore the lines that say //retrieve frags and //set frags to data[frags], or to have a more modular implementation you could assign a flag value(-1) to frags inside saveUserData and then in loadUserData only set frags if the value is != -1.

Now, about your question. It makes absolutely no difference where we create the trie. The only instance where it matters is if you needed to access the trie before plugin_init.
For example, if you were to read a configuration file with model replacements inside the trie and then you wanted to precache the models, sure, you should create the trie in plugin_precache because prechaching the models should also happen inside plugin_precache. plugin_init is called after plugin_precache, creating the trie in init would not work(because it will too late).
Basically, create the trie BEFORE you need to use it.
__________________

Last edited by HamletEagle; 08-01-2021 at 07:58.
HamletEagle is offline
deprale
Senior Member
Join Date: Oct 2018
Location: Leeds
Old 08-01-2021 , 08:17   Re: get_user_authid bug?
Reply With Quote #13

Quote:
Originally Posted by HamletEagle View Post
Let me know if you get stuck or don't understand something in the template I provided. If you want some more intuition about what it does read below:
Inside saveUserData I retrieved the steamid and then use it to index the trie/save the data inside the trie. What this does is basically create an association like steamid -> (money, frags, deaths).
Inside loadUserData, I also retrieve the steamid and query the trie, asking the following "give me the data I saved for the player with this steamid". The trie will return the money, frags and deaths that we previously saved.
If you don't want to restore frags(let's say) then simply ignore the lines that say //retrieve frags and //set frags to data[frags], or to have a more modular implementation you could assign a flag value(-1) to frags inside saveUserData and then in loadUserData only set frags if the value is != -1.

Now, about your question. It makes absolutely no difference where we create the trie. The only instance where it matters is if you needed to access the trie before plugin_init.
For example, if you were to read a configuration file with model replacements inside the trie and then you wanted to precache the models, sure, you should create the trie in plugin_precache because prechaching the models should also happen inside plugin_precache. plugin_init is called after plugin_precache, creating the trie in init would not work(because it will too late).
Basically, create the trie BEFORE you need to use it.
Thanks for the information!
Really helpful, and very easy to understand, your trie method worked flawlessly! Love it!
As for trying to not save certain data, that is indeed a very easy and simple method as well, I could basically add a variable and then set it to true and if its true then save -1 to user money, genius
__________________
deprale is offline
Reply



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 15:58.


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