Raised This Month: $ Target: $400
 0% 

Solved How to set dynamic array size


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 02-12-2023 , 12:01   Re: How to set dynamic array size
Reply With Quote #1

This is continue of that KeyValue thing.

If your rank system is same as in previous topic How to loop steamid while using keyvalues

PHP Code:
public Action top(int clientint args)
{
    
kv.Rewind();

    
// Go to first sub-entry in "Players Information"

    
if(kv.JumpToKey("Players Information") && kv.GotoFirstSubKey())
    {
        
int count;
        
// Make first loop, count sub-entries
        
        //char section[30];

        
do
        {
            
//kv.GetSectionName(section, sizeof(section));
            //PrintToServer("section %s", section);
            
count++;
        }
        while(
kv.GotoNextKey(true))

        
// after loop, remember change position, GoBack()
        
kv.GoBack();

        
// we have sub-entries
        
if(count)
        {
            
//PrintToServer("count %i", count);

            // Dynamic size array, store points and "ID position" of KeyValue section
            
int[][] array = new int[count][2];
            
count 0;

            
// Make second loop and collect points and Kv ID position
            
kv.GotoFirstSubKey();

            do
            {
                array[
count][0] = kv.GetNum("Total Points"0);
                
kv.GetSectionSymbol(array[count][1]);
                
PrintToServer("points %i ID %i", array[count][0], array[count][1]);
                
count++;
            }
            while(
kv.GotoNextKey())

            
kv.GoBack();

            
// Sort 2D array by points
            
SortCustom2D(array, countsortfunc2d);

            
char name[MAX_NAME_LENGTH];

            
// Loop top-10 results from array
            
for(int x 010x++)
            {
                
kv.JumpToKeySymbol(array[x][1]);
                
                
name[0] = '\0';    // erase string
                
kv.GetString("Player Name"namesizeof(name));
                
PrintToServer("name %s point %i"namekv.GetNum("Total Points"0));
                
kv.GoBack();
            }
        }
    }

    
kv.Rewind();

    return 
Plugin_Handled;
}

public 
int sortfunc2d(int[] elem1int[] elem2, const int[][] array, Handle hndl)
{
    
//PrintToServer("elem1 %i elem2 %i", elem1[0], elem2[0]);

    //if(elem1[0] < elem2[0])
    //{
    //    return 1;
    //}

    
return elem2[0] - elem1[0]; // I'm not sure is this good way

here output
Spoiler


And in l4d2_rank_system.txt file have data of players scores in KeyValue.


But have to say this is like pain in ass using KeyValues.
One mistake in code can lead easily to data loss.

Another way is using SMC parse to look those KeyValue data from file.

But like @Silvers mentioned already, use SQL. SourceMod local sqlite.
You can expand more features with tables.

But make first simple rank system without Database or else storage save,
use global variable (array) to keep scores update.

- And then, you update scores every round_end into Database or else.
Main idea is handle scores in global variables, it is much faster.
Attached Files
File Type: txt l4d2_rank_system.txt (1.9 KB, 101 views)
File Type: sp Get Plugin or Get Source (test.sp - 85 views - 3.6 KB)
__________________
Do not Private Message @me
Bacardi is offline
alasfourom
Senior Member
Join Date: Feb 2022
Location: Saudi Arabia
Old 02-12-2023 , 13:01   Re: How to set dynamic array size
Reply With Quote #2

Thank you Bacardi, that makes sense now, and it worked as expected

I was trying something out to sort arrays in general but damn I was totally lost smh, I kinda got it now, and its good as a reference for me.

Quote:
But make first simple rank system without Database or else storage save,
use global variable (array) to keep scores update.

- And then, you update scores every round_end into Database or else.
Main idea is handle scores in global variables, it is much faster.
Gotcha!

I still never dealt with SQL yet. Already reading about it, but still kinda complicated for me, hopefully will be upgrading it later
__________________

Last edited by alasfourom; 02-12-2023 at 13:01.
alasfourom 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 00:58.


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