GameMaker Script Based Index vs Global Instance Based Index - Quiestion

D

Daniel Cook

Guest
Hey guys,
I'm working on a project item system and I was hoping for a bit of guidence regaruding the best practice to storing large amounts of data. The set-up is this:
I have numour item within my game, lets say 100 for now.
Each of though items holds 10 variables with different values.
I have used an Enum to give each of my items a uniqe IDNumber.

Now when I want to create a new item I have come up with 2 ways of doing this.
The first (which in my mind is the better way), is to have a script which takes the ID number as an argument and then uses a Switch function to assign the correct values to a new instance.

var _itemID = argument0;
var _inst = instanceCreate();

switch(_itemID){
case itemIndex.gun:
_inst.var1 = 10;
_inst.var2 = 2;
...
break;
case itemIndex.sword:
_inst.var1 = 2;
_inst.var2 = 54;
...
break;​
...​
}

return _inst;

The second, is to have a global instance holding all of the data in arrays. So within a script I would do this.

var _itemID = argument0;
var _inst = instanceCreate();
var _index = global.itemData_inst;

inst.var1 = _index.array1[_itemID];
inst.var2 = _index.array2[_itemID];
...​
return _inst;

I have a limited understanding of how computer alocate memory, but from what I understand. If i go with option 2, then a chucnk of memory would be assigned for all 10 variable for all 100 items. So it is holding 1000 variables all the time for something that I only needs to call on maybe 2 times every minute.

While option 1 would mean that the data isn't stored perminently, but only for the step when the script is called. It would also only store the 10 variable within the relivent Case Block, as oposed to all 1000 variables at all times.

Is my thinking correct or am I was off the mark.
Thank for reading I know its a long 1.
 

Simon Gust

Member
I personally would go with arrays. Objects need so much more memory, not because of the variables assigned but because of the built in variables and other stuff they have. On top of that, Objects take cpu even when idling.
Note: Don't worry about memory on a scale of what you are doing.

Alternative:
You can store multiple numbers in a single number.
Since each number is 32 bits (4bytes) large you can set individual bits to a variable. Though 10 variables is a lot.
Can you tell me what exactly those variables are supposed to hold? It may not even be necessary to have those.
 
D

Daniel Cook

Guest
[QUOTE="Simon Gust[/QUOTE]
Thanks for getting back.
The bulk of the code is to work with then inventory system I am creating. I'm trying to create an inventory system in the style of resident evil 4, where your capacity is based on a grid rather then a number representing weight. The information I hold within my items is as followed:

item_data_type = Item_IDs.ammo_9mm; //Item Types ID
item_data_amount = 50; //Amount of items in the stack
item_data_capacity = 50; //Capacity of items stack
item_data_inv_gridPos[1] = 0; //Inventory Grid Posision
item_data_inv_size[1] = 2; //Inventory Grid Size Y
item_data_inv_size[0] = 1; //Inventory Grid Size X
item_data_inv_rotation = 0; //Inventory Rotation
item_data_inv_autoDelete = true; //Inventory Empty Delete
item_data_inv_graphic = spr_item_icon_ammo_ph_9mm; //Inventory Graphic

Then within my inventory system I uses a List & Grid DS to keep track of what is in the inventory and were it is posisioned. The extra 2 variable are for speical items like guns which would have an amount of bullets within them.

The basic idea was when a player picks up a item, I shove it off screen and keep it to hold all of the data needed by the inventory system instance. I came from Unity to GM. So i would have perferd to create a custom class that just held all that data for me, creating an object instance seemed like the closest thing to a class in GM. but I never though about all of the built in variables that ship with each instance. :(

I have found a post on building structs from array, which I would like but I need to post 3 more posts befor I can do that. So I'm thinking that might be a better way to go.

If you have an alternative suggestion I'de love to hear it though. :D
 

Simon Gust

Member
Why not store item info in a LUT using just the item_data_type.
You have to think about what info can be individual and what can be generalized.
item_data_type is individual as it is the core of the information about the item
item_data_amount is also individual.
item_data_capacity is not individual, two items of the same type have the same capacity.
item_data_inv_gridPos seems unnecessary, you have the position inside a 2D array or grid built in.
item_data_inv_size[1] the amount of spaces the item takes in y direction can also be generalized.
same for the width.
rotation is individual.
don't know what auto delete does.
item_data_inv_graphic is general, this is a good example, you don't have to store the graphic of an item in this, you could just use the type to point to another array that holds the info for that type of item

concept
Code:
item {maxStack, invHgt, invWdt, graphic}
item_lut[0, item.maxStack] = 50;
item_lut[0, item.invHgt] = 2;
item_lut[0, item.invWdt] = 1;
item_lut[0, item.graphic] = spr_item_whatever;
Here I described the general info for the item "1".
Now I can use this info when I please to

Code:
for (var i = 0; i < 12; i++)
{
  for (var j = 0; j < 12; j++)
  {
    var itemID= inventory[i, j];
    if (itemID != -1)
    {
       var sprite = item_lut[itemID, item.graphic];
       draw_sprite(sprite, 0, i * 16, j * 16):
    }
  }
}
 
G

Guest

Guest
@Simon Gust This is a really low level question, but I'm still grappling with the term "look up table." The only good google result related to GM is, I think, another post by you on the term.

Just for posterity, can you verify that the following combination of two enums and a 2D array is a LUT?

Code:
enum weapons
  {
  dagger,
  sword
  }

enum weapon_specs
  {
  hit,
  dam,
  price
  }

weapons_lut[weapons.dagger, weapon_specs.hit] = 5;
weapons_lut[weapons.dagger, weapon_specs.dam] = 1;
weapons_lut[weapons.dagger, weapon_specs.price] = 10;
weapons_lut[weapons.sword, weapon_specs.hit] = 2;
weapons_lut[weapons.sword, weapon_specs.dam] = 6;
weapons_lut[weapons.sword, weapon_specs.price] = 20;

var sellprice = weapons_lut[weapons.sword, weapon_specs.price] * .5;
 
Last edited by a moderator:

Simon Gust

Member
@Simon Gust This is a really low level question, but I'm still grappling with the term "look up table." The only good google result related to GM is, I think, another post by you on the term.

Just for posterity, can you verify that the following combination of two enums and a 2D array is a LUT?

Code:
enum weapons
  {
  dagger,
  sword
  }

enum weapon_specs
  {
  hit,
  dam,
  price
  }

weapons_lut[weapons.dagger, weapon_specs.hit] = 5;
weapons_lut[weapons.dagger, weapon_specs.dam] = 1;
weapons_lut[weapons.dagger, weapon_specs.price] = 10;
weapons_lut[weapons.sword, weapon_specs.hit] = 2;
weapons_lut[weapons.sword, weapon_specs.dam] = 6;
weapons_lut[weapons.sword, weapon_specs.price] = 20;

var sellprice = weapons_lut[weapons.sword, weapon_specs.price] * .5;
Yes, but It doesn't even have to be an array, anything that has two indexes can be a lut. Enums aren't a must, they are certainly useful though.
The problem is that gm has very limited ways to make luts.
Your code is a perfect example btw.
 
Top