How to get a component of a node?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Andre Mruiz

I wanna get a component, variable or whatever it is called of a node from a script of another node.
Because I have a script on a RichTextLabel node and I have a Timer also, what I wanna do is get the “Wait Time” component of the Timer and change it in the script of the RichTextLabel.

I did this but it just doesn´t work:

func _on_Timer_timeout():
    if event.presed and event.scancode == KEY_KP_2 or KEY_K:
        get_node("Cronómetro").get("Wait Time")
:bust_in_silhouette: Reply From: kidscancode

Note: I edited your post to fix the code formatting. In future please try and format your code samples so they’re easier to read.

It’s hard to tell exactly how to fix your problem without knowing your tree structure, but I suspect it’s due to having the node path wrong. The path you use in get_node() is relative to the current node. get_node("Cronómetro") only works if “Cronómetro” is the child of the current node. If it’s a sibling, for example, it would be “…/Cronómetro”. See this tutorial for more info:

Next, it’s not clear what you want to do with the wait time. You can access a node’s property directly by its name. For example:


would print the property. A list of a particular node type’s properties, or “member variables” can be found in the docs. See Timer, for example.

thanks for the advice! :slight_smile:

Andre Mruiz | 2018-12-16 22:25