Hi there, right now I'm reworking my game's inventory system and I've discovered that apparently when you queue_free() a node to get rid of it, it also deletes all references to it.

I've already figured out how to put a node into a variable, which was easy, but once I do queue_free() to remove it from the scene, the reference becomes a <Freed Object> and is basically treated like null.

Is there another way to handle this type of situation?

  • HavenHuski queue_free() will destroy the node, which is probably not what you want. If you need to transfer the node from the map (for example) to the inventory, simply re-parent it to inventory node/scene. If you want to remove it from the scene entirely but keep it alive and add it again at a later time you can use remove_child() and add_child().

It doesn't make sense to store a reference to a node that no longer exists.

Are you trying to store one or more of the node's properties?

    DaveTheCoder

    So whenever the player presses the F key while looking at a node with the is_lootable property set to true, the node gets added to the player's inventory, represented as a dictionary with various key value pairs to serve different purposes, since my inventory will be slot-based.

    After this happens, and I can use all its properties and variables in other scripts. The thing is I still need to delete the node from the scene once it's collected, and the only way I've seen to remove a node from a scene is queue_clear(), which as I mentioned, makes it so the node that I had just put into my inventory is now a <Freed Object>, and I can't access the node in other scripts anymore

    Does this make sense?

      I'm going to try something else and post what happens here

      HavenHuski I still need to delete the node from the scene once it's collected

      Do you need to delete the node, or only prevent it from being displayed? You can prevent it from being displayed by calling hide(), or by removing it from the scene tree with remove_child().

      HavenHuski

      If you need to keep the node around, consider hiding it and moving it to wherever it needs to be in the UI.

      If you only need some data about the item for your inventory (we don't store every gold coin individually) then handle the data side of the UI on pickup and delete the node.

      If you later need a visible item, instantiate new nodes from the data side of your inventory when you open the inventory screen (or drop the item or whatever)

      HavenHuski queue_free() will destroy the node, which is probably not what you want. If you need to transfer the node from the map (for example) to the inventory, simply re-parent it to inventory node/scene. If you want to remove it from the scene entirely but keep it alive and add it again at a later time you can use remove_child() and add_child().

        xyz

        Seems to be working, I'll have to rework my inventory but that won't be a problem for me. Thanks 🙂

        HavenHuski the node gets added to the player's inventory, represented as a dictionary with various key value pairs to serve different purposes, since my inventory will be slot-based.

        Are you creating the Dictionary as a variable within the node's script? If so, and you want to retain the Dictionary after the node is deleted, you might need to call duplicate() to make a copy of the Dictionary. I would think that GDScript's reference counting would make that unnecessary, but I don't know exactly what your code is doing.

        @"HavenHuski"#p114067 remove_child() does nothing

        It removes the node from the tree. You need to call it on the parent as the name suggests, not on the node you're removing.

        In typical inventory way, you get the variables you need from the 3D object on the ground, and create a UI element that has all these variables, then when the UI element is created, you delete the 3D object. The process is the same the other way around.

        You can do that with a custom_class.
        CustomClass that holds the variables

        extends Node
        class_name ItemVariables
        var item_name : string
        var item_icon : Texture2D
        var item_mesh : MeshInstance3D (or a PackedScene)
        etc

        3D Object

        var itemvariables : ItemVariables #the variable that holds your ItemVariables
        
        func create_3D_object(_itemvariables : ItemVariables, ui_element_reference):
              itemvariables = _itemvariables
              #assign whatever you need to assign here, for the mesh,
              ui_element_reference.queue_free()

        UI Element / inventory slot
        In the create ui_element, you can retrive those variables and create whatever you need (visual stuff)

        var itemvariables : itemVariables #the variable that holds your ItemVariables
        
        func create_ui_element(_itemvariables : ItemVariables, 3D_object_reference):
                itemvariables = _itemvariables
                #assign whatever you need to assign here, for xeample texture, amount, etc. 
                3D_object_reference.queue_free()