I’m trying to make it so I can drop items I’m holding, and have them taken from the inventory. I’m running into some trouble though. I keep getting the error,
Invalid access to property or key 'item_grids' on a base object of type 'previously freed'.
The error is happening on the for loop of this function,
#Removing the item from the players inventory if the take_item_check function has use set to true
func take_item(used_item):
for grid in used_item.item_grids:
var grid_to_check = used_item.grid_anchor.slot_ID + grid[0] + grid[1] * col_count # use grid anchor instead of current slot to prevent bug
grid_array[grid_to_check].state = grid_array[grid_to_check].States.FREE
grid_array[grid_to_check].item_stored = null
grid_array[grid_to_check].stored_item_ID = null
used_item.queue_free()
Here is the code that’s trying to run that function:
if Input.is_action_pressed("throw"):
if equippedItem != Items.NULL:
inventory.take_item(inventory.equippedItem)
Sorry if there isn’t enough information, I can send through any extra code snippets needed, the inventory system is pretty big, so I thought I’d only send through what’s necessary.
Thanks for your help!
It is a bit complicated to know what is going on with the given elements, I would simply debug the for loop by adding a debug point at the start of your for loop to check what values your variables have and on which iteration you are getting the error. Pay close attention to your used_item variable value since it seems to be the one getting freed in one instance of the loop.
It is also possible that by setting some values to null, you are unwillingly freeing the ressources. In godot, if an object is not referenced by anything, godot will delete it automatically to save memory space (which makes sense since if you have no reference to the object, you can’t use it).
I set a break point, and used_item is set to Object ID: 159350000278, which means it isn’t being set to null. Are you able to help me understand what the specific error code I got means?
Thinking about it, the error is quite weird, Godot usually tells that an object is null, not type ‘previously freed’. This seem to be a custom type of yours. If you click on the object assigned to used_item and check its properties (left panel), do you see an ‘item_grids’ property ?
Or maybe this line frees the object and changes its type while in the loop :
Yeah, the item does have an item_grids property. It’s a grid based inventory system, I’m sorry I can’t show all of the code, it’s a lot of lines of code at this point. As for the line you showed. All that does is sets each grid state to free, which means items can be placed on it, but it isn’t dealing with the actually item scene itself.
Otherwise it’s running your code every frame as long as the action is pressed, meaning it will try to free your item multiple times.
You might also want to explicitly change your equippedItem reference after you’ve used and freed the item, to avoid any unexpected behavior.