Raised This Month: $51 Target: $400
 12% 

ADT_Array question


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
blaacky
Senior Member
Join Date: Oct 2012
Old 04-17-2014 , 15:44   ADT_Array question
Reply With Quote #1

How do I tell if a handle is an already initialized adt_array without making a separate variable that is just set to true when you've initialized it?

This pretty much gives me an error on GetArraySize because the array isn't an adt_array.

PHP Code:
if(PlayerID >= GetArraySize(g_hMapsDone[Type][Style]))
{
    
ResizeArray(g_hMapsDone[Type][Style], PlayerID 1);
}
            
if(
GetArrayCell(g_hMapsDone[Type][Style], PlayerID) == INVALID_HANDLE// This line doesn't always work
{
    new 
Handle:hCell CreateArray(3);
    
SetArrayCell(g_hMapsDone[Type][Style], PlayerIDhCell);
}
            
new 
Handle:hCell GetArrayCell(g_hMapsDone[Type][Style], PlayerID);
            
new 
iSize GetArraySize(hCell); // This should work because the previous if statement should have forced hCell to be a valid handle 

Last edited by blaacky; 04-17-2014 at 15:53.
blaacky is offline
blaacky
Senior Member
Join Date: Oct 2012
Old 04-17-2014 , 16:13   Re: ADT_Array question
Reply With Quote #2

Basically, every index in the g_hMapsDone adt_array needs to be another adt_array. And I have to make sure an index is an adt_array before I do something with it. This is my only solution so far, which I stated I didn't want to do (using a separate variable). I don't even think this will work 100% of the time because when you make an array bigger with ResizeArray, there is a possibility of getting junk numbers, leaving the possibility that I will get a -1 on an index that hasn't been initialized as an adt_array.

PHP Code:
if(PlayerID >= GetArraySize(g_hMapsDone[Type][Style]))
{
    
ResizeArray(g_hMapsDone[Type][Style], PlayerID 1);
    
ResizeArray(g_hInitialized[Type][Style], PlayerID 1);
}
            
if(
GetArrayCell(g_hInitialized[Type][Style], PlayerID) != -1)
{
    new 
Handle:hCell CreateArray(3);
    
SetArrayCell(g_hMapsDone[Type][Style], PlayerIDhCell);
    
SetArrayCell(g_hInitialized[Type][Style], PlayerID, -1);
}
            
new 
Handle:hCell GetArrayCell(g_hMapsDone[Type][Style], PlayerID);
            
new 
iSize GetArraySize(hCell); 

Last edited by blaacky; 04-17-2014 at 16:14.
blaacky is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 04-17-2014 , 17:07   Re: ADT_Array question
Reply With Quote #3

Handles have the value INVALID_HANDLE by default. Just remember to set the handle variable back to INVALID_HANDLE when you CloseHandle it.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
blaacky
Senior Member
Join Date: Oct 2012
Old 04-17-2014 , 17:42   Re: ADT_Array question
Reply With Quote #4

HTML Code:
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
508550920
L 04/17/2014 - 16:38:42: [SM] Native "GetArraySize" reported: Invalid Handle 1e4
fdf08 (error: 4)
Here is the output from printing out the value of "GetArrayCell(g_hMapsDone[Type][Style], PlayerID)" every loop. The last output was 508550920, meaning the plugin didn't think it was an INVALID_HANDLE, and therefore skipped the if statement. Except it is still very much an INVALID_HANDLE, so GetArraySize didn't work on the hCell variable.

Here is the whole function:

PHP Code:
public LoadStats_Callback(Handle:ownerHandle:hndlString:error[], any:data)
{
    if(
hndl != INVALID_HANDLE)
    {
        for(new 
TypeType MAX_TYPESType++)
        {
            for(new 
StyleStyle MAX_STYLESStyle++)
            {
                
g_hMapsDone[Type][Style]    = CreateArray();
                
g_hInitialized[Type][Style] = CreateArray();
            }
        }
        
        new 
Position;
        new 
lMapIDlTypelStyle;
        new 
MapIDTypeStylePlayerIDFloat:Time;
            
        while(
SQL_FetchRow(hndl))
        {
            
MapID    SQL_FetchInt(hndl0);
            
Type     SQL_FetchInt(hndl1);
            
Style    SQL_FetchInt(hndl2);
            
PlayerID SQL_FetchInt(hndl3);
            
Time     SQL_FetchFloat(hndl4);
            
            if(
lMapID != MapID || lType != Type || lStyle != Style)
                
Position 0;
            
Position++;
            
            if(
PlayerID >= GetArraySize(g_hMapsDone[Type][Style]))
            {
                
ResizeArray(g_hMapsDone[Type][Style], PlayerID 1);
                
ResizeArray(g_hInitialized[Type][Style], PlayerID 1);
            }
            
            if(
GetArrayCell(g_hMapsDone[Type][Style], PlayerID) == INVALID_HANDLE)
            {
                new 
Handle:hCell CreateArray(3);
                
SetArrayCell(g_hMapsDone[Type][Style], PlayerIDhCell);
            }
            
            
/*
            if(GetArrayCell(g_hInitialized[Type][Style], PlayerID) != -1)
            {
                new Handle:hCell = CreateArray(3);
                SetArrayCell(g_hMapsDone[Type][Style], PlayerID, hCell);
                SetArrayCell(g_hInitialized[Type][Style], PlayerID, -1);
            }
            */
            
            
new Handle:hCell GetArrayCell(g_hMapsDone[Type][Style], PlayerID);
            
            new 
iSize GetArraySize(hCell);
            
ResizeArray(hCelliSize 1);
            
            
SetArrayCell(hCelliSizeMapID0);
            
SetArrayCell(hCelliSizePosition1);
            
SetArrayCell(hCelliSizeTime2);
            
            
lMapID MapID;
            
lType  Type;
            
lStyle Style;
        }
    }
    else
    {
        
LogError(error);
    }

The problem is in the ResizeArray function. A lot of unused indexes are junk numbers. Here is an example test.

PHP Code:
new Handle:hArray CreateArray();
ResizeArray(hArray10000);
for(new 
i10000i++)
    if(
GetArrayCell(hArrayi) != 0)
        
PrintToServer("%d"GetArrayCell(hArrayi)); 
There is a lot of crap in the array

Last edited by blaacky; 04-17-2014 at 19:06.
blaacky is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 04-17-2014 , 19:03   Re: ADT_Array question
Reply With Quote #5

ResizeArray doesn't set the new slots to any value if the array gets bigger. It's just garbage memory you're reading there (Same for ShiftArrayUp). You'd have to loop through the newly added slots in the array and set them to 0 manually or rethink the way you store data. I don't think it's a good idea to store array indices in a database. And from the look of it, you seem to have possible empty slots if that "PlayerID" in the database skips some number in the sequence.
__________________
Peace-Maker 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 10:27.


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