[ TUT ]Dynamic Array
Dynamic Array What you have to know before we begin ?
First, I will call dynamic arrays dyn arrays, and the normal arrays as normal arrays( note that normal mean regular in this context, not perpendicular or sth else. I also don't like the word default, some people may think that dyn arrays are not default and they should not be used) But, before we start what is a regular array ? This quote from wiki is self explanatory: Quote:
Code:
But, let's go back to the topic. A dynamic array basically do the same, but you can have unlimited storage space, with an dyn array you assign data to a cell. If you want a comparasion between tries and dyn arrays read this: https://forums.alliedmods.net/showthread.php?t=88396 Limitations:
Quote:
For example, if you get some content from a file, just when map change/ server start, you can use a dynamic array without problems. You instert data just one time, then you retrieve it when you need, this is generally better than a normal array, in most cases you won't know the dimension of what you read from file. If you will look in cellarray.inc you will notice that dyn arrays have a lot of native, a nightmare, really. I can't explain all of them so I will choose the most used ones, I may edit later and add more.
As you can see, even with just the most usefull ones, we have a list of 11 natives. Now, let's create a simple plugin: Code:
So, what this plugin do ? Well, nothing, it just create a dynamic array with 1 as cellsize and then destroy it. What you can see ?
Now, let's advance a bit. Code:
What this plugin will do ? Well, when you execute cellarray_writetest from server console, it will push all the numbers from the cost to the array, then when you type cellarray_readtest it will print everything. Output: Code:
1. ArrayPushCell( Array:which, any:input )
2.ArraySize( Array:which )
3.ArrayGetCell( Array:which, item )
Now, another example for strings: Code:
Output: Code:
] cellarray_writetest 1. I changed the ArrayCreate line, we are going to store string, so we should provide a maxim size for that string. I choosed 15, even if it's too high for so little string, just for an example. 2. ArrayPushString( Array:which, const input[] )
3. ArrayGetString( Array:which, item, output[], size )
Now, the last part, pushing an array to an dyn array. I will choose an enum for this, but you don't need it everytime. If you don't know how to use an enum, you are invited to read here https://forums.alliedmods.net/showthread.php?t=140103 and here https://forums.alliedmods.net/showthread.php?t=26634 Another quick plugin example: Code:
This one is completly different. 1. ArrayCreate( eData ): we create the array with the cellsize = eData size 2. I'm not doing for loops, since I'm using only the entry 0 to push the array, sure that you should create a loop when using more entryes. 3. new Data[ eData ] this will create a normal array with the data. Copy in Data[ szString ] a simple text, this is done with copy native. Assign to Data[ iValue ] a random number, let's say 15. Push everyting into the array with ArrayPushArray. 4. When we read from the file, we create a normal array again, sure that it can be global, but I think in this way it's more clear. Get in the Data arrray what the dyn array contains, and print the info. Output: Code:
String: test
6. ArrayGetArray( Array:which, item, any: output[] )
Huh, I'm done with all of this stuffs. Some end notes:
If you have questions fell free to ask. This is my first tutorial, I'm sorry for my english, also I'm not used to give explanations, if you can't understand something please ask, I will try to say in other words. Also, for more informations about this natives and for more natives see cellarray.inc, everything is well documented. |
Re: Dynamic Array
It's very nice, thanks you Hamlet :)
|
Re: [ TUT ]Dynamic Array
Quote:
|
Re: [ TUT ]Dynamic Array
I was speacking about normal arrays and not the dynamic ones. If someone think a bit, it's obvious that dynamic arrays are only 2nd dimension, the first one is the entry number, which is dynamic, the second one is the cellsize, with is fixed, so it's static. I guess I didn't explained well what I mean with unlimited storage, I can't say exactly what I want in english, my vocabular is pretty limited.
Edit: added more info about dynamic arrays, limitations and another native. |
Re: [ TUT ]Dynamic Array
I pointed it out because you mention that 3 dimensional arrays are possible but 4+ dimensions are not possible which implies that your tutorial will explain how to use dynamic arrays to overcome that limitation.
|
Re: [ TUT ]Dynamic Array
Quote:
|
Re: [ TUT ]Dynamic Array
Some thoughts:
Quote:
Quote:
Quote:
Quote:
Quote:
Current pre-allocates memory but don't update count. This means you still have to use Push (and not Set). To have like documentation says, you need to use ArrayResize(), this will update the count (so real blank entry) and you will be able to use Set directly. Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
About the presentation itself, it would be cool you make anchors on your natives menu so we can jump directly to the native we want. |
Re: [ TUT ]Dynamic Array
Most of your points are taking into accout the dev builds and I'm not used with them, also this tutorial is for the latest stable version, 1.8.2
I said I can't explain all, because I don't have the time to write them, not that I don't know or whatever. Quote:
Quote:
Quote:
Quote:
Quote:
I will also add anchors tag. |
Re: [ TUT ]Dynamic Array
Quote:
I think you should either clearly stated is about only <= 1.8.2 or adding support for 1.8.3 at the same time. IMO, you should do the latter. |
Re: [ TUT ]Dynamic Array
Sorry for bumping.
1. Is it possible to skip the "Array" part when creating an Array ? Like instead of PHP Code:
PHP Code:
2. why is this called a 2 Dimensional array if it has got 3 parameters PHP Code:
|
All times are GMT -4. The time now is 03:21. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.