AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Clone dynamic array (https://forums.alliedmods.net/showthread.php?t=278421)

DavidLin 01-31-2016 11:45

Clone dynamic array
 
I made code which randomly spawns entities from dynamic array, the problem is that when deleting from temporary array it also deletes from the main array, is it possible to clone the array or something so that the main array could not be edited, tried ArrayClone( ); but it gets undefined.

PHP Code:

        new Float:Orig], Float:Angl];
        new 
AmountCvar   get_pcvar_numcvar_amountSpawn );
        new 
ArraySizeBox ArraySizeSupplyBoxes );
        new 
size = ( AmountCvar == || AmountCvar ArraySizeBox ) ? ArraySizeBox AmountCvar;
        new 
DataSupplyBoxesArray ];
        new 
countrandomId;
        new Array:
Boxes;
        
Boxes SupplyBoxes;

        do {
                
randomId randomArraySizeBox );
                
ArrayGetArrayBoxesrandomIdData );
                
Orig] = Databox_origin ][ ]; Angl] = Databox_angles ][ ];
                
Orig] = Databox_origin ][ ]; Angl] = Databox_angles ][ ];
                
Orig] = Databox_origin ][ ]; Angl] = Databox_angles ][ ];
                
spawnSupplyBoxOrigAnglDataline_id ] );
                
count++;

                
ArrayDeleteItemBoxesrandomId );
                
ArraySizeBox--;
        } while( 
count size ); 


Bugsy 01-31-2016 12:08

Re: Clone dynamic array
 
If you are doing stuff on a temp array, it should not affect the main array if it is not referenced. Which one is the one you want modified and which should remain untouched?

DavidLin 01-31-2016 12:18

Re: Clone dynamic array
 
The temp array is
new Array:Boxes;

And the main array is SupplyBoxes which is filled when plugin starts. The spawn event is made after some time from round start. I came up with a temporary solution but I think it isn't efficient ( to push main arrays items to temp by a loop ).

PHP Code:

        new Float:Orig], Float:Angl];
        new 
AmountCvar   get_pcvar_numcvar_amountSpawn );
        new 
ArraySizeBox ArraySizeSupplyBoxes );
        new 
size = ( AmountCvar == || AmountCvar ArraySizeBox ) ? ArraySizeBox AmountCvar;
        new 
DataSupplyBoxesArray ];
        new 
countrandomId;
        new Array:
Boxes ArrayCreateSupplyBoxesArray );
        for( new 
index 0index ArraySizeBoxindex++ ) {
                
ArrayGetArraySupplyBoxesindexData );
                
ArrayPushArrayBoxesData );
        }

        do {
                
randomId randomArraySizeBox );
                
ArrayGetArrayBoxesrandomIdData );
                
Orig] = Databox_origin ][ ]; Angl] = Databox_angles ][ ];
                
Orig] = Databox_origin ][ ]; Angl] = Databox_angles ][ ];
                
Orig] = Databox_origin ][ ]; Angl] = Databox_angles ][ ];
                
spawnSupplyBoxOrigAnglDataline_id ] );
                
count++;

                
ArrayDeleteItemBoxesrandomId );
                
ArraySizeBox--;
        } while( 
count size ); 


Bugsy 01-31-2016 13:13

Re: Clone dynamic array
 
What is the actual purpose (in English) of what this code is doing.

DavidLin 01-31-2016 13:20

Re: Clone dynamic array
 
1. The items from main array are passed to a temporary array.
2. The code randomly chooses array items and passes their info to another function while deleting temporary arrays items.

But somehow it deletes not only the temporary arrays items but the mains too.

klippy 01-31-2016 13:24

Re: Clone dynamic array
 
You can consider variables containing dynamic arrays handles as references. That means that by doing:
PHP Code:

new Array: arr1 ArrayCreate();
new Array: 
arr2;
arr2 arr1

arr2 will be a reference to arr1, thus by changing arr2 you are basically changing arr1.

You said you tried ArrayClone but it didn't work. Can you show us what did you try?

DavidLin 01-31-2016 13:30

Re: Clone dynamic array
 
I used it like new Array: Arr2 = ArrayClone( Arr1 ); but the compiler showed me that the function is undefined so I checked the cellarray.inc and there wasn't such a function, maybe I am using an older amxmodx but I downloaded it from the original website

klippy 01-31-2016 13:43

Re: Clone dynamic array
 
Right, doesn't look like there is ArrayClone pre-1.8.3. Well, too bad, I guess you'll have to copy the array yourself (like you tried), or think of another system for managing your array for what you are trying to do.


All times are GMT -4. The time now is 09:23.

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