0 votes

Hi,

I am attempting my first project with animatedsprites. I generally am putting scenes in a containing Node2D just to hold all of my subnodes. So, I have a "zombie" Node with a single child node that is an animatedsprite and holds 5 different animations.

I am using this code in the top level Node2D (zombie):

extends Node2D

var state = 0 # 0 = not alive, 1 = appearing, 2 = walking, 3 = dieing, 4 = idle
var animator = self.get_child(1) # we will only have a single, animatedsprite

func _ready():
    self.set_process(true)
    state = 1 # we are alive and should come out of the ground


func _process(delta):
    if (state) == 1:
        animator.set_current_animation
...

So my question is, when I assign the animatedsprite node in the "get_child" call at the top, how come the editor does not give me the methods on that type at the bottom? The line that says:

animator.set_current_animation

Is not recognized in the editor. I had to type that in and hope that it works. Am I doing something wrong? Should I be casting the 'animator' variable to an 'animatedsprite' type to get this to work?

Thanks.

asked Feb 9, 2017 in Engine by Judd Gledhill (41 points)

1 Answer

+1 vote

Casting is not a thing in this case, because GDScript is a dynamically-typed language. Auto-completion usually works by "guessing" what is the type of stuff by looking only at the script, and also the scene currently open (which allows knowing what is the type of things accessed using get_node()).

You have found a case where auto-complete doesn't work, indeed. But that's also kind of a bad practice: you should write the name of the node, not the index of it (if your root has only one child, then 1 is even going to produce an error because indices start at 0).

Also, you can't use functions that access the tree to initialize your member variable, unless you prefix it by the onready keyword, which will make it executed at the same time _ready() is called.

To sum things up, try this instead:

onready var animator = get_node("NameOfYourAnimatedSprite")
answered Feb 9, 2017 by Zylann (26,055 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.