godotgdscript

What's the difference between $ and % in gdscript?


I've seen people use $node and %node to get nodes but i have no clue what the difference is. What's the difference?

I haven't found anything online and they seem to do the same thing, but I'm pretty sure they're different.


Solution

  • You find the answer in the official docs:

    There are also two constructs that look like literals, but actually are not:

    $NodePath -> Shorthand for get_node("NodePath")

    %UniqueNode -> Shorthand for get_node("%UniqueNode")

    which essentially means: If you have not made the node unique and you use $ with the path to the child node. So if we assume the following tree:

    Main <- here lies our script
      UI
        Container
          Target
       
    

    you can get the target node by using $UI/Container/Target. If you made the Target node unique you also can use %Target instead.

    If you don't know what a unique node is: You can mark a node as "unique" inside the editor. This will give it a unique name. With it you can access the node from your scripts without needing to know its full/relative path. The full explanation is also in the docs here.