Raised This Month: $120 Target: $400
 30% 

nVault Tutorial


Post New Thread Reply   
 
Thread Tools Display Modes
Craxor
Veteran Member
Join Date: Jan 2016
Location: Europe (ro)
Old 09-23-2016 , 14:55   Re: nVault Tutorial
Reply With Quote #81

Quote:
Originally Posted by KliPPy View Post
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.

ok, so why not like that:

PHP Code:
const MAX_PLAYERS 33;
new 
szAuthIdMAX_PLAYERS ]; 
I suppose it's ok for readabilty ...
???

Last edited by Craxor; 09-23-2016 at 14:55.
Craxor is offline
Send a message via ICQ to Craxor
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-23-2016 , 15:01   Re: nVault Tutorial
Reply With Quote #82

You can do that, but MAX_PLAYERS is 32 so its not technically proper to set it to 33 directly.
__________________
Bugsy is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Europe (ro)
Old 09-23-2016 , 15:13   Re: nVault Tutorial
Reply With Quote #83

Quote:
Originally Posted by Bugsy View Post
You can do that, but MAX_PLAYERS is 32 so its not technically proper to set it to 33 directly.
I'm confuse, by that do you mean to let guys who reads our code that 'MAX_PLAYERS" will be to store for players indexes and +1 just for container wich contain the arrays, you know what i mean) - anyway i think the scope is just only for code readability, no?
Craxor is offline
Send a message via ICQ to Craxor
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-23-2016 , 15:18   Re: nVault Tutorial
Reply With Quote #84

The maximum number of players that is supported is 32, hence MAX_PLAYERS = 32 is used.

The + 1 is done strictly for sizing an array. Like I said, you could set it directly to 33 but it is not technically proper. You could have other parts of code that need to loop from 1 to max players, in this case if you defined it at 33 you would then need to loop from 1 to MAX_PLAYERS - 1.

I think we are getting off track here, if you want to continue a general scripting discussion please create a thread in scripting help.
__________________

Last edited by Bugsy; 09-23-2016 at 15:19.
Bugsy is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Upplands Väsby, Sweden
Old 09-24-2016 , 06:59   Re: nVault Tutorial
Reply With Quote #85

Quote:
Originally Posted by Bugsy View Post
@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])
}
You're right. I was wrong.

It was my intention to do it that way. i just didn't test it properly.
I do understand that converting it back is no use and in that case it's much better storing the string in the global array.
I would however remove "STEAM_" from it. I'm not saying everyone has to do so.

I used this code and figured it worked but of course the key is interpret in the same way even if it's not supported in that format and the data will be retrieved correctly.
Code:
#include <amxmodx> #include <amxmodx> #include <nvault> public plugin_init() {     register_plugin("Test Plugin 1", "1.0", "[ --{[email protected] ]");     new key[] = {2147483647, 0};     new val[] = {127, 0};     new nVault = nvault_open("test");     nvault_set(nVault, key, val);     new temp[10];     nvault_get(nVault, key, temp, charsmax(temp));     server_print("key: %d, val: %d", key[0], temp[0]);     nvault_close(nVault); }
Code:
key: 2147483647, val: 127
Works great with SQL though.
__________________

Last edited by Black Rose; 09-24-2016 at 07:02.
Black Rose is offline
Send a message via Skype™ to Black Rose
Old 09-24-2016, 08:38
Craxor
This message has been deleted by Craxor. Reason: i'm talking bullshits..
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-24-2016 , 11:45   Re: nVault Tutorial
Reply With Quote #86

With your code, Black Rose, it will appear to work correctly to users while in reality it is not.

It is storing the data in nVault with a single character as the key because it is using the right-most byte in the cell as the ascii character since it is expecting a string (which is normal for how Pawn stores/interprets strings).

PHP Code:
public client_authorizedid 
{
    
//Lets pretend my SteamID 'value' is 2298457
    
giSteamIDid ][ ] = 2298457 //GetUseriSteamID( id );
    
gPointsid ] = nvault_getg_Vault giSteamIDid ] )
    
    
//Print the key as both an integer as string.
    
server_print"nvault_get : [%d] [%s]" giSteamIDid ] , giSteamIDid ] );

Output
Code:
nvault_get : [2298457] [Y]
steam id integer = 2298457
char 'Y' ascii val = 89

In binary:
2298457 = 0000 0000 0010 0011 0001 0010 0101 1001
0000089 = 0000 0000 0000 0000 0000 0000 0101 1001

So when passing a cell holding 2298457, Pawn is taking the right-most byte value of 89 and say's oh ok, this means character 'Y' is the key.



I know you already acknowledged this issue, but for anyone that may have tried your code and saw it working, I wanted to explain it more because it should not be used.

Even with SQL, wouldn't you still need to convert it to a string in your SQL query? I think this would be useful in some type of hash lookup table where it takes an integer value as the input.
__________________

Last edited by Bugsy; 09-24-2016 at 11:48.
Bugsy is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Upplands Väsby, Sweden
Old 09-24-2016 , 12:39   Re: nVault Tutorial
Reply With Quote #87

Code:
create table if not exists inventory (SteamID int);
insert into inventory(SteamID) values(-2127214489);
Yes it's a string in the query but it is then stored as an integer inside the database which requires less space.

I did try to confirm my code in the beginning by using the nVault editor but it wouldn't start on my system. More info on that in the nVault editor thread.
__________________
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-24-2016 , 12:42   Re: nVault Tutorial
Reply With Quote #88

Quote:
Originally Posted by Black Rose View Post
Code:
create table if not exists inventory (SteamID int);
insert into inventory(SteamID) values(-2127214489);
Yes it's a string in the query but it is then stored as an integer inside the database which requires less space.
Yes, but from a plugin perspective you would still be better off storing the steam id as string, right?

Quote:
Originally Posted by Black Rose View Post
I did try to confirm my code in the beginning by using the nVault editor but it wouldn't start on my system. More info on that in the nVault editor thread.
I think the control needs to be registered properly but I'm not sure. I am on Windows 7 64-bit and it loads fine.
__________________
Bugsy is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Upplands Väsby, Sweden
Old 09-24-2016 , 12:52   Re: nVault Tutorial
Reply With Quote #89

Quote:
Originally Posted by Bugsy View Post
Yes, but from a plugin perspective you would still be better off storing the steam id as string, right?
Not necessarily. It depends on each situation I guess but as long as you don't need the string again you're better off storing it as an int.
In this nVault situation it's better with the string.
__________________

Last edited by Black Rose; 09-24-2016 at 12:52.
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-24-2016 , 13:03   Re: nVault Tutorial
Reply With Quote #90

Quote:
Originally Posted by Black Rose View Post
Not necessarily. It depends on each situation I guess but as long as you don't need the string again you're better off storing it as an int.
In this nVault situation it's better with the string.
How would you pass an integer into the query? Wouldn't the steam id always go into a SQL query string? I agree with you in the table design setting the steamid as in integer, 4 bytes would always be better than 34 bytes or whatever that is needed for a steam id string.

"SELECT * FROM tblWhatever WHERE SteamID=123123"
__________________
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 17:02.


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