Raised This Month: $ Target: $400
 0% 

Clone dynamic array


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
DavidLin
Member
Join Date: Jan 2016
Old 01-31-2016 , 11:45   Clone dynamic array
Reply With Quote #1

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 ); 
DavidLin is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-31-2016 , 12:08   Re: Clone dynamic array
Reply With Quote #2

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?
__________________
Bugsy is offline
DavidLin
Member
Join Date: Jan 2016
Old 01-31-2016 , 12:18   Re: Clone dynamic array
Reply With Quote #3

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 ); 
DavidLin is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-31-2016 , 13:13   Re: Clone dynamic array
Reply With Quote #4

What is the actual purpose (in English) of what this code is doing.
__________________
Bugsy is offline
DavidLin
Member
Join Date: Jan 2016
Old 01-31-2016 , 13:20   Re: Clone dynamic array
Reply With Quote #5

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.
DavidLin is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 01-31-2016 , 13:24   Re: Clone dynamic array
Reply With Quote #6

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?

Last edited by klippy; 01-31-2016 at 13:25.
klippy is offline
DavidLin
Member
Join Date: Jan 2016
Old 01-31-2016 , 13:30   Re: Clone dynamic array
Reply With Quote #7

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
DavidLin is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 01-31-2016 , 13:43   Re: Clone dynamic array
Reply With Quote #8

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.
klippy is offline
Reply


Thread Tools
Display Modes

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 09:23.


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