0 votes

I have a set of code in an autoload script whose purpose is to update the visuals of my inventory slots.
Problem is that when I change scenes the node's path changes and this code no longer works.

func _update_slot_visual(slotIndex, item_name, new_quantity):
    var slot = get_tree().root.get_node("/root/Home/UserInterfaceGroup/Inventory/InventoryContainer/Slot" + str(slotIndex))
    if slot.item != null:
        slot.item._set_item(item_name, new_quantity)
    else:
        slot._initialize_item(item_name, new_quantity)

The path /root/Home/UserInterfaceGroup/Inventory/InventoryContainer/Slot changes to /root/Level1/UserInterfaceGroup/Inventory/InventoryContainer/Slot when the scene changes.

I was hoping that changing the line to this would work, but it just comes up null:

var slot = get_tree().root.find_node("/root/*/UserInterfaceGroup/Inventory/InventoryContainer/Slot" + str(slotIndex))
Godot version 3.4 stable
in Engine by (14 points)

1 Answer

+1 vote

You can either use a Format String, or just join strings together with the + operator. For example using the plus:

var level_path = "/level8"
var correct path = "/root" + level_path + "/UserInterfaceGroup/..."

with the format string:

var format_path = "/root/%s/UserInterfaceGroup/..."
var actual_path = format_path % "level8"

In both cases you would need to pass the data of which level you are on of course.

by (418 points)

Thanks, I'll try this.

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.