Godot 4.0.1 stable - GDScript autocomplete for parent classes' properties and methods not working
Godot version
4.0.1 stable (cacf49999)
System information
Windows 10 x64 22H2
Issue description
GDscript autocomplete for parent classes' inherited properties does not work. Not sure if it's a global problem, or just specific to the one I've found (completely new to Godot, actually was just following through the 2D tutorial).
Steps to reproduce
- Create a
Path2D(named MobPath) and then aPathFollow2Das a child (named MobSpawnLocation) - Refer to it in code either via
$MobPath/MobSpawnLocationorget_node("MobPath/MobSpawnLocation")(doesn't matter) - Try to find the
positionorrotationproperties of this object - autocomplete does not offer it up.
Both of these properties are inherited from the Node2D parent class.
Tried finding any other inherited property or class coming from Node2D, not working.
Tried finding any other inherited property or class coming from other parent classes, not working either.
Example: part of the script from the tutorial
func _on_mob_timer_timeout():
var mob = mob_scene.instantiate()
var mob_spawn_location = $MobPath/MobSpawnLocation
mob_spawn_location.progress_ratio = randf()
var direction = mob_spawn_location.rotation + PI/2 # .rotation is not found via autocomplete
mob.position = mob_spawn_location.position # .position is not found at all via autocomplete
direction += randf_range(-PI / 4, PI / 4)
mob.rotation = direction
var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
mob.linear_velocity = velocity.rotated(direction)
add_child(mob)
Minimal reproduction project
N/A
Both of these properties are inherited from the Node2D parent class.
The result of $Path/To/Node is a Node, which does not necessarily have a position or rotation property. For instance, bare Nodes don't have any concept of a position.
You need to add type hints to get working autocompletion:
var mob_spawn_location: Node2D = $MobPath/MobSpawnLocation
I'm not sure I follow you.. The MobSpawnLocation object is an instance of the PathFollow2D class, and according to Godot Docs, this "Inherits: Node2D < CanvasItem < Node < Object".
Shouldn't inheritance work in a way that anything which is a public property of any of your classes' parents, is by default an accessible property of your class, unless it's specifically made private in your class? But then if it was made private, then accessing those properties should not work at all - but it does work.
The function get_node returns a Node so it doesn't have hints for things like Node2D, this has nothing to do with hidden properties, it's all to do with that the engine doesn't know what type that node is, because it can change
Fair enough - I've added the type hint into the editor, yet autocomplete is not working:

Even tried it with Node2D as type hint - not working:

Does it work if you use var mob_spawn_location := $MobPath/MobSpawnLocation as PathFollow2D (notice the := instead of =)?
Yes, it does!

Can't reproduce™
Make sure the scene using your script is currently opened. Path-based autocompletion works based on the current scene.
I believe the scene is opened, although I'm not sure what this would mean exactly. You can see to the left under Scene, that Main is selected, MobPath/SpawnLocation is also visible. I've rewritten the code to be the first variant but with type hinting, where the autocomplete does not work.

I've tested and can reproduce. Here is a minimal reproduction code
func _ready():
var child = $Node2D
rotation = child.rotation # rotation won't autocomplete here
To reproduce you need both to store the node in a variable and to try to autocomplete in an assignement
I have noticed similar behavior with autocomplete not working, even with type hints, though having nothing to do with parent classes (as noted in the title). The following GIF is from version v4.1.2.stable.official [399c9dc39] and the level variable is a PackedScene @export variable.
As far as I understand it, the type hint of : TileMap should be making tilemap into type TileMap with appropriate autocomplete. However, only adding as TileMap gets the autocomplete working.
For the problem with type hints not working see #74888.
The unique problem here is what was described by ajreckof.
The problem is with this code.
Using Node.next is not a reliable way to get the last assigned value of a variable. In comparison _guess_identifier_type has to loop through the suit to get the last assigned value.
IMHO we should just remove _get_subscript_type and see that this behavior is integrated into the whole _guess_expression_type chain of calls.
I'm new using godot and following the 2d tutorial. If it helps: I'm running into the same thing here https://docs.godotengine.org/en/latest/getting_started/first_2d_game/05.the_main_game_scene.html when writing the _on_mob_timer_timeout function.
context:
- I have set Editor settings - Text Editor - Completion - Add type hints = On.
main.gd
extends Node
@export var mob_scene: PackedScene
func _on_mob_timer_timeout() -> void:
var mob = mob_scene.instantiate()
# ...
mob_scene is set to the Mob scene, which is a RigidBody2D which is a descendant of Node2D amongst others.
But mob.position or mob.rotation do not autocomplete.
When I add a class definition to mob.gd like so: class_name Mob, I can type cast with as Mob and then it works:
var mob = mob_scene.instantiate() as Mob
But it doesn't make much sens to me to type cast, since it's not a type-safe language to start with.
It would be really nice if it would just infer all ancestor members automatically.