Find file route by string

Godot Version

v4.2.1.stable.official [b09f793f5]

Question

attempting to search for the file path by feeding it a string but it errors out into
Invalid get index ‘show’ (on base: ‘null instance’).
it works when i directly give it the sprite to show so its an issue with this line, not sure how to fix since im new to godot

get_tree().get_root().get_node(String(AlwaysLoads.P1Character)).show

What exactly is “AlwaysLoads.P1Character”? And is “show” a method or a variable?

its the name of a file as a string and show is a method

There are some possible errors here.

One is that if you want to call the method in your node, you need to add the () to invoke it. Eg: .show().

The other is related to why it not finding your node in the scene tree.

Invalid get index ‘show’ (on base: ‘null instance’).

This means that your node basically doesn’t exists. Try to run your game, go to the Remote scene tab and check if the path to your node is really like you are trying. Maybe it can be inside another node and you will need to pass the whole path of it.

Also, if your AlwaysLoads.P1Character variable is already a string, you don’t need to use the String constructor on it.

Here is one example that worked for me with the same structure you are trying:

const TEST_NAME: String = "Slime"

func _ready() -> void:
	var slime = get_tree().get_root().get_node("Game").get_node(TEST_NAME)
	slime.test()


PS: Your AlwaysLoads.P1Character needs to be the node name, not the file name.

thank you :+1:

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