Fatal error: Unable to find any instance for object index '9' name 'inventory'

Ok so I'm trying to get it so when I pick up my items a notification will pop up. I can get it to pop up when I drop items but not when I pick them up. I get a error message like below. Appreciate any help and will post some more code below if you nee more info or code let me know.

Code:
FATAL ERROR in
action number 1
of  Step Event0
for object obj_item:


Unable to find any instance for object index '9' name 'inventory'
 at gml_Script_create_notification (line 7) - var _item_name = inventory.ds_items_info[# 0, _item_index];
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_create_notification (line 7)
called from - gml_Object_obj_item_Step_0 (line 57) -              if instance_exists(create_notification(1, in)){;
Code:
///create_notification script
///description create_notification(number,item_index)
/// @arg number
/// @arg item_index

var _num = argument0;
var _item_index = argument1;
var _item_name = inventory.ds_items_info[# 0, _item_index];

if(!instance_exists(obj_notification)) {
    instance_create_layer(0, 0, "Instances", obj_notification);
}

with (obj_notification) {
  
    //create grid
    if(!ds_exists(ds_notifications, ds_type_grid)) {
        ds_notifications = ds_grid_create(2, 1);
        var _notif_grid = ds_notifications;
        _notif_grid[# 0, 0] = _num;
        _notif_grid[# 1, 0] = _item_name;
    }
      
        //add to grid
    else {
        var _notif_grid =ds_notifications;
        var _grid_height = ds_grid_height(_notif_grid);
        var _found = false;
      
        for (var _yy = 0; _yy < _grid_height; _yy++) {
            if (_item_name == _notif_grid[# 1, _yy]) {
                _notif_grid[# 0, _yy] += _num;
                _found = true;
                break;
            }
        }
         if (!_found) {
             ds_grid_resize(_notif_grid, 2, _grid_height + 1);
            _notif_grid[# 0, _grid_height] = _num;
            _notif_grid[# 1, _grid_height] = _item_name;
         }
    }
  
    //reset the fade away
    event_perform(ev_other, ev_user0);
    }
Code:
 ///Obj_item step event
var picked_up = true;
if(drop_move){
    x = lerp(x, goal_x, 0.1);
    y = lerp(y,goal_y, 0.1);
    if(abs(x- goal_x) < 1 and abs(y- goal_y) < 1) { drop_move = false; }
}
  else {
      if(!keyboard_check(ord ("O"))) exit;
      var px = global.PlayerID.x;
      var py = global.PlayerID.y;
      var r = 32;
    if(point_in_rectangle(px, py, x-r, y-r, x+r, y+r)){
       
        //Are we on top of the player
        r = 2;
        if(! point_in_rectangle(px, py, x-r, y-r, x+r, y+r)){
             //move towards the player for pickup
            x = lerp (x, px, 0.1);
            y = lerp(y, py,0.1);
           
        }else { // pickup item
            var in = item_num;
           
            with(inventory){
                var ds_inv = ds_inventory;
               
               
            //check if item exists in inventory already  
            var yy = 0; repeat(inv_slots){
                if(ds_inv[# 0, yy] == in){
                ds_inv[# 1, yy] += 1;
                picked_up = true;
                break;
                } else {
                    yy += 1;
                }
            }
            //otherwise add item to an empty slot if there is one
            if(!picked_up) {
                yy = 0; repeat(inv_slots){
                if(ds_inv[# 0, yy] == item.noone){
                ds_inv[# 0, yy] = in;
                ds_inv[# 1, yy] += 1;
                picked_up = true;
                break;
                } else {
                    yy += 1;
                   }
                }
              }
 
   
          }
                 //destroy Item if picked_up
           if(picked_up){
             
              create_notification(1, in);
               instance_destroy();
               show_debug_message("picked up an item.");
          }
   
        }
    }
  }
 

TsukaYuriko

šŸŒ 
Forum Staff
Moderator
There is no instance of inventory when that code runs - which means you either didn't create it yet, destroyed it or deactivated it.
 
How can you find out ??? If I had to guess I would say it might have got destroyed because I got a inventory object I'll post the code below.
Code:
 /// inventory step event 
if(keyboard_check_pressed(ord("I")))
  switch(global.show_inventory){
      
case global.show_inventory: 
  instance_activate_all();
  global.show_inventory = false;   
    instance_destroy();

break;

  }
 if(!global.show_inventory) exit;
 #region Mouse Slot
 mousex = device_mouse_x_to_gui(0);
 mousey = device_mouse_y_to_gui(0);

var cell_xbuff = (cell_size+x_buffer)*scale;
var cell_ybuff = (cell_size+y_buffer)*scale;

 var i_mousex = mousex - slot_x;
 var i_mousey = mousey - slot_y;

 var nx = i_mousex div cell_xbuff;
 var ny = i_mousey div cell_ybuff;
  if(nx >= 0 and nx < inv_slots_width and ny >= 0 and ny < inv_slots_height){
      var sx = i_mousex - (nx*cell_xbuff);
      var sy = i_mousey - (ny*cell_xbuff);
      
if((sx < cell_size*scale) and (sy < cell_size*scale)){
        m_slotx = nx;
        m_sloty = ny;
     }
  }
 //set selected slot to mouse position
 selected_slot = min(inv_slots-1, m_slotx + (m_sloty*inv_slots_width));
 #endregion
 
 //pickup item
 
 var inv_grid = ds_inventory;
 var ss_item = inv_grid[# 0, selected_slot];
 
 if(pickup_slot != -1){
    if(mouse_check_button_released(mb_left)){
        if(ss_item == item.noone){
            inv_grid[# 0, selected_slot] = inv_grid[# 0, pickup_slot];
            inv_grid[# 1, selected_slot] = inv_grid[# 1, pickup_slot];
            
            inv_grid[# 0, pickup_slot] = item.noone;
            inv_grid[# 1, pickup_slot] = 0;
            
              pickup_slot = -1;
            
        } else if (ss_item == inv_grid[# 0, pickup_slot]) {
            if(selected_slot != pickup_slot){
                inv_grid[# 1, selected_slot] += inv_grid[# 1, pickup_slot];
            inv_grid[# 0, pickup_slot] = item.noone;
            inv_grid[# 1, pickup_slot] = 0;
        }
               pickup_slot = -1
        } else {
            var ss_item_num = inv_grid[# 1, selected_slot];
            inv_grid[# 0, selected_slot] = inv_grid[# 0, pickup_slot];
            inv_grid[# 1, selected_slot] = inv_grid[# 1, pickup_slot];
            
            inv_grid[# 0, pickup_slot] = ss_item;
            inv_grid[# 1, pickup_slot] = ss_item_num;
            
              pickup_slot = -1;
        }
    }
 }
else if(ss_item != item.noone){
    //Drop item into Game world
    if(mouse_check_button_pressed(mb_right)){
        inv_grid[# 1, selected_slot] -= 1;
        //destroy item in inventory if it was the last one
        if(inv_grid[# 1, selected_slot] == 0){
            inv_grid[# 0, selected_slot] = item.noone;
            }
            //create the item
    create_notification(-1, ss_item);
        var inst = instance_create_layer(global.PlayerID.x, global.PlayerID.y, "Instances", obj_item);
            with(inst){
                item_index = ss_item;
                x_frame = item_index mod (spr_width/cell_size);
                y_frame = item_index div (spr_width/cell_size);
        }
           show_debug_message("Dropped an item.");
            
    }
    //Drop pickup item into new slot
     if(mouse_check_button_pressed(mb_left)){
         pickup_slot = selected_slot;
     }
 }
 

TsukaYuriko

šŸŒ 
Forum Staff
Moderator
instance_exists checks whether an instance exists. You can as well add debug messages next to everything that creates or destroys instances, so you can check the debug console for any occasions where something got destroyed or created.

I usually make wrapper scripts for these functions which do both - logging and creation - so I don't have to write two lines whenever I want to create or destroy something, but also have logging bundled with it right away, ideally with a toggle so I can turn the messages on and off depending on whether I want to test creation/destruction/activation issues or not.
 
Ok I did a instance_exists for the obj_item step event and get the error there just don't know how to fix it I'll put the code below.
Code:
 ///obj_item step event
if(picked_up){
               if(!instance_exists(inventory)) {
    instance_create_layer(0, 0, "Instances", inventory);
              create_notification(1, in);
               instance_destroy();
               show_debug_message("picked up an item.");
Code:
 /// error message
Resizing swap chain...Resizing swap chain...Dropped an item.
Dropped an item.
Dropped an item.
Grid 4, index out of bounds writing [0,-1] - size is [2,10]
picked up an item.
Resizing swap chain...Resizing window...Attempting to set gamepadcount to 0
Not shutting down steam as it is not initialised
 
Top