Array loosing data

Godot Version



I’m creating an inventory. When I pick up an item, I add it to an Inventory node with an array called items. With a breakpoint, I can check that the array has the element added. After resuming the game, if I check the remote section and look for the array, it is empty again.

The array is currently in a global script but I started with the inventory node instantiated in the same class and the result was the same. The global script is empty, exept for the inventory definition

extends Node

@onready var inventory: Inventory =

The inventory class is empty exept for the array definition and a _init function:

extends Node

class_name Inventory

var items: Array[Item] = []

func _init(slots: int):
	print("inventory size")

When godot makes a “global script” it creates a new instance and adds it to the tree. If you want to keep data you set in the editor, you can’t just use the global script mechanism as you need an instance to see the array on the editor. What you could do, however, is make that array a Resource. It is more boilerplate, but gives you a chance to set the defaults. Might not be what you need, tho. There are many ways to do this.

The problem is that I lose the info on runtime. I add the item to the array and inmediatly dissapear. The global script is only one try to solve the issue i dont actually needed right now

Why is this @onready?
Is Inventory a node?
We need to see the node tree.
I have a suspicion that somehow more than 1 instance of Inventory is created.

I removed the @onready, wasnt needed anymore. I checked the ID of the PlayerGlobal and from the Inventory objects and both keeps the same ID every time.

In this screen I stop the game with a breakpoint aftear I walk over the berry. If I resume the game and pick another fruit the array is empty again, but the IDs of PlayerGlobal and Inventory remains the same.

Inventory extends from node, in the original post there is the whole code. But is not a scene in the game tree I use it like a class to define a var on the Playersglobal Script with Inventory type.

You stop the game with a breakpoint and the berry is for sure in the inventory. It is an element of items.
If so then the item is subsequently removed by some code or there is more than 1 inventory instance and the item is added to one that you don’t readily see.

Just to be clear… the class Inventory is NOT an autoload, correct?

Is it PlayerGlobal.inventory that subsequently is missing the item or is it slot?

How are you checking to see if the item is not there?
I would add a second berry and check at your existing breakpoint to see if the first berry is still there.

I finally figured out where the problem was.

The assignment was by reference an not by value. So when the item is beign deleted to remove it from the map is also removed from the array.

the solution was simply add a call to duplicate()
before: inventory.items[i] = item
fixed: inventory.items[i] = item.duplicate()

Thanks for the help!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.