The Godot Community Forums are back up and running! Please read the the announcement post for important information on what happened. Thanks!

Just a quick tip for finding nodes

UnknownUserUnknownUser Posts: 0
edited September 16 in Programming

were all familiar with:

get_tree().get_root().get_node("rootNodeName")

but I completely forgot we have an autoload where we can all all sort of method or access variables, so all I did was add a return function call findNodeByName that takes 2 string the current scene and the node you want to find

func findNodeByName(currentScene, nodeName):
    return get_tree().get_root().get_node(currentScene +"/"+ nodeName)

keep in mind that the root node of the scene has to be the same or you could just use the rootNode instead(up to you really)

then just call it assuming the script is called Globals

var someNode = Globals.findNodeByName(get_tree().current_scene.name, "NameOfNode")

you could shorten it even further by getting the scene name and storing it in a variable in Globals

Comments

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file