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

Godot 4.0.1 stable - GDScript autocomplete for parent classes' properties and methods not working #75040

Open
zeroxx1986 opened this issue Mar 17, 2023 · 13 comments

Comments

@zeroxx1986
Copy link

zeroxx1986 commented Mar 17, 2023

Godot version

4.0.1 stable (cacf499)

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

  1. Create a Path2D (named MobPath) and then a PathFollow2D as a child (named MobSpawnLocation)
  2. Refer to it in code either via $MobPath/MobSpawnLocation or get_node("MobPath/MobSpawnLocation") (doesn't matter)
  3. Try to find the position or rotation properties 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

@zeroxx1986 zeroxx1986 changed the title Godot 4.0 stable - autocomplete for parent classes' properties and methods not working Godot 4.0 stable - GDScript autocomplete for parent classes' properties and methods not working Mar 21, 2023
@zeroxx1986 zeroxx1986 changed the title Godot 4.0 stable - GDScript autocomplete for parent classes' properties and methods not working Godot 4.0.1 stable - GDScript autocomplete for parent classes' properties and methods not working Mar 21, 2023
@Calinou
Copy link
Member

Calinou commented Mar 21, 2023

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

@zeroxx1986
Copy link
Author

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.

@AThousandShips
Copy link
Member

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

@zeroxx1986
Copy link
Author

Fair enough - I've added the type hint into the editor, yet autocomplete is not working:

image

@zeroxx1986
Copy link
Author

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

image

@Calinou
Copy link
Member

Calinou commented Mar 21, 2023

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

@zeroxx1986
Copy link
Author

Yes, it does!

image

@KoBeWi
Copy link
Member

KoBeWi commented Mar 21, 2023

Can't reproduce™
image
image
Make sure the scene using your script is currently opened. Path-based autocompletion works based on the current scene.

@zeroxx1986
Copy link
Author

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.

image

@ajreckof
Copy link
Member

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

MRP.zip

@MBoffin
Copy link

MBoffin commented Oct 17, 2023

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 [399c9dc] 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.

autocomplete

@HolonProduction
Copy link
Member

For the problem with type hints not working see #74888.

The unique problem here is what was described by ajreckof.

@HolonProduction
Copy link
Member

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.

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

7 participants