AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved How to set dynamic array size (https://forums.alliedmods.net/showthread.php?t=341788)

alasfourom 02-11-2023 20:13

How to set dynamic array size
 
Hello guys, I recently started reading about arrays and how to sort integers etc.

Anyway, I have a stored data file with a list of clients, and I'm trying to set the array count to the numbers in the list. Then I will sort these clients in a Descending way with certain value.

PHP Code:

void SortDescending()
{
    
int iCount GetTotalPlayersInTheList();
    
int iArray[iCount]; //Error Here
    
    //Then I will do something with it


But it always gives me error
Quote:

brackets after variable name indicate a fixed-size array, but a dynamic size was given - did you mean to use 'new int[size]' syntax?
I would like only to know how to set "iArray" count to be equal to "iCount".

Bacardi 02-11-2023 20:38

Re: How to set dynamic array size
 
https://wiki.alliedmods.net/Introduc...awn_1.7#Arrays



PHP Code:

    int[] myarray = new int[20];
    
PrintToServer("%i size %i"myarray[1], 20); 

...and you can not use sizeof() for those, use that size variable when making array.

Silvers 02-11-2023 21:28

Re: How to set dynamic array size
 
Should probably use an ArrayList for this

alasfourom 02-11-2023 22:17

Re: How to set dynamic array size
 
I have mental block now, just to clear my point I will list the callback function here

PHP Code:

Action Command_Top(int clientint args)
{
    
g_KeyValue.Rewind();
    if(!
g_KeyValue.JumpToKey("Players Information"))
    {
        
delete g_KeyValue;
        
ThrowError("Corrupted file %s."g_sData_Path);
    }
    
    
//int iCount = GetTotalPlayersInTheDataFile();
    
int iPoints[100], iNumber 0;
    
char sName[100][32], sSteamID[100][32], sTemp[64];
    
    
Menu menu = new Menu(Handle_ShowTop10);
    
menu.SetTitle("Top 5 Ranks:");
    
    if(
g_KeyValue.GotoFirstSubKey())
    {
        do
        {
            
g_KeyValue.GetSectionName(sSteamID[iNumber], sizeof(sSteamID));
            
g_KeyValue.GetString("Player Name"sName[iNumber], sizeof(sName));
            
iPoints[iNumber] = g_KeyValue.GetNum("Total Points");
            
iNumber++;
        }
        while(
g_KeyValue.GotoNextKey());
    }
    
    
SortIntegers(iPoints100Sort_Descending);
    
    for (
int i 05i++)
    {
        
FormatEx(sTempsizeof(sTemp), "%s: %d points"sName[i], iPoints[i]);
        
menu.AddItem(sSteamID[i], sTempITEMDRAW_DEFAULT);
    }
    
    
menu.ExitBackButton true;
    
menu.Display(clientMENU_TIME_FOREVER);
    
    return 
Plugin_Handled;


its totally showing the top 5 players when called, but my issue, if the players count in the data file exceeded the [100], how to make it fit exactly my long data list.

So this part would be dunno something like this

PHP Code:

    int iCount GetTotalPlayersInTheDataFile();
    
int iPoints[iCount ], iNumber 0;
    
char sName[iCount ][32], sSteamID[iCount ][32], sTemp[64]; 

I also updated the post, cause I was poorly explaining my point, which I think its a little bit better

Silvers 02-12-2023 05:21

Re: How to set dynamic array size
 
A struct would probably be better suited for this, various datatype entries, no size limit, then you can store the struct inside an ArrayList and read/update as you need. Are you updating points to a KV file? The more entries you have the slower it'll be, suggest using a database instead. There are plenty of plugins already existing with various point style systems, suggest looking at those. KV system is slow for something like this where it's constantly being read/written to.

alasfourom 02-12-2023 09:18

Re: How to set dynamic array size
 
Awww I see, you are right

Thanks Silvers I will take a look then

Bacardi 02-12-2023 12:01

Re: How to set dynamic array size
 
2 Attachment(s)
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.

alasfourom 02-12-2023 13:01

Re: How to set dynamic array size
 
Thank you Bacardi, that makes sense now, and it worked as expected :up:

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


All times are GMT -4. The time now is 00:58.

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