Why use onready variables here?

It seems like we should just be able to go to our crash_sequence and complete_level functions and add a line like $ExplosionSFX.play().

Why go through the trouble of setting up the onready variable?

You can! And if your script is for an Autoload, you pretty much have to do it this way because Autoloads are incompatible with _ready().

One important use for @onready references I noticed is that if you change the node’s location in the scene tree (thereby changing its nodepath), you would have to go through and update every explicit use of that nodepath in your code. You still have to do that with an @onready reference, but now it’s just one line instead of n.

Needless to say, @onready references also shorten your code lines when the nodepath is longer than one level of depth (which it often will be). They also give you an opportunity to give a different name to it within this script, which can sometimes be useful when making a reference to something this node inherited.

Oh yeah. And, it’s also nice not to have to write a $ or % in front of everything =)

You can absolutely use explicit nodepaths whenever you want to, but more often than not, the @onready reference simply works a little better, especially as the project gets more complex.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms