Why do people use `$"."`?

Hey everyone,

As you may know, with Node-dervied classes, $"." points to the current node.
That means $"." == self and those three calls are equivalent :

method_call()
self.method_call()
$".".method_call()

Are there any benefits when using $"."?

It happens when one drags and drops the scripted node into it’s own script. It’s likely worse than using self.

5 Likes

I never saw anyone use it.

Note that self and $"." are only equivalent for nodes. For objects that don’t inherit node classes, using $"." make no sense and won’t work, but they still can use self.

4 Likes

You should always use the first one IMO. There are never namespace collisions in GDScript that require you to use self. And you should never use $ references in code, they should always be assigned to an @onready variable. So using @onready var myself = $"." becomes a bit ridiculous because now you’re referring to a variable which you already have a representation of, and you don’t need.

3 Likes

Maybe it was a tutorial, but i never use that method either.

I have just seen a post with this exact initializer !

I also sometimes stumble upon scripts filled with $"." and I alaways wondered why.

Now about the usage of self, I guess that depends on preferences and code style guidelines !
Personally I prefer to avoid using self as much as I can, but I have seen teams enforcing its usage in the past.

1 Like

Using self to access properties is a silly stylistic choice imo. The only sensible use is when you need to pass a reference to somebody else.

Because they are coming from another language, or learned that habit from someone else. It’s a bad habit.

What @normalized said. Like I said before, it’s used for namespace conflicts, but that’s not an issue in GDScript. The only time you should use self is when passing yourself off to another function, either with a Signal or in use of a recursive search of all nodes (like in an import script).

Otherwise, no offense, it’s a sign that you do not know how to read scope in code and are a newbie. If I had a developer check in code that used self their PR would be rejected with a comment to remove all references to self because it’s redundant. It’s like RAS Syndrome and saying ATM Machine (Automatic Teller Machine Machine).

And before you tell me that you’ve seen senior developers do that, you should know my job is to go into companies and teach senior developers how to be better at their jobs.

It is insane that this is a person who donates their time to help people solve lil problems with a game engine.

1 Like

We all have hobbies. :slight_smile: And people taught me. I’m just paying it forward.

4 Likes

Never knew this method existed. The more you know.

1 Like

There a lot of things that GDScript lets you do that aren’t the best habits. Part of it I think is keeping things backwards compatible. Others, like AnimatedSprite2D are created to be better versions of things like Sprite2D that allows animations, and then you get questions about which to use. The fact that you can use node notation to call yourself makes sense from a technical implementation point of view, but just because you can, doesn’t mean you should.

1 Like

The animation tree has access to dictionary properties using the square brackets notation…

anim_tree["parameters/playback"].travel("state_name")

if the script is on the animation tree the ‘self’ keyword seems appropriate

self["parameters/playback"]

If the node is a derived type then ‘self’ is useful to quickly access the hidden variable names using the intellisense/ auto complete.

The dollar notation points to child nodes with

$"./NodeName" 

And thats maybe just good practice to keep the notation standard consistent throughout.

Also, as was said above, the node appears like ` $"." if dragged in from the node tree.

1 Like

Good question. Earlier I saw: $“.”.global_position = ...