How to locate a texturebutton?

I’m VERY new to godot and I don’t know how to locate a button or a texturebutton in a script.

If you can put the example in the comments PLEASE do! And if want to help a bit more I want a script to widen the texturebutton

Thanks!

what do you mean by locate?

Do you already have a scene with a fixed position where the button is, and you only want to reference it so you can access it in the script?

Or do you want to find the texturebutton / button node dynamically, e.g. if you don’t know the node path beforehand, and access it in the script?

The first option is the more common one, and more simple because you know the relative position beforehand. There are a few ways to just reference a node in the script - these are the usual options:

  • $"../path/to/node" in the code, which is the same as get_node("../path/to/node")
  • an @onready variable → ctrl + drag & drop the node into the script.
    cleaner than the above, because you only call the get_node function once and have better control over where it is used
  • an @export variable → alt + drag & drop the node into the script. It will show a field in the inspector in the editor where you can drag&drop your node
  • scene unique nodes accessed with %uniquename
  • retrieving the node from a group, e.g. via get_nodes_in_group

My recommendation is to use export since you can reassign it in the editor and it does not unlink if you move nodes in the tree (it does unlink if you rename the exported variable in the script, though).

In case of the second option, this may be more complicated, because you might need to traverse the tree. In this case get_tree(), get_parent(), get_children() and especially find_child() / find_children() are helpful
You can often get around this case with a good architecture.