1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

[Solved] What's the Difference between an Array and a Ds_List?

Discussion in 'Programming' started by TriiiKill, Sep 1, 2017.

  1. TriiiKill

    TriiiKill Guest

    Just like the title suggests.
     
  2. Smiechu

    Smiechu Member

    Joined:
    Jul 14, 2017
    Posts:
    626
    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...
     
  3. Gamebot

    Gamebot Member

    Joined:
    Jul 3, 2016
    Posts:
    191
    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.
     
    Wraithious likes this.
  4. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    2,012
    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.
     
  5. stainedofmind

    stainedofmind Member

    Joined:
    Jun 20, 2016
    Posts:
    701
    Also, let's not forget a key difference: Arrays are automatically garbage collected, ds lists need to be freed manually.
     
  6. TriiiKill

    TriiiKill Guest

    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!
     
  7. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,148
    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.
     
    stainedofmind likes this.
  8. CMAllen

    CMAllen Member

    Joined:
    Mar 2, 2017
    Posts:
    856
    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.
     
    Last edited: Sep 1, 2017
  9. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,148
    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.
     
    atmobeat and CMAllen like this.
  10. stainedofmind

    stainedofmind Member

    Joined:
    Jun 20, 2016
    Posts:
    701
    The technical term @TheouAegis is referring to is a "jagged array".
     
  11. CMAllen

    CMAllen Member

    Joined:
    Mar 2, 2017
    Posts:
    856
    Ah! Gotcha. Wasn't quite sure what you meant, but that clears it up.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice