AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   DataPacks(?) and Dynamic Arrays how to store some data (https://forums.alliedmods.net/showthread.php?t=294576)

grs4 03-02-2017 10:00

DataPacks(?) and Dynamic Arrays how to store some data
 
From file i want to extract some data which are saved in scheme as below:

PHP Code:

    1
    
{
        
12341.5
        3
5171.5
        1
1535.0
    
}
    
2
    
{
        
31250.0
    
}
    
3
    
{
        
632131.5
        7
553321.5
    
}
    
4
    
{
        
641210.0
    


Now i want to keep it in Dynamic Arrays.

Numbers of data, and entries in data this always will be randomly. so how to save this?
Maybe some datapacks?

grs4 03-02-2017 12:03

Re: DataPacks(?) and Dynamic Arrays how to store some data
 
So.. if someone will have common problem, here is a solution.


PHP Code:


new Array:g_Array;

public 
plugin_init()
{
   
g_Array ArrayCreate(150); 50 is max data


before loading our data:

PHP Code:

new Array:aTempArray ArrayCreate(15); //5 is max entries in every data 

at entry load

PHP Code:

new DataPack:entryInfo CreateDataPack();
WritePackCell(entryInfovalueA);
WritePackCell(entryInfovalueB);
WritePackCell(entryInfovalueC);
WritePackCell(entryInfovalueD);
WritePackFloat(entryInfovalueE);
                
ArrayPushCell(aTempArraywaveRoundInfo); 

at new data load

PHP Code:

ArrayPushCell(g_ArrayaTempArray);
aTempArray ArrayCreate(15); 

And if you want to load all data from arrays

PHP Code:


public readConfig()
{
    new 
size1 ArraySize(g_Array);
    for(new 
0size1i++)
    {
        new Array:array = 
ArrayGetCell(g_Arrayi);
        new 
size2 ArraySize(array);
        
        
log_amx("Data %d"i+1);
        
        for(new 
0size2j++)
        {
            new 
DataPack:pack DataPack:ArrayGetCell(array, j);
            
ResetPack(pack);
            
log_amx("entry: %d / %d"j+1pack);
            for(new 
05k++)
            {
                if(
!= 4)
                {
                    
log_amx("%d: %d"k+1ReadPackCell(pack));
                }
                else
                {
                    
log_amx("%d: %0.2f"k+1ReadPackFloat(pack));
                }
            }
            
DestroyDataPack(pack);
        }
    }


result:

PHP Code:


L 03
/02/2017 18:04:02: [lw.amxxData 1
L 03
/02/2017 18:04:02: [lw.amxxentry1
L 03
/02/2017 18:04:02: [lw.amxx11
L 03
/02/2017 18:04:02: [lw.amxx25
L 03
/02/2017 18:04:02: [lw.amxx32
L 03
/02/2017 18:04:02: [lw.amxx41
L 03
/02/2017 18:04:02: [lw.amxx52.50
L 03
/02/2017 18:04:02: [lw.amxxData 2
L 03
/02/2017 18:04:02: [lw.amxxentry2
L 03
/02/2017 18:04:02: [lw.amxx15
L 03
/02/2017 18:04:02: [lw.amxx25
L 03
/02/2017 18:04:02: [lw.amxx32
L 03
/02/2017 18:04:02: [lw.amxx41
L 03
/02/2017 18:04:02: [lw.amxx52.50
L 03
/02/2017 18:04:02: [lw.amxxentry3
L 03
/02/2017 18:04:02: [lw.amxx11
L 03
/02/2017 18:04:02: [lw.amxx21
L 03
/02/2017 18:04:02: [lw.amxx32
L 03
/02/2017 18:04:02: [lw.amxx44
L 03
/02/2017 18:04:02: [lw.amxx50.00 

And also remember to delete all at plugin end, if you dont do this, server will be crashed at every map change. (Because datapacks are in memory)

PHP Code:

public plugin_end()
{
    new 
size1 ArraySize(g_Array);
    
    for(new 
0size1i++)
    {
        new Array:array = 
ArrayGetCell(g_Arrayi);
        new 
size2 ArraySize(array);
        
        for(new 
0size2j++)
        {
            new 
DataPack:pack DataPack:ArrayGetCell(array, j);
            
DestroyDataPack(pack);
        }
        
ArrayDestroy(array);
    }
    
    
ArrayDestroy(g_Array);


For me, is very bad, that I Have to know size of DataPack, there is no something like "DataPackSize" and "DataPackGetType" (<- This should return a type of data in current position.

yas17sin 03-02-2017 12:21

Re: DataPacks(?) and Dynamic Arrays how to store some data
 
this should be in Code Snippets :/.

Arkshine 03-03-2017 03:20

Re: DataPacks(?) and Dynamic Arrays how to store some data
 
Using another Array to handle an array (since you have a fixed number of values, 5) seems more suit than using datapack here.

grs4 03-03-2017 03:48

Re: DataPacks(?) and Dynamic Arrays how to store some data
 
With datapacks are more troubles than i knew it can be.

All datapacks which i'm creating has always 5 values
when i want to read a value from postion no. 1

log
PHP Code:


L 03
/03/2017 09:41:16Datapack operation is invalid.
L 03/03/2017 09:41:16: [AMXXDisplaying debug trace (plugin "lw.amxx"version "1.0")
L 03/03/2017 09:41:16: [AMXXRun time error 10native error (native "ReadPackCell")
L 03/03/2017 09:41:16: [AMXX]    [0lw.sma::getValueFromPack (line 120)
L 03/03/2017 09:41:16: [AMXX]    [1lw.sma::test2 (lin 

PHP Code:

stock any:getValueFromPack(&DataPack:packeDataInfo:info)
{
    
SetPackPosition(pack_:info);
    
//log_amxx firstly returns: 11 0 0 0
    // then: 11 1 9 0
   // so, why it did not set pack postion to "1" as "_:info" ?
    
log_amx("%d %d %d %d"pack_:infoGetPackPosition(pack), IsPackEnded(pack));
    return 
info == DATA_VALUE_FLOAT ReadPackFloat(pack) : ReadPackCell(pack);


Also, as you told I changed from DataPacks to Arrays. Now everything works great. But i'm interesting in this problem, what is causing it. Why i changed postion to "9" when it should set to "1"

Arkshine 03-03-2017 05:47

Re: DataPacks(?) and Dynamic Arrays how to store some data
 
Because you don't read the documentation. You misunderstand SetPackPosition(), it expects a value from GetPackPosition(), not an arbitrary index. It might be confusing, we should probably do like SourceMod (they have the same api) and adding a tag for position to both natives.

grs4 03-03-2017 06:19

Re: DataPacks(?) and Dynamic Arrays how to store some data
 
read I read.
Apperantly I did not undestood. Thanks a lot for help


All times are GMT -4. The time now is 20:59.

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