Skip to content
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

[3.x] get_node() overrides autocompletion information for statically typed variables #53318

Open
Drumstickz64 opened this issue Oct 1, 2021 · 7 comments

Comments

@Drumstickz64
Copy link

Godot version

3.3.stable

System information

OS: Windows 10

Issue description

When I provide a type hint to a node from get_node, my type hint gets overridden by get_node. this only happens in the editor. Ingame the types work as expected. Forgive me if this is the intended behavior, but is just seems odd to me.

# target should be of type Node2D, but is of type Node.
onready var target: Node2D = $Node

This becomes relevant when using get_node with NodePaths provided from the Inspector, And the editor doesn't provide correct autocompletion.

export var target_path: NodePath

# target should be of type Node2D, but is of type Node
onready var target: Node2D = get_node(target_path)



func _physics_process(delta: float) -> void:
        # target.position not suggested by autocomplete
	print(target.position)

Steps to reproduce

  1. use get_node to get a node and store it in a variable.
  2. provide a type hint to that variable.
  3. try to access any of the properties of the node. but the editor won't provide them

Minimal reproduction project

GetNodeTypeIssue.zip

@Calinou Calinou changed the title get_node overrides type hints in the editor get_node() overrides type hints in the editor Oct 1, 2021
@KoBeWi
Copy link
Member

KoBeWi commented Oct 1, 2021

Looks similar to #39228

@AnidemDex
Copy link

AnidemDex commented Oct 1, 2021

Ar not we supposed to do a kind of typecast to the node, since get_node() returns Node type?
Something like:

var node_variable: Node2D = get_node(<NodePath>) as Node2D

Edit: I suppose that when you do var : <Class_Node> = get_node(<NodePath>) it tries to use var as <Class_Node> but since its assigment doesn't promise that it'll be the type that is expecting, it uses the common base class type (can't prove this supposition, is just a theory)

@KoBeWi
Copy link
Member

KoBeWi commented Oct 1, 2021

If you provide type for variable, it should be treated like of this type, without any need for casting. If it doesn't, it's a bug (it makes type hints pointless).

@AnidemDex
Copy link

AnidemDex commented Oct 1, 2021

... it should be treated like of this type, without any need for casting. If it doesn't, it's a bug...

Then, I think it's a bug, since I have to do what I said to make the editor recognizes the type of the variable for autocompletion. This is a fragment of code of one of my projects
image
Edit: Using Godot 3.3.3 in windows 10 x64

@KoBeWi KoBeWi added bug and removed discussion labels Oct 1, 2021
@Drumstickz64
Copy link
Author

Drumstickz64 commented Oct 2, 2021

So i found some more context to this issue. This might be of use to you:

get_node() tries to provide autocompletion information, even if you're using dynamic types.
It does so (to my understanding) by looking at the node tree of the currently open scene if one of the nodes in that scene has the currently open script attached to it.

# godot autocompletes the play() function if it can resolve the path to a node in the currently open scene
$AnimationPlayer.play()

This is a nice feature for those using dynamic typing. But the weird part is that this information overrides the autocompletion information provided by static types.
Also this only effects autocompletion. All other static typing features seem to be unaffected.

export var target_path: NodePath

# target is of type node2D, but provides autocompletion suggestions for Node
onready var target: Node2D = get_node(target_path)


func _physics_process(delta: float) -> void:
        # target.position not suggested by autocomplete
        # Godot does not raise an UNSAFE_PROPERTY_ACCESS warning, which means that the type works, just not in autocompletion
	print(target.position)

        # play() method doesn't exist on Node2D, Godot raises an UNSAFE_METHOD_ACCESS warning as expected
        target.play()

@Drumstickz64 Drumstickz64 changed the title get_node() overrides type hints in the editor get_node() overrides autocompletion information for statically typed variables Oct 2, 2021
@cdemirer
Copy link
Contributor

cdemirer commented Jul 7, 2022

Seems to be the same as #42005.

@KoBeWi
Copy link
Member

KoBeWi commented Dec 13, 2022

This seems fully fixed on current master.

@KoBeWi KoBeWi added this to the 3.x milestone Dec 13, 2022
@HolonProduction HolonProduction changed the title get_node() overrides autocompletion information for statically typed variables [3.x] get_node() overrides autocompletion information for statically typed variables Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants