Discussion in 'Programming' started by TriiiKill, Sep 1, 2017.
Just like the title suggests.
For ds_list you can use a whole range of built in functions... i.e. sort, clear, read, write etc.
With arrays you have to do everything on your own...
So if you plan to do a lot of manipulation... use ds_list... but if you only need to declare a list of values which won't change during the game, or you don't plan any complicated manipulations - use an array...
An array by default can be 1d or 2d. A single list or a grid. Which you have to do by hand.
A ds_list is a special 1d array (a single column list) which comes with many functions! The list is easier to add items, sort, replace ect...
A ds_grid is your 2d array (a grid) which also comes with many functions to make it easier to add items, keep track of where you are on the grid...
You can look up ds_list and ds_grid in the help files for all the functions.
Arrays are slightly faster to use I recall, but it doesn't make a difference unless you are referencing them a lot. On the downside GML lacks the usual array handling stuff that many other languages have; there's no sorting, each-iterating or even simple in_array command.
Also, let's not forget a key difference: Arrays are automatically garbage collected, ds lists need to be freed manually.
I was learning about arrays and ds_lists, and I got to thinking about how similar they are and could not figure out the difference.
Thanks for your input!
Also a 2d array is not the same shape as a ds_grid. A ds_grid is rectangular in structure, whereas a 2d array has no defined shape and is merely an indexed set of 1d arrays.
If you're referring to memory addresses, that depends entirely on how the array is originally declared. Declaring an array's dimensions in reverse *SHOULD* keep all the memory addresses linear (ie, starting from the absolute extent of its dimensions). I think a ds_grid does that by default. Besides, all GM data structures are actually just black-boxed arrays with specialized GML functions to access/manipulate the data contained in them. At the actual code level, there's no difference between them.
Not sure I get why GM data structures aren't naturally garbage collected. There must be something in the way GM creates and handles the data itself that keeps them from going out of scope when they're no longer needed. -edit- I can only assume it's because there's another layer of code that references the created arrays which keeps all that data in scope even after referencing objects are deleted.
I mean a grid takes up w*h memory, but a 2d array takes up a+b+c+d+e+...etc memory. If you have a 3x4 grid, it takes up the equivalent of 12 variables. A [w,h] 2d array takes up a minimum 2w variables. A grid will always take up the same amount of memory, whereas a 2d array can take up a variable amout of memory.
Most people will never intentionally mane use of that difference, but it's there.
The technical term @TheouAegis is referring to is a "jagged array".
Ah! Gotcha. Wasn't quite sure what you meant, but that clears it up.