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

creating inventory using ds_map

I'm trying to make an inventory using the ds_map function that will contain the cards that the player doesn't have in their deck. At the start of the game I want the player to have three cards already in their inventory (Barrier, Bomb, and Snipe Shot as you'll see in the code below). I seem to keep getting this error every time I try running the code in game:

Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 2
of Create Event
for object inventory_controller:

Push :: Execution Error - Variable Index [0,1] out of range [1,1] - -1.inv_card(100024,1)
at gml_Object_inventory_controller_CreateEvent_2 (line 23) - ds_map_add(global.inventory, inv_card[inv_card_num], "Bomb"); // This card will be added to the next position in the array and stored in the inventory
############################################################################################
This the code I'm using in my inventory_controller object:
Code:
// This code will set up the player's bag/inventory for the start of the game

global.total_inv_slots = 200

//

global.inventory = ds_map_create()

// Use an array to allow for more flexiablity...
inv_card[0] = 0; // have to initalize the key value before using it
inv_card_num = 0;  // use "inv_card_num" as a variable instead of just "i" to be more specfic


// Note that a "card name" should be assigned to the "inventory card"
// The following code adds Snipe Shot  card to the player's bag/inventory
ds_map_add(global.inventory, inv_card[inv_card_num], "Snipe Shot"); // I chose inv_card instead of card1 as the variable cause I have assigned card1 already
//           map id              key                    value


inv_card_num++ // increase the variable so that the next card added to the player's inventory will be added to the next position in the array


ds_map_add(global.inventory, inv_card[inv_card_num], "Bomb"); // This card will be added to the next position in the array and stored in the inventory


inv_card_num++ // increase the variable so that the next card added to the player's inventory will be added to the next position in the array


ds_map_add(global.inventory, inv_card[inv_card_num], "Barrier");
Any ideas as to why this isn't working for me?
 

wamingo

Member
On line 23, you're trying to store "Bomb" at index inv_card[1], but you haven't initialized index 1 in inv_card.
 
On line 23, you're trying to store "Bomb" at index inv_card[1], but you haven't initialized index 1 in inv_card.
Yeah, you were right. Thanks. I fixed the code by initializing inv_card by using a blank string and the inv_card_num variable as the index:

Code:
// This code will set up the player's bag or inventory for the start of the game

global.total_inv_slots = 200

//

global.inventory = ds_map_create()

// Use an array to allow for more flexiablity...
inv_card[0] = ""; // have to initalize the key value before using it
inv_card_num = 0;  // use "inv_card_num" as a variable instead of just "i" to be more specfic... [controls the position of the inv_card array]


// Note that a "card name" should be assigned to the "inventory card"
// The following code adds Snipe Shot  card to the player's bag/inventory
ds_map_add(global.inventory, inv_card[inv_card_num], "Snipe Shot"); // I chose inv_card instead of card1 as the variable cause I have assigned card1 already
//           map id              key                    value


inv_card_num++ // increase the variable so that the next card added to the player's inventory will be added to the next position in the array
inv_card[inv_card_num] = "" // THIS IS IMPORTAMT! Must set the next inv_card index (in this case inv_card[1]) to a blank string ""

ds_map_add(global.inventory, inv_card[inv_card_num], "Bomb"); // This card will be added to the next position in the array and stored in the inventory


inv_card_num++ // increase the variable so that the next card added to the player's inventory will be added to the next position in the array
inv_card[inv_card_num] = "" // (sets inv_card[2] = "" )

ds_map_add(global.inventory, inv_card[inv_card_num], "Barrier");

However, now I'm concerned as to how I would keep track of how many of each card is in the player's inventory. Would I just do another ds_add statement like this?:
Code:
ds_map_add(global.inventory, "Snipe Shot", 3); // add three snipe shot cards to your inventory
ds_map_add(global.inventory, "Bomb", 1); // add one Bomb card to your inventory
ds_map_add(global.inventory, "Barrier", 1); // add one Barrier card to your inventory
 
Last edited:

wamingo

Member
I think you're not using the map as intended.

Code:
first add the card types with a default number of cards - probably zero.

ds_map_add( inventory, "Snipe Shot", 0)
ds_map_add( inventory, "Bomb", 0)
etc...

then when you want to give the player a card of type "Bomb", you CAN use
ds_map_find_value and ds_map_replace,
OR you can do yourself a favor and use the '?' accessor
(you could also have done that above):

inventory[? "Bomb"] += 1;
 
I think you're not using the map as intended.

Code:
first add the card types with a default number of cards - probably zero.

ds_map_add( inventory, "Snipe Shot", 0)
ds_map_add( inventory, "Bomb", 0)
etc...

then when you want to give the player a card of type "Bomb", you CAN use
ds_map_find_value and ds_map_replace,
OR you can do yourself a favor and use the '?' accessor
(you could also have done that above):

inventory[? "Bomb"] += 1;
Okay, I tried that and it works, so now I can add and remove the cards from the inventory. Now I'm trying to figure out how to draw the contents of the inventory on screen and allow the player to scroll through the cards they have.
 
Top