AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Arrays (https://forums.alliedmods.net/showthread.php?t=226625)

Clauu 09-20-2013 17:59

Arrays
 
When an item goes deleted the order is restored or it just goes down with 1position? For example if we delete item 2 from an array with 4elems, how it will look like.. 0,1,3? If yes is any solution to be reordered and changed in the normal way? Does anyone knows how array: works here?

Black Rose 09-20-2013 20:27

Re: Arrays
 
Are we talking about static...
Code:
new array[4]
...or dynamic...
Code:
new Array:hArray = ArrayCreate()
...arrays?

hornet 09-20-2013 22:20

Re: Arrays
 
You can't delete values from a static array as such, but you can however recreate something similar to the ArrayDeleteItem() function, and "pull" all of the values down, if you were to assume something like a cell with its value being 0 would be empty.

When you delete an item from a dynamic array, you are actually deleting a cell, therefore all cells that come after the one you deleted will drop 1 position, and the total array size will drop by 1.

Clauu 09-21-2013 03:20

Re: Arrays
 
It;s about dynamic arrays, when i delete an item and then loop again with the new size i get invalid cellvector errors for the deleted position so i guess that the new array it isn;t remaped.

hornet 09-21-2013 03:37

Re: Arrays
 
Show your code and explain what your trying to do.

Clauu 09-21-2013 03:59

Re: Arrays
 
PHP Code:

function check_array() {
for(new 
data,i=0,size=ArraySize(arr_data);i<size;i++)
if((
data=ArrayGetCell(arr_data,i))==2) {
ArrayDeleteItem(arr_data,i);
break;
} } 

Now if the arr_data size is 4 and the position 2 is deleted then at the next function call it will throw a 'Invalid cellvector handle provided' error for that deleted position because it no longer exists and the array isn't reordererd with new keys.

Black Rose 09-21-2013 04:37

Re: Arrays
 
Your example doesn't delete the position 2, it deletes whatever position contains the value 2.
If you're going to post an example instead of your actual code, make sure that the example can produce the error.

I'm gonna guess that your actual loop doesn't contain break. Because then you will get that error unless you account for the removed entries.

Code:
/**  * Deletes an item from the array.  All items beyond it get shifted down 1 space.  *  * @param which         The array that contains the item to delete.  * @param item          The item to delete.  */ native ArrayDeleteItem(Array:which, item);

Code:
#include <amxmodx> public plugin_init() {     register_plugin("Test Plugin 6", "", "");         new Array:hArray = ArrayCreate(1,1);         for ( new i = 1 ; i <= 5 ; i++ )         ArrayPushCell(hArray, i);         new size = ArraySize(hArray);         for ( new i = 0 ; i < size ; i++ )         server_print("PRE: %d: %d", i, ArrayGetCell(hArray, i));         new deleted_items;         for ( new i = 0 ; i < size ; i++ ) {         server_print("Reading cell %d with value of %d", i - deleted_items, ArrayGetCell(hArray, i - deleted_items));         if ( ArrayGetCell(hArray, i - deleted_items) == 3 )             ArrayDeleteItem(hArray, i - deleted_items++);     }         size = ArraySize(hArray);     for ( new i = 0 ; i < size ; i++ )         server_print("POST: %d: %d", i, ArrayGetCell(hArray, i)); }

Code:

PRE: 0: 1
PRE: 1: 2
PRE: 2: 3
PRE: 3: 4
PRE: 4: 5
Reading cell 0 with value of 1
Reading cell 1 with value of 2
Reading cell 2 with value of 3
Reading cell 2 with value of 4
Reading cell 3 with value of 5
POST: 0: 1
POST: 1: 2
POST: 2: 4
POST: 3: 5


Clauu 09-21-2013 05:14

Re: Arrays
 
'==2' was just an value example and not position to delete, the code posted is similar to original that contains ents and i;m checking if those are valid before deletion. Hmm seems like the new array is remaped as it should but why it keeps giving me those errors :stupid:
PHP Code:

public remove_ents() {

    new 
size ArraySize(arr_ents);
    
    if(
size<1)
        return;

    new 
entid,timer,classname[32];
    for (new 
0sizei++) {
        
        if (!
is_valid_ent((entid ArrayGetCell(arr_entsi)))) {
            
ArrayDeleteItem(arr_entsi);
            
ArrayDeleteItem(arr_ents_timei);
            break;
        }
            
        
timer ArrayGetCell(arr_ents_timei);
        if(
timer == 0) {
            
pev(entidpev_classnameclassnamecharsmax(classname));
            if(
equali(classname"GENT"))
                
engfunc(EngFunc_RemoveEntityentid);
            
ArrayDeleteItem(arr_entsi);
            
ArrayDeleteItem(arr_ents_timei);
        }
        else
            
ArraySetCell(arr_ents_time,itimer 1);
    }



Black Rose 09-21-2013 05:30

Re: Arrays
 
The problem is you delete one cell without recounting or subtract the number of removed indexes.
The code works great except for 2 things.
* If you delete one item, the one after will be ignored because it was shifted to the position of the first one.
* When the loop reaches the end, it doesn't know that the size has changed. This is the reason it throws an error, you're trying to retrieve values outside of the array range.

Look at the example I gave. Especially the variable deleted_items.

Clauu 09-21-2013 06:24

Re: Arrays
 
Ups i forgot to add a break to the timer condition also, thanks :)


All times are GMT -4. The time now is 19:03.

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