This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

What happens is that I'm instancing a node as a child of another one through code using add_child. This is part of my inventory system, as the instanced node appears when getting an item. In order to instance the node I'm using the following function:

func ObjectToInventory(slot, item_name, texture_to_load):
    slot.add_child(InventoryItemInstance)
    slot.get_node("Item1").name = item_name
    slot.get_node(item_name).ItemTexture = load(texture_to_load)
    slot.get_node(item_name).texture_normal = load(texture_to_load)

ObjectToInventory(item_slots, "LEDTubeLight", "res://Textures/UI/InventoryImages/LEDTubeLight.png")

(InventoryItemInstance is the node I want to instance, which was defined before:

var InventoryItemScene = preload("res://Scenes/Assets/UI/InventoryItem.tscn")
var InventoryItemInstance = InventoryItemScene.instance()

This part of the code works perfectly, the problem is that the node is a button, and it doesn't seem to work when instancing it through code. The issue is not from the node itself, as I tried adding it through the editor, and it worked.

I also checked the remote tab, and the node was instanced correctly.
Before getting the item:
enter image description here

After getting the item:
enter image description here

I used isinstancevalid() to check if the node was instanced correctly, by printing something if it's valid. When I add the node from the editor, the instance is valid, but when it's added through code, it doesn't seem to be, as nothing is printed.

Any help would be appreciated

Godot version 3.5 stable
in Engine by (37 points)

Try to use the static-typing and activate all the warnings (Project Settings>Debug>GDscript>Warnings).
See if you get some warnings about sub-class using.

3 Answers

0 votes
Best answer

I figured out the solution, I only had to put a variable to the button texture inside of physics process (delta) instead of at the beginning.
Instead of doing

onready var LEDTubeLight = item_slots-get_node("LEDTubeLight)

I had to do:

func _physics_process (delta):
     var LEDTubeLight = item_slots-get_node("LEDTubeLight)
by (37 points)
selected by
0 votes

Maybe just add the initial instance in the editor, hidden. Then, when you need to instance the node, just duplicate the initial instance and make it visible. I dunno if I understood your dilemma... but I know this may offer you a work around.

by (214 points)

This suggestion usually works for me

I thought of doing at first, but I need to check how many children there are in the node to set a maximum amount of items to the inventory.

Will it work if I create the nodes separately but put them in another node as invisible? If so, how can I move those nodes to the actual inventory slots? For instance, if I store those invisible nodes as children of another node called InventoryItemQueue, how can I move them to another node called ItemSlots?

This post explains how to reparent a node via scripting. If you store invisible children, you may want to store them as children of a parent of type Node, and make sure when you reparent the children to make them visible

+1 vote

You predefined both class and instance. Because of this, your Objecttoinventoryfunction can only work once, as it adds child of this first predefined instance. Every time after that, it should throw an error, that this instance is already childed.
All You need to do is to instantiate object inside a `Objecttoinventory as a first line, and remove predefined instance.

by (8,188 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.