All Questions

Community

M
Mr Spookydoom

Reason for autocomplete sometimes not working?

On some occasions Autocomplete doesn't really seem to work like here:

func _on_timer_timeout() -> void:
    for target in target_list:
        var rocket := preload("res://TowerDefense/common/Rocket.tscn").instance()
        add_child(rocket)
        var target_angle = target.global_position.angle_to_point(cannon.global_position)
# Here for some reason when typing rocket.ro... Autocomplete doesn't work
        rocket.rotation = target_angle
        rocket.global_position = cannon.global_position

That's kind of confusing because I keep thinking I made a mistake or am trying to call something I can't and probably have some type error or something.

Does this happen in predictable situations where I just have to know to keep tying despite not getting any autocomplete hints?

  • Razoric replied

    A lot of Godot uses dynamic language. When you call `preload().instance()`, Godot will return a 'Node'. It has no idea that it's actually Rocket.gd at the time you're coding, so it can only give you the generic stuff it knows for sure, and that a TSCN will produce a Node. There's no at-coding-time analysis of what's coming out of a TSCN file, not for coding purposes. It knows it's a PackedScene, but that's it. It doesn't instance it and look at its insides.

    In such cases, you must clarify, so you can use `var rocket: Spatial = preload(...).instance()`, or anything more specific that you may need.

  • Nathan Lovato replied

    As Razoric explained, this is a problem with Godot not understanding what a rocket is and what functions it has because the instance() function is generic and can produce all kinds of nodes.

    To get auto-completion for the rotation, position, etc. You need to give Godot a more precise a type hint. In this case, Node2D should do.

    The reason is that the position, rotation, and scale are defined on the Node2D type, which areas, physics, bodies, etc. extend. So we can use it to get auto-completion just for the Node2D functions and variables.

    var rocket: Node2D = preload("res://TowerDefense/common/Rocket.tscn").instance()