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

issue when adding to inventory from deck

I'm currently setting up the deck editor for my game and was testing it to see if I could successfully remove cards from my deck. I was able to do so successfully, but when my deck is gone (empty) and I press the "N" key again I get this error:

Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 4
of  Step Event0
for object deck_editor_controller:

DoAdd :2: undefined value
at gml_Object_deck_editor_controller_StepNormalEvent_4 (line 27) -        global.inventory[? sel_card1Q] += 1; // This code SHOULD add one "instance" of the card to the inventory. (E.X. If the player selects the Bomb card twice, a bomb card will be added to the inventory.)
############################################################################################
Here the code for the Step Event of deck_editor_controller
Code:
// Change animation speed based on whether the player is selecting or not selecting a card
if selecting = false { image_speed = 0.3 }
if selecting = true { image_speed = 0.6 } // cursor animates faster if the player is selecting a card(s)

//
if select_press < 0 { select_press = 0; }
if select_press > 2 {select_press = 2; }

// Code for selecting
if keyboard_check_pressed(ord('N')) && selection_mode = "deck" && !ds_map_empty(global.player_deckA)
{
    select_press++;  // increase the value of this variable, so that the game knows whether the player selected a card once or twice

    if deck_position = 1 && selecting = false && !ds_list_find_value(global.mydecklist, 0) = ""  // if the player isn't currently selecting this particular card or the slot isn't blank
    {
        var sel_card1;
        sel_card1 = ds_list_find_value(global.player_deckA, 0); // find the value of the first card listed from your deck and store it
        ds_queue_enqueue(card_selection_queue, sel_card1); // This will store the value of the card selected in the queue
        selecting = true; // set the variable that monitors whether the player is selecting something at the moment 
    }

        if deck_position = 1 && selecting = true && !ds_list_find_value(global.mydecklist, 0) = ""  && select_press = 2 // if the player IS already selecting this particular card
       {
        // If the player selects the same card twice, read the queue, remove it from the deck, and create a blank slot
       var sel_card1Q; //temporary variable that will be used to help put the card into the inventory of the player
       sel_card1Q = ds_queue_head(card_selection_queue); // Before removing the card from the deck and decklist, read from the queue to get the value (card name) of the card being sent to the inventory
       global.inventory[? sel_card1Q] += 1; // This code SHOULD add one "instance" of the card to the inventory. (E.X. If the player selects the Bomb card twice, a bomb card will be added to the inventory.)

      
        ds_list_delete(global.mydecklist, 0); // Deletes the first card in the decklist (must do this cause of the way the deck_controller for battle is set up)
        ds_queue_clear(card_selection_queue); // This will clear the queue (NOT DESTROY IT) allowing for another card name to be stored there
      
        ds_map_replace(global.player_deckA, global.deckAcard1, ""); // create a blank space, indicating the card is no longer in the deck
        selecting = false
        select_press = 0; // set the variable back to 0
        }
 
P

Paolo Mazzon

Guest
DoAdd :2: undefined value
That means the key you gave doesn't exist. When you say
Code:
var sel_card1Q;
sel_card1Q = ds_queue_head(card_selection_queue);
global.inventory[? sel_card1Q] += 1;
You are assuming that whatever is at the head of that queue exists within your map. In this case, it doesn't. What you need to do is check if it exists before adding anything to it.
Code:
var sel_card1Q;
sel_card1Q = ds_queue_head(card_selection_queue);
if (!ds_map_exists(global.inventory, sel_card1Q))
    global.inventory[? sel_card1Q] = STARTING VALUE;
global.inventory[? sel_card1Q] += 1;
 
DoAdd :2: undefined value
That means the key you gave doesn't exist. When you say
Code:
var sel_card1Q;
sel_card1Q = ds_queue_head(card_selection_queue);
global.inventory[? sel_card1Q] += 1;
You are assuming that whatever is at the head of that queue exists within your map. In this case, it doesn't. What you need to do is check if it exists before adding anything to it.
Code:
var sel_card1Q;
sel_card1Q = ds_queue_head(card_selection_queue);
if (!ds_map_exists(global.inventory, sel_card1Q))
    global.inventory[? sel_card1Q] = STARTING VALUE;
global.inventory[? sel_card1Q] += 1;
What you suggested works, but you had part of it backwards. The way you put it still gave me errors. I had to set the starting value first, then check to see if the map and other variable exists. Anyway, thanks for your help.
 
Top