Godot Version
v4.4
Question
Does anyone know why my @onready variables here are not type-safe? I specify a node type after each colon, yet the line numbers are still greyed out. GDScript is not happy for some reason.
v4.4
Does anyone know why my @onready variables here are not type-safe? I specify a node type after each colon, yet the line numbers are still greyed out. GDScript is not happy for some reason.
Well, because it’s not type safe. Node.get_node()
returns a Node
and you are telling the gdscript compiler that it’s a Node3D
. Because the compiler can’t know at compile time that the get_node()
result is a Node3D
it can’t mark the line as type safe.
The same happens if you do something like:
The compiler can’t know for sure that the right hand side is a String
it just knows that the left hand side is a String
because I told them it was.
If I give a more concise type to a
then:
It know knows that the right hand side is a String
so it marks it as type safe.
Back to your question you could force it to be type safe by adding as Node3D
like:
And will mark it as type safe because you are telling the compiler that the right hand side is a Node3D
(but if you lie to it it may crash at runtime)
Thank you. How concerned should I be about type safety? Should I try to make every line of code type safe?
Check this documentation page Static typing in GDScript — Godot Engine (stable) documentation in English where it explains the pros of type safety.
At this moment I’d say it’s not necessary. Try to type your code correctly but, if a line is not green and does not cause problems, don’t obsess over it.