• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM What is the most efficient way to create a list holding 3 values?

D

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?
 

Fabseven

Member
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)
 

Alexx

Member
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:
P

ph101

Guest
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.
 
A

anomalous

Guest
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).
 
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:
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).
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

Member
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.
 
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.
 
D

DariusWolfe

Guest
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 by a moderator:
Top