GML Need help with ds grids.

Discussion in 'Programming' started by SyntheticStorm9, May 27, 2017.

  1. SyntheticStorm9

    SyntheticStorm9 Member

    Joined:
    Oct 21, 2016
    Posts:
    98
    I am trying to make a simple variable storing grid but I cannot get it to work, if someone could post simple instructions for making a simple grid that would be really helpful,(creation,loading vars,submitting vars)
     
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,174
    sure thing
    Code:
    /// create 
    wdt = 64;
    hgt = 64;
    grid_name = ds_grid_create(wdt, hgt);
    
    /// write
    var xx = irandom(64);
    var yy = irandom(64);
    var val = random(10000);
    ds_grid_set(grid_name, xx, yy, val);
    // there will be a value between 0 and 10000 somewhere in this grid
    
    /// read
    var val = ds_grid_get(grid_name, xx, yy);
    
    Visually literally excel





    Biased notes:
    ds_grids are bad and you should consider arrays.
     
  3. Monsi

    Monsi Member

    Joined:
    Apr 30, 2017
    Posts:
    119
    Could you elaborate?
     
  4. toxigames

    toxigames Member

    Joined:
    Jun 20, 2016
    Posts:
    190
    I do not agree that ds_grids are bad. On the contrary imo ds_grids are fast and very useful and convenient for solving lots of tasks. Remember to destroy the ds_grid with ds_grid_destroy(index) when you're done with it, or risk memory leaks.
     
  5. Gamebot

    Gamebot Member

    Joined:
    Jul 3, 2016
    Posts:
    182
    If all you want to do is store information you can simply use a 2d array. Its better on the system and you don't have to worry about destroying it when your done. I use these for both menus and inventory.

    Though, in certain cases I use grids such as my playfair decoder/encoder simply because its easier to use the built in functions for getting x1, x2, y1, y2. I suppose someone else will post that im lazy with the previous. It just depends on what your planning to do with it.
     
  6. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,174
    ok, first ds_grid aren't bad I take it back but it is definetly worse at storing raw data.
    The singlemost reason why I hate ds_grids is that when reading out of bounds, you get messages on your console without notice. If you play on fullscreen you have no way to see those while playing the game and when you abort it you'll be suprised to wait at least 10 minutes for the messages to clear. So if you're new to grids and try to figure them out you just get punished with timeouts. But if you used arrays, which are also faster, they would never do that, they just tell you in the face what's wrong.

    In the end it depends on what you use.
    arrays for raw data or LUTs
    grids for dynamic visual data storing

    Honestly though, who ever uses the extra functions in ds_grids?
     
    toxigames likes this.
  7. toxigames

    toxigames Member

    Joined:
    Jun 20, 2016
    Posts:
    190
    Sure, ds_grids is not always the best choice I won't disagree with that. I have never had that out of bounds error happen, but I just tested it and you're right about writing to the grid the out of bounds error just prints to the console. One may or may not spot something wrong in game, depending on the particular effect the out of bounds error creates. I wonder why it doesn't actually throws and error? I wouldn't want my game continue to play (possibly with subtle bugs) with a near silent out of bounds error like that. Guess I learned something today ;) but even so this doesn't take away from all the convenience and flexibility ds_grids offer, but it is something to take extra care for I can see that. I couldn't provoke Gamemaker into hanging for 10 minutes though, even when going extreme out of bounds.

    What are the extra functions of ds_grids? I use a lot of the ds_grid functions, far from all of them though, guess it depends on what you want to do?
     
    Simon Gust likes this.
  8. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,174
    Things like getting the sum or the max value of a region of grid positions. I don't know what they're useful for but they're fast.
     
  9. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,212
    Well I use the min value of a region in my tetris-style game. The base grid is 0, blocks add 1 to the grid section they land on and then once the min value of a row is 1, I know it has been 'completed'.
     
    Monsi likes this.

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