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

Solved Need Help regarding ArrayList


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
chaithresh
Member
Join Date: Oct 2016
Location: Mangalore
Old 01-12-2019 , 05:52   Need Help regarding ArrayList
Reply With Quote #1

I am writing a plugin where I have to store players steamids in an array and check when they connect if they are part of the array.

I came across a class called ArrayList and I'm having a hard time understanding this particular class. Can anyone help me with how can I use this particular Class:

ArrayList ArrayList(int blocksize, int startsize)

Last edited by chaithresh; 01-24-2019 at 05:30. Reason: [Solved]
chaithresh is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 01-12-2019 , 06:04   Re: Need Help regarding ArrayList
Reply With Quote #2

PHP Code:

ArrayList g_Steam
;

public 
void OnPluginStart()
{
    
// g_Steam = new ArrayList(); // for data storage different than string, but i suppose we work with strings here
    
g_Steam = new ArrayList(ByteCountToCells(65)) // for strings, 65 = size of string like char[65] steam;
}

public 
void OnClientPostAdminCheck(int client)
{
    
char[65steam;
    
// get steam of client, GetClientAuthId etc.
    
    
int index g_Steam.FindString(steam);
    if (
index != -1// we found the steam in the array
    
{
        
// do something here
    
}
}

void MakeArrayList()
{
    
// we call this function OnMapStart() or OnConfigsExecuted()?
    // we clear the arraylist first
    
    
g_Steam.Clear();
    
    
// we make the arraylist here
    
    
char[65steam;
    
g_Steam.PushString(steam);

__________________

Last edited by Ilusion9; 01-12-2019 at 06:05.
Ilusion9 is offline
DarkDeviL
SourceMod Moderator
Join Date: Apr 2012
Old 01-12-2019 , 06:08   Re: Need Help regarding ArrayList
Reply With Quote #3

I believe everything is also lost upon plugin unloads with ArrayList, so when the plugin loads again, you also have to load / re-build the data from some sort of storage.

If you need the data across map changes and server restarts, I would suggest you to use a SQL database (NOT sqlite!) instead, and preferably with the database server on another system.

Can you provide some more insight on your end game, as it might be possible that we could recommend other (and potentially better) ways to accomplish what you want?
__________________
Mostly known as "DarkDeviL".

Dropbox FastDL: Public folder will no longer work after March 15, 2017!
For more info, see the [SRCDS Thread], or the [HLDS Thread].

Last edited by DarkDeviL; 01-12-2019 at 06:09.
DarkDeviL is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 01-12-2019 , 06:47   Re: Need Help regarding ArrayList
Reply With Quote #4

Quote:
Originally Posted by arne1288 View Post
NOT sqlite!
SQLite is built exactly for simple use cases like this, where does you recommendation against it come from?

If you don't want persistent storage (so not SQL / something else), you almost certainly want a StringMap rather than an ArrayList, as checking for a SteamID in the set will be considerably faster.
__________________

Last edited by asherkin; 01-12-2019 at 06:48.
asherkin is offline
chaithresh
Member
Join Date: Oct 2016
Location: Mangalore
Old 01-12-2019 , 07:08   Re: Need Help regarding ArrayList
Reply With Quote #5

Quote:
Originally Posted by Ilusion9 View Post
PHP Code:

ArrayList g_Steam
;

public 
void OnPluginStart()
{
    
// g_Steam = new ArrayList(); // for data storage different than string, but i suppose we work with strings here
    
g_Steam = new ArrayList(ByteCountToCells(65)) // for strings, 65 = size of string like char[65] steam;
}

public 
void OnClientPostAdminCheck(int client)
{
    
char[65steam;
    
// get steam of client, GetClientAuthId etc.
    
    
int index g_Steam.FindString(steam);
    if (
index != -1// we found the steam in the array
    
{
        
// do something here
    
}
}

void MakeArrayList()
{
    
// we call this function OnMapStart() or OnConfigsExecuted()?
    // we clear the arraylist first
    
    
g_Steam.Clear();
    
    
// we make the arraylist here
    
    
char[65steam;
    
g_Steam.PushString(steam);

Thanks for the explanation, I understood the most part, I have a small doubt regarding "g_Steam = new ArrayList(ByteCountToCells(65)):".
1) how many steamids I can add in this array?
2) I'm storing SteamID64 which is 17 characters, can I change the number 65 to 17? does it affect in any way?
chaithresh is offline
chaithresh
Member
Join Date: Oct 2016
Location: Mangalore
Old 01-12-2019 , 07:13   Re: Need Help regarding ArrayList
Reply With Quote #6

Quote:
Originally Posted by arne1288 View Post
I believe everything is also lost upon plugin unloads with ArrayList, so when the plugin loads again, you also have to load / re-build the data from some sort of storage.

If you need the data across map changes and server restarts, I would suggest you to use a SQL database (NOT sqlite!) instead, and preferably with the database server on another system.

Can you provide some more insight on your end game, as it might be possible that we could recommend other (and potentially better) ways to accomplish what you want?
1) I'm loading steamids from a key-value file for that particular map and once a game/map ends a new set of steam ids will be loaded.
2) I just want to check whenever a player connects if he is part of the steamid lists in keyvalue, if he is part of it allow him in the server, else kick him.
3) I am trying to avoid going through Keyvalue file again and again everytime a client connects.
chaithresh is offline
DarkDeviL
SourceMod Moderator
Join Date: Apr 2012
Old 01-12-2019 , 07:41   Re: Need Help regarding ArrayList
Reply With Quote #7

Quote:
Originally Posted by asherkin View Post
SQLite is built exactly for simple use cases like this, where does you recommendation against it come from?
That was indeed a bit misplaced! I was originally writing something about support for multiple servers too, which was the original reason to the inclusion, but then thought more clarification of the "end game" would make more sense first and then forgot to remove that part.

Quote:
Originally Posted by chaithresh View Post
1) I'm loading steamids from a key-value file for that particular map and once a game/map ends a new set of steam ids will be loaded.
2) I just want to check whenever a player connects if he is part of the steamid lists in keyvalue, if he is part of it allow him in the server, else kick him.
3) I am trying to avoid going through Keyvalue file again and again everytime a client connects.
So some sort of a server whitelist, as I expected.

If you require support for multiple servers or another system to "generate" the data for you, I would still suggest using a database like mentioned above.
__________________
Mostly known as "DarkDeviL".

Dropbox FastDL: Public folder will no longer work after March 15, 2017!
For more info, see the [SRCDS Thread], or the [HLDS Thread].
DarkDeviL is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 01-12-2019 , 10:32   Re: Need Help regarding ArrayList
Reply With Quote #8

Quote:
Originally Posted by chaithresh View Post
Thanks for the explanation, I understood the most part, I have a small doubt regarding "g_Steam = new ArrayList(ByteCountToCells(65)):".
1) how many steamids I can add in this array?
2) I'm storing SteamID64 which is 17 characters, can I change the number 65 to 17? does it affect in any way?
1. as many as you want, this is why you use arraylist (dynamic vector) and not a static one (char g_Steam[MAX_STEAMID][65])
2. Yes, you can change 65 to whatever size you want.

ByteCountToCells(65) means the size of the string in the array. If you have a string with 500 length, then in the arraylist will be saved only the first 65 characters.

If you only read data, then you can use KeyValues or File. If you also change the data, then you should use databases. For multiple servers, whatever you do (read or change) then you must use only databases.
__________________
Ilusion9 is offline
chaithresh
Member
Join Date: Oct 2016
Location: Mangalore
Old 01-14-2019 , 02:36   Re: Need Help regarding ArrayList
Reply With Quote #9

Quote:
Originally Posted by Ilusion9 View Post
1. as many as you want, this is why you use arraylist (dynamic vector) and not a static one (char g_Steam[MAX_STEAMID][65])
2. Yes, you can change 65 to whatever size you want.

ByteCountToCells(65) means the size of the string in the array. If you have a string with 500 length, then in the arraylist will be saved only the first 65 characters.

If you only read data, then you can use KeyValues or File. If you also change the data, then you should use databases. For multiple servers, whatever you do (read or change) then you must use only databases.
Perfect, Thanks for the explanation, Now i will try with this new method and get back to you once it works.
chaithresh is offline
chaithresh
Member
Join Date: Oct 2016
Location: Mangalore
Old 01-14-2019 , 02:41   Re: Need Help regarding ArrayList
Reply With Quote #10

Quote:
Originally Posted by arne1288 View Post
That was indeed a bit misplaced! I was originally writing something about support for multiple servers too, which was the original reason to the inclusion, but then thought more clarification of the "end game" would make more sense first and then forgot to remove that part.



So some sort of a server whitelist, as I expected.

If you require support for multiple servers or another system to "generate" the data for you, I would still suggest using a database like mentioned above.
Got it so
1) If you use ArrayList the data will be lost on map change.
2) But database will be useful when I want to carry forward data around the map changes and need to use the same data between multiple servers.

Thanks for the info, <3
chaithresh 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 09:59.


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