How to modify child node's _ready and _process functions from the parent's script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By nergadzed

Hello!

I’m fairly new to the Godot world and I do understand that this might not be the most efficient or “proper” way of doing things, but I like to do things in one place and please excuse me.

How do I assign child node’s _ready function from the parent node’s script?

I did try

MyObjet.set("_ready", custom_ready_funcref)

didn’t work.

:bust_in_silhouette: Reply From: Ertain

If you only need to do something with a child node’s _ready() function, attach a script to the child node (if you can attach it in the editor, and not trying to do this at runtime), and write the code in the child node’s _ready() function.

If it’s something more complicated, then please explain the problem a bit more.

I’m creating nodes using .new method in parent object’s _init method. This function must be executed at run time, doesn’t it?! I think what I’m trying do to is not exactly possible, I’m generating objects on the fly and trying to assign overrides of virtual initialization and processing functions at run time…

nergadzed | 2023-01-28 16:37

If these are classes such that the _init() function is being used, then figure out which properties of the class must be defined outside of the class, and add them as parameters to the _init() function:

class Foo:
    var thing1
    var thing2
    func _init(item1, item2):
        var thing1 = item1
        var thing2 = item2
...

Then when it comes time to assign an object, fill in the necessary data:

var bar = Foo.new("I'm Thing 1!", "I'm Thing 2!")

Note that this is only when making a class. Regular ol’ GDScript with a node that extends another node doesn’t require the _init() function to be defined. Hope that helps.

Ertain | 2023-01-29 00:12