All Questions

Community

A
Chief A
posted to: Coding the State

Owner not loaded in time for an inherited child state connect to a signal

I found the following lines in the State.gd code:

func _ready () -> void:
	yield (owner, "ready")


So I'm making some tests and somehow trying to connect the AnimationPlayer of the owner (Player) to a state, however it is not working. I've tried to make a super call, but it still didn't work:

func _ready () -> void:
	._ready ()
	owner.anim.connect ("animation_finished", self, "_on_anim_finished")


The only way it works is if I call the yield in the inherited state itself:

func _ready () -> void:
# ._ready ()
  yield (owner, "ready")
  owner.anim.connect ("animation_finished", self, "_on_anim_finished")
  

So, how could I solve this in such a way that the yield(owner...) could work in it's inherited children if they have they own _ready()?

  • Nathan Lovato replied
    Solution

    That's normal. Using yield turns a function into a coroutine: it returns a GDScriptFunctionState object instantly and finishes later. So when you write:

    func _ready () -> void:
    	._ready ()
    

    The parent class's _ready() call returns instantly, before it finishes. You have to add a yield on the child node to wait for the parent's coroutine to finish:

    func _ready () -> void:
        yield(._ready(), "completed")
    
  • A
    Chief A replied

    Thanks, man!