Raised This Month: $106 Target: $400
 26% 

nVault Tutorial


Post New Thread Reply   
 
Thread Tools Display Modes
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Upplands Väsby, Sweden
Old 09-21-2016 , 16:28   Re: nVault Tutorial
Reply With Quote #71

Quote:
Originally Posted by Craxor View Post
@prostetamoto: Just for safety, 'maybe' player is not fully connected so i make a little delay, is not big deal but just to be sure( and in my past i was having problems some times so this should be there ).

It's you preferences rose, but bugsy said to make a global var gAuthId to avoid calling get_user_authid more times, even if you make it in a stock calls are maded more times but that's about preferences.


About szPoints should be [11] sorry i did not know about max integers

Oo, about '33' as i know:

32 - the container wich hold the the arrays:

31 - id 32
30 - id 31
29 - id 30
28 - id 29
27 - 28
..... bla bla bla...
4 - id 5
3 - id 4
2 - id 3
2 - id 3
1 - id 2
0 - id 1.

So why using 33 ?
Yeah. Ignore the fact that I've been coding for AMXX for 11 years.

My example only calls get_user_authid() once on authorized.
EDIT: This is wrong as pointed out by Bugsy: Then it stores the integer value generated from that and never use the string again.
Trust me, it's better in every way. It's just a question of overkill.


Indexes range from either 0-31 if you use a listen server or 1-32 if you have a dedicated server (because the server has id 0).
If you set up a variable with 32 indexes (new array[32]), you get 32 indexes, but they range from 0-31. If a player joins and gets the id 32 and you try calling array[id(32)], you will get index out of bounds error and the plugin fails miserably.
You can use array[id-1], but that means if you run a listen server you will get index out of bounds in the other end instead since there is no array[-1].
For simplicity people tend to use 33 because there is no need to worry. Nothing can go wrong. I don't think many people knows about the listen server problem though, but that doesn't matter.

You can also look at it from another perspective.
If you use array[id-1] it means that the engine has to carry out a math operation which steals precious CPU. And optimization (in my world) is about using memory to preserve CPU. Because memory is almost never an issue. And in this case we're talking about 1 cell extra, that's 4 bytes. Again, overkill. The point of not carrying out any math operations with the player ID is readability. And that's the most important thing of any code.
If you can read it properly, you can understand it.
If you can understand it you can debug and support it.
That's the way of becoming a good coder IMHO.

You clearly want to become a good coder. And I support you. But you have to chill out a bit. You will make mistakes in the beginning, don't worry about it.
__________________

Last edited by Black Rose; 09-24-2016 at 06:45.
Black Rose is offline
Send a message via Skype™ to Black Rose
Craxor
Veteran Member
Join Date: Jan 2016
Location: Europe (ro)
Old 09-22-2016 , 03:02   Re: nVault Tutorial
Reply With Quote #72

Yes, but if i will use you examples also for Saving data i willl need to call more times get_user_authid, i'm wrong?
I know about if i use just for taking data from nvault and is logic if i'm using only once ) what about retrieving?

Anyway i need to read more times what you write, you write tooo much for me ) i wil re-read more times

Btw: if a make something like: new szAuthid[32+1]; will change resize automatcly when new index is needed?

About

Quote:
You clearly want to become a good coder. And I support you. But you have to chill out a bit. You will make mistakes in the beginning, don't worry about it.
Yeaaa, i have just 4 years of coding and i'm still beginner because i don't stay all the fucking day to understand codes, i have also lot of plans about my life but that's realy offtopic.

Last edited by Craxor; 09-22-2016 at 03:32.
Craxor is offline
Send a message via ICQ to Craxor
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Upplands Väsby, Sweden
Old 09-22-2016 , 11:48   Re: nVault Tutorial
Reply With Quote #73

Quote:
Originally Posted by Craxor View Post
Yes, but if i will use you examples also for Saving data i willl need to call more times get_user_authid, i'm wrong?
I know about if i use just for taking data from nvault and is logic if i'm using only once ) what about retrieving?
EDIT: This is wrong as pointed out by Bugsy: That's the whole point. Compressing that long string into 1 cell which can be stored in exactly the same way. You don't have to convert it when saving or loading. get_user_authid() is called once for each player joining.
The saving is very much like your example. There's not much that can be done to optimize that.

Code:
I'm bad code, don't use me.

The CPU optimization is almost none.
The memory optimization is better but completely unimportant.
The database size however, much better.

Instead of saving this:

Code:
STEAM_0:X:XXX1 1234
STEAM_0:X:XXXXXX2 999
STEAM_0:X:XXXXX3 1
...
you're saving this:
Code:
1 1234
2 999
3 1
...
Quote:
Originally Posted by Craxor View Post
Btw: if a make something like: new szAuthid[32+1]; will change resize automatcly when new index is needed?
szAuthid[32+1] will become 33, and it will not resize automatically.
There are ways to changing the size of "static" arrays (not to be confused with dynamic arrays). But it's very complicated. It is almost never used.

Quote:
Originally Posted by Craxor View Post
Yeaaa, i have just 4 years of coding and i'm still beginner because i don't stay all the fucking day to understand codes, i have also lot of plans about my life but that's realy offtopic.
Everyone has stuff in their lives.
__________________

Last edited by Black Rose; 09-24-2016 at 12:53.
Black Rose is offline
Send a message via Skype™ to Black Rose
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-22-2016 , 21:52   Re: nVault Tutorial
Reply With Quote #74

@Black Rose -- I don't see the advantage of converting the steam id to an integer when using nvault. You would then need to convert it back to a string to use it as a key when saving and loading data since nvault only allows string keys. I like where your head is at with this but with nvault I don't see the point. Yes, it saves memory, but as you said a few posts ago, memory is not much of a concern anymore, it's CPU that is expensive. Plus, you realize he's trying to learn, I don't understand why you're trying to confuse him even more with code that has moot benefit over using a string -- on top of that, it doesn't even work like I think you're expecting it to. You are trying to store this unique value into a 2 cell array/string -- 1 cell will do for an integer and 2 for a string doesn't make sense. If I misinterpreted anything, please correct me. I'm not trying to ruffle anyone's feathers here.

Code:
//Each player gets a 2 cell array/string? Why?
//If your plan was to store only an integer here,
//you only need a single dimension array.
new giSteamID[33][2];
stock GetUseriSteamID(id) {     new szSteamID[21];     get_user_authid(id, szSteamID, charsmax(szSteamID));     return str_to_num(szSteamID[10]) | '0' - szSteamID[8] << 31; } public client_authorized( id ) {     giSteamID[id][0] = GetUseriSteamID(id);
    //nVault needs a string as the key, here you are
    //passing it a 1 character string which I do not think
    //is your intention. You should convert it back to a string
    //using num_to_str() but then you defeat the purpose of
    //using an integer instead of a string so you may as well
    //just store the full steam id in a string.
    gPoints[id] = nvault_get(gnVault, giSteamID[id])
}

@Craxor

Quote:
Originally Posted by Craxor View Post
Yes, but if i will use you examples also for Saving data i willl need to call more times get_user_authid, i'm wrong?
I know about if i use just for taking data from nvault and is logic if i'm using only once ) what about retrieving?

Anyway i need to read more times what you write, you write tooo much for me ) i wil re-read more times

Btw: if a make something like: new szAuthid[32+1]; will change resize automatcly when new index is needed?
You should ever only call get_user_authid() once at client_authorized() and store the value globally. You can then use it again when the user disconnects to save their data as well as anywhere else in your plugin that the steam id may be needed.

You cannot and do not ever need to change/resize the array once it's defined (talking regular arrays here). You should always size your arrays at the max possible size you will need, so in the case of CS 1.6, the highest possible player id value that can exist is 32. This is why you see arrays for players defined at 33 because the array index begins at 0 (0 to 32, total of 33 cells). The rules are different when working with dynamic arrays since you can resize on the fly.

Your code isn't terrible, I only changed a few things. Some of it was just for readability/organization.
Spoiler
__________________

Last edited by Bugsy; 09-22-2016 at 22:12.
Bugsy is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Europe (ro)
Old 09-23-2016 , 03:02   Re: nVault Tutorial
Reply With Quote #75

Not realeated to nvault but i have a question, why this:
MAX_PLAYERS + 1

And why not directly set the define 33 ? why need a calculations, i'm realy curious, thanks

Last edited by Craxor; 09-23-2016 at 03:02.
Craxor is offline
Send a message via ICQ to Craxor
KliPPy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 09-23-2016 , 04:48   Re: nVault Tutorial
Reply With Quote #76

Readability and maintainability. Just by sizing an array with MAX_PLAYERS + 1 (MAX_PLAYERS is 32, so +1 makes it 33), it gets self-documented, saying that array is intended for player data.

Always use named constants instead of "magic" numbers if possible, they make the code better.
KliPPy is offline
[email protected]
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 09-23-2016 , 12:38   Re: nVault Tutorial
Reply With Quote #77

Quote:
Originally Posted by Bugsy View Post
You should ever only call get_user_authid() once at client_authorized() and store the value globally.
Why? Do you call get_user_authid over9000 times per second?
__________________
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-23-2016 , 12:42   Re: nVault Tutorial
Reply With Quote #78

Quote:
Originally Posted by [email protected] View Post
Why? Do you call get_user_authid over9000 times per second?
Because that is all it needs to be called. The value will never change during a user's connection to your server. Is it fun to call it more than once? I dont understand what point you're trying to make.
__________________
Bugsy is offline
[email protected]
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 09-23-2016 , 13:39   Re: nVault Tutorial
Reply With Quote #79

Quote:
Originally Posted by Bugsy View Post
Because that is all it needs to be called. The value will never change during a user's connection to your server. Is it fun to call it more than once? I dont understand what point you're trying to make.
Why you make code more complicated? It is easier just use get_user_authid than adding cache. Also caching make your code less portable to another plugins.
__________________
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-23-2016 , 13:54   Re: nVault Tutorial
Reply With Quote #80

Its not complicated if the variable (global) has a different scope. Give me an example for how this would affect portability.
__________________
Bugsy 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 05:16.


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