AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   ArrayDeleteItem() - Delete First Entry (https://forums.alliedmods.net/showthread.php?t=121875)

Drak 03-20-2010 16:21

ArrayDeleteItem() - Delete First Entry
 
I have an array, visually would look like this:

Code:

Slot 1: STEAM_ID_LAN|PLAYER: Hello World 1
Slot 2: STEAM_ID_LAN|PLAYER: Hello World 2
Slot 3: STEAM_ID_LAN|PLAYER: Hello World 3

Code:
            // Delete our previous one             for(new Count;Count < Size;Count++)             {                 ArrayGetString(g_NewspaperArray,Count,Message,255);                 strtok(Message,ArrayAuthID,35,Temp,1,'|');                                 trim(ArrayAuthID);                 remove_quotes(ArrayAuthID);                                                 if(equali(AuthID,ArrayAuthID))                 {                     if(++Num >= MAX_ADS)                     {                         server_print("we reached our max");                         ArrayDeleteItem(g_NewspaperArray,Count);                         break                     }                 }                             }
This will delete the latest entry in the array. (When it reaches it's max, three.) This will delete: STEAM_ID_LAN|PLAYER: Hello World 3 - I want it to delete: STEAM_ID_LAN|PLAYER: Hello World 1. How would I go about doing this?

EDIT:
I apparently can't think - id just snip the thread, but my fix obvious is to use "ArrayDeleteItem(g_NewspaperArray,Count - MAX_ADS)"

Bugsy 03-20-2010 17:43

Re: ArrayDeleteItem() - Delete First Entry
 
ArrayDeleteItem( g_NewspaperArray , 0 );?

I'm assuming this array holds data for each player so it will not be in order so using just 0 will not always work properly. Maybe you can include a timestamp (get_systime()) in the string and use that to delete the oldest? Then scan through items and find the entry with oldest timestamp (get_systime()-timestamp) and delete that one?

I don't understand 100% what you are trying to do, if you can explain further I can probably provide a better answer, or attempt to atleast.

fysiks 03-20-2010 18:12

Re: ArrayDeleteItem() - Delete First Entry
 
Quote:

Originally Posted by Bugsy (Post 1123706)
ArrayDeleteItem( g_NewspaperArray , 0 );?

I'm assuming this array holds data for each player so it will not be in order so using just 0 will not always work properly.

If all entries remain in the same order as they are placed in the dynamic array and if when an entry is deleted all the rest just shift down to fill in using 0 should work correctly every time, I think.

Is one of those two things not true with dynamic arrays?

Bugsy 03-20-2010 19:42

Re: ArrayDeleteItem() - Delete First Entry
 
Console command add\delete will show order in which items are added\removed. If you're always trying to remove the oldest item added to your array then using ArrayDeleteItem( g_NewspaperArray, 0 ) should always work.
PHP Code:

#include <amxmodx>

new Array:g_NewspaperArray;
new 
g_Added 3;

public 
plugin_init()
{
    
g_NewspaperArray ArrayCreate255 32 );
    
ArrayPushStringg_NewspaperArray"STEAM_ID_LAN|PLAYER: Hello World 1" );
    
ArrayPushStringg_NewspaperArray"STEAM_ID_LAN|PLAYER: Hello World 2" );
    
ArrayPushStringg_NewspaperArray"STEAM_ID_LAN|PLAYER: Hello World 3" );

    
register_concmd"add" "AddItem" );
    
register_concmd"delete" "DeleteItem" );    
}

public 
AddItem()
{
    new 
Message256 ];
    
    
formatexMessage 255 "STEAM_ID_LAN|PLAYER: Hello World %d" , ++g_Added );
    
ArrayPushStringg_NewspaperArray Message );
    
    for( new 
Count Count ArraySizeg_NewspaperArray ) ; Count++ )
    {
        
ArrayGetStringg_NewspaperArray Count Message 255 );
        
server_printMessage );
    }
}

public 
DeleteItem()
{
    new 
Message256 ];
    
ArrayDeleteItemg_NewspaperArray );
    
    for( new 
Count Count ArraySizeg_NewspaperArray ) ; Count++ )
    {
        
ArrayGetStringg_NewspaperArray Count Message 255 );
        
server_printMessage );
    }




All times are GMT -4. The time now is 08:41.

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