0 votes

I have a general enemy class (parent), and a enemy type (child). The parent class has a ready function which calls some functions, and the child's _ready function initializes some variables, and calls the parents _ready function. Here's a example code

parent:

extends KinematicBody2D
class_name Enemy
func _ready():
    print("parent")
    LoadRaycasts()

child:

extends Enemy
func _ready():
    print("child")
    health=20
    speed=30
   .ready()

I was expecting the output as:
child
parent

But what I get is:
parent
child
parent

Even when I don't call the parents _ready from child I still get:
*parent
child*

Where I think I should be getting child only. As I am overriding the parents _ready()

I need to call the parents _ready after childs _ready() is called only once But somehow parent's _ready gets called before childs even if I override it.

I tried printing objects that are calling the _ready() by
print("parent:"+str(self)) and print("parent:"+str(self))
Then I see that the functions are called by the same object.
Heres the output
*parent:[KinematicBody2D:1185]
child:[KinematicBody2D:1185]
parent:[KinematicBody2D:1185]*

Thank you for reading the question.
It will be really helpful if you could explain what I am doing wrong and what I should be doing.

in Engine by (26 points)

1 Answer

0 votes
Best answer

functions like _ready() , _process(delta), _physics_process(delta) .. etc are not overridden and parents are initialized first.

by (731 points)
selected by

Oh got it thanks.
Now I changed the ready() in parent to _onready(), and call it from child after initializing.

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.