• 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!

GameMaker Using ds_maps inside ds_grid for inventory

C

Chafe

Guest
I've been trying to figure out my inventory for and RPG, and this is the idea I have:
1. Set up ds_maps containing information for each individual item (sprite, name, description, script for what it does when used etc)
2. Set up ds_grid for inventory, then use a script to add that item to my inventory when I want to
3. Call those ds_maps in those specific ds_grid positions when I need them
Basically, I'm trying to adapt the Beyond Us Gaming inventory tutorial from using Variables to set up each item every time I want to add it to my inventory to using a consistent list of ds_maps that I can just call whenever.
But is this smart at all? If I have 100+ items, then will Game Maker be able to handle that many ds_maps at once without massive memory leakage or whatever? Can I, in fact, actually access ds_map keys and values within a ds_grid?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
But is this smart at all? If I have 100+ items, then will Game Maker be able to handle that many ds_maps at once without massive memory leakage or whatever?
Okay, yes, GM can handle this without issue. :) I would also say it's a good approach... Just make sure to destroy each of the maps when you no longer need the ds_grid OR make the grid a global variable, populate it with empty maps on game start, then simply update the map contents as you go along (that way you'll never need to destroy them). Instead of maps you could also use arrays, and to make them easier to access simply create macro constants for each array entry (so array[0] becomes array[value], where value is a #macro constant).

Can I, in fact, actually access ds_map keys and values within a ds_grid?
Yes, you can and it's pretty easy. You'd do something like:

Code:
var _map = grid[# xx, yy];
var _value = _map[? "inventory_value"]
 
C

Chafe

Guest
Alright, I guess I'm going with my plan, then. Here's to hoping I can actually make it work!

My second question, though, would be when I should be destroying my ds_maps or ds_grid.
If I destroy any of my ds_maps at any time, won't that mean I won't be able to access that data, which I need to be able to call for an item-adding script (which basically goes "check what ds_map this object points to, add that ds_map to inventory)?
If I destroy my ds_grid, won't that mean that my inventory, which I need to be available at all times (since it's an RPG) would also disappear?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
My second question, though, would be when I should be destroying my ds_maps or ds_grid.
That's entirely up to you! You can create/destroy the grid when the player first eneters the game (from the menu screen or whatever) and then destroy it when you go back to the menu screen, or you can create it on game start and never destroy it.... the same for the maps. You can create them all and then simply set the data of them as you go through the game (have a key for "no data" for when a cell is empty), or you can add and remove the maps as you add or remove things from the inventory (in which case you'd set the grid cell value to -1 to show that the cell has no data).

If I destroy any of my ds_maps at any time, won't that mean I won't be able to access that data, which I need to be able to call for an item-adding script (which basically goes "check what ds_map this object points to, add that ds_map to inventory)?
Yep, if you try to access a map that doesn't exist you¡ll get an error. But like I said above, this isresolved by simply checking the value of the grid cell... all data structures are given integer indices starting at 0, so if a grid cell has a value greater than 0, it has a map... easy and quick check! Just remember to set the grid cell to -1 when the map is removed.

If I destroy my ds_grid, won't that mean that my inventory, which I need to be available at all times (since it's an RPG) would also disappear?
That will destroy the inventory, and again it comes down to how you want to program it. You can build a grid at game start and never destroy it, or you can create a grid whenever the game is started from the main menu of the game and destroy it when the player dies or goes back to the main menu.

Personally I'd favour the following construction...

In the very first room of the game have a game start event and in that create a GLOBAL ds grid.
Now at the start of the game from the main menu, clear the grid to -1 (so each cell = -1).
In the main game, whenever you add an item to the grid, create a ds map and store it's iD in the appropriate grid cell.
When the item is used or removed, destroy the ds map and set the grid cell to -1
On game end, loop through each grid cell and check to see if it's > -1, and if it is destroy the map for that cell.

Hope that helps!
 
C

Chafe

Guest
That does help a lot, thanks!
One last question, though: What would happen if I, at the start of the game, set the ds_maps for every item, and then called for those with a script that for example goes "add ds_map with the ID of "potion" to grid" when I need them? Instead of creating those ds_maps when I do an "add item to inventory" event, that is. Basically, I'd be creating a database of items at the start of the game that I would then be accessing when I need to, rather than just creating ds_maps for items when I add them to my inventory.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Yep, you could do that too. I suppose it would depend on the scope of the game and how you want items in the inventory to interact. Personally, I would probably do this as I think it would be easier to maintain and cleaner to code...
 
C

Chafe

Guest
Alright, I'll try it my way, and if it doesn't end up working out I can just change gears. Thanks a lot!
 
Top