1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GM:S 1.4 What is the most efficient way to create a list holding 3 values?

Discussion in 'Programming' started by DennisGMC, Oct 26, 2016.

  1. DennisGMC

    DennisGMC Guest

    In my game i need to create a list holding 3 values the sprite id x and y coordinates. I have no idea how long the list is going to be so i can't limit it like a ds_grid. What would be the most efficient way to do this?
     
  2. Fabseven

    Fabseven Member

    Joined:
    Oct 7, 2016
    Posts:
    354
    Is it a "return value" in a script ?
    If you want a readeable code you may create a enum
    Code:
    enum rv
    {
        posx=0,
       posy=1,
      sprite=2,
      <otherthing>=3,
     <anotherthing>=4
    }
    
    and a array like this :
    arrayreturn[rv.posx] =
    arrayreturn[rv.posy] =
    .....

    Warning : if you use an array in a script the return value is a pointer on this array, so after the script the original array (named in the script) will still be alive, the values you got from return in the script is more like a pointer.

    eg :
    Code:
    script random_pos
    arrayreturn[rv.posx] = irandom(100)
    arrayreturn[rv.posy] = irandom(100)
    return arrayreturn
    
    in code :
    rand = random_pos() //rand is a pointer on arrayreturn of the random_pos script
    
    
    so if you used random_pos again the values of rv.posx and rv.posy will update and you may have some bug if you are using the "rand pointer" in something.
    But no big deal if your copy the values like this (i think)
    Code:
       obj.x = rand[rv.posx]
       obj.y = rand[rv.posy]
    
    To avoid this problem you could use a 2d array or an offset as an argument of the script (and in the code of the script of course)
     
  3. Alexx

    Alexx Member

    Joined:
    Jun 21, 2016
    Posts:
    440
    Personally, with 3 values, efficiency shouldn't be your main priority, unless you're processing each a few hundred times or so each step.

    Your approach should also consider how and what you will be doing with values, ie:
    • Will they change?
    • Will you need to sort them?
    • Will you need to retrieve from certain instances?

    A little more information is needed to make a fair assessment.
     
    Last edited: Oct 26, 2016
  4. ph101

    ph101 Member

    Joined:
    Jun 20, 2016
    Posts:
    416
    can't you use a 2d array for this?

    Code:
    item[0,1] = item0.sprite_index;
    item[0,2] = item0.x;
    item[0,3] = item0.y;
    item[1,1] = item1.sprite_index;
    item[1,2] = item1.x;
    item[1,3] = item1.y;
    
    etc.
     
  5. anomalous

    anomalous Member

    Joined:
    Jun 29, 2016
    Posts:
    464
    You can resize grids FYI. I mention that because grids are very useful for a variety of things and its annoying but you can indeed resize them as needed.

    For just insert and retrieve (without sorting or saving, etc.), I think I'd try a 2D array. If you need to do lookups or any other functions with it, I'd use a data structure.

    When I do not need high performacne, I use ds_list, probably because I can add 15 values at a time, and can do lookups, etc. Trivial to save too (ds_list_write).
     
  6. Online Handle

    Online Handle Member

    Joined:
    Oct 11, 2016
    Posts:
    242
    If you want it for efficiency, I would suggest using three separate hash maps.
    You should get around O(1) per lookup (hopefully).
    http://docs.yoyogames.com/source/dadiospice/002_reference/data structures/ds maps/

    If you want to use only one map, instead of three, then I would suggest having it store arrays of size three, where each array holds the sprite id, x, and y.

    Edit:
    I just realized you said a list. I would suggest using a queue, in which everytime you dequeue an item, you enqueue it back into the queue. I would have the queue store arrays which would hold the sprite id, x, and y information.

    https://docs.yoyogames.com/source/dadiospice/002_reference/data structures/ds queues/index.html
     
    Last edited: Oct 26, 2016
  7. stainedofmind

    stainedofmind Member

    Joined:
    Jun 20, 2016
    Posts:
    701
    FYI, the argument limit for scripts and functions was removed an update or two back. you can pass as many values ad you need to them now.
     
    Hyomoto likes this.
  8. Hyomoto

    Hyomoto Member

    Joined:
    Jul 7, 2016
    Posts:
    1,078
    Damn, I missed that in the patch notes, but that's damn good to know. I take it the argumentX variables are unchanged, but I can use argument[ x ]. The manual hasn't been updated to reflect that change so it's speculative. I'm guessing it probably also obeys the array restriction then, but having to pass 65k arguments would be a bit silly.
     
  9. stainedofmind

    stainedofmind Member

    Joined:
    Jun 20, 2016
    Posts:
    701
    Yeah, I would assume the same. I missed it in the update too, but read from another forum member about it. Super stoked about it, even though I had to go through my script library and modify a bunch of things. This might also only be in the Beta release.
     
  10. DariusWolfe

    DariusWolfe Member

    Joined:
    Sep 23, 2016
    Posts:
    32
    Not sure exactly how efficient, but I've never seen a problem with using a single array or list to hold references to objects, and then getting the x and y values from the referenced object, and it's dead simple.

    Like so:

    for(i=0;i<v_arraylength;i++)
    {
    draw_circle(v_array<i>.x,v_array<i>.y,5)
    }

    This would draw a circle with radius 5 at the x,y of each object referenced in the array.

    Edit: Wow, I'm a bonehead. That code was completely nonfunctional, because I didn't realize it would read the array references as formatting code. Read angle brackets as straight brackets.
     
    Last edited: Oct 27, 2016

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