-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a casting option for @onready variables generated by dragging and dropping nodes when the editor setting "text_editor/completion/add_type_hints" is true. #9966
Comments
See:
|
Hmm. I never thought about the error detection being better with form 1, but yeah it does sound better. This could be changed to not remove the type hints, but then variable declarations would become obnoxiously long. Is the possibility of marking form 1 declarations as "safe" realistic? It's just really annoying that the engine provides such a tool but not a way to make it happy in every context, specially when the tool gives the impression that making it happy is the the intended way to write better code (particularly in the case of people less experienced with coding). |
We can't make everybody happy, not even Godot's editor o,.,o Godot is highlighting the lines where the types can't lead to a runtime error. On one hand, we don't want runtime errors. On the other hand, errors are useful. So, I think it would be better if in this case it wasn't a runtime error. For example we can imagine Godot checking if the type is correct (for any form of the I believe in that case considering both safe could be more palatable. And yes, we can make the argument that these are different kinds of safety. On a similar note, remember that both lines would fail if the Meanwhile, have you considered using |
Taking into account the fact that nodes might or might not be there when the script expects it now makes me see how messy something like this could be. As for |
It is possible to have them collapse with an export group. It is also possible to hide them dynamically, for example in I have to accept lines that are not marked as type safe anyway (e.g. when using a The reason I've been moving to Addendum: If we could annotate that a script is for a scene and only that scene, so that when we instantiate the script we get an instance of the scene (there was a proposal for that), then the static analysis could benefit from that too. |
The problem is a misunderstanding of the tool, not how it works in this case. We can't make a tool that can reliably determine whether your code is good or not. So I don't think this is a good idea. This will only make things even more confusing if we try to highlight better code rather than type-safe code. But I understand how it can be annoying for a perfectionist.
This is why we added a note to the documentation. I think that the problem is that the
If we introduced new cast operators (since # Don't hides an error in debug builds.
@onready var label := static_cast[Label]($Label) # Safe line.
func _on_body_entered(body: Node2D) -> void:
if body is Player:
# The type is already checked at runtime.
var player := static_cast[Player](body) # Safe line.
...
# The type is not yet checked at runtime.
var enemy := dynamic_cast[Enemy](body) # Safe line.
if enemy: # No error above, but you should check that it is not `null`.
... Also, if we improve the static analysis of local variables in the future, then the following code will also be considered safe: if body is Player:
var player: Player = body # Safe line at this point, no unnecessary runtime code.
... |
Describe the project you are working on
A third person shooter with lots of
@onready
variables used on scripts.Describe the problem or limitation you are having in your project
Variables declared as
@onready var foo: Node = $Foo
don't satisfy Godot's Safe Lines tool, and will be marked as "unsafe".Declaring them as
@onready var foo := $Foo as Node
is enough to make the tool happy.I try to reduce "unsafe" lines as much as possible, and when you have a lot of nodes that you want to make into variables it becomes cumbersome to rewrite them all from form 1 to form 2.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
Add a setting to EditorSettings called "text_editor/completion/cast_dropped_nodes" (or a better name if anyone thinks about one) to enable variables to be generated using type casting.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
_initial_set("text_editor/completion/cast_dropped_nodes", false);
to the //Completion section [line 664].ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from)
[line 1817] addconst bool use_cast = EDITOR_GET("text_editor/completion/cast_dropped_nodes");
underif (use_type) {
[line 1940].text_to_drop += vformat("@onready var %s: %s = %c%s\n", variable_name, class_name, is_unique ? '%' : '$', path);
to:
End Result:
editor_settings.cpp's changes would end up looking like this:
script_text_editor.cpp's changes would end up looking like this:
EditorSettings.xml's changes would end up looking like this:
If this enhancement will not be used often, can it be worked around with a few lines of script?
In theory one could write a script that
@onready
variables that include type hints and no casting.This still needs to be run either every time a drag-and-drop variable is created, or manually by the user at their own discretion. The performance of the script would also rely on the length of the target script.
Is there a reason why this should be core and not an add-on in the asset library?
The drag-and-drop variable generation functionality and the string template used for it seem to be hard coded into the engine's source code. I don't see another way of doing it without making bigger changes to the relevant code.
The text was updated successfully, but these errors were encountered: