derkork / godot-statecharts

A state charts extension for Godot 4
MIT License
761 stars 39 forks source link

OnReady vars null on initial enter state #28

Closed Red3Tango closed 1 year ago

Red3Tango commented 1 year ago

Using vars that are set with @onready (or in _ready()) are still unset when the Initial State is first Entered. Only workaround I have found, for nodes use $Node/Path or get_node(). For dynamic vars, would need to be hard-coded.

I have not found an elegant way around this to "fix", and there may not be. A note or warning may be nice about the load order of when in runs the initial state enter signal.

derkork commented 1 year ago

I think this is caused by the recent change that ensures that state changes happen in the same frame. It could be fixed by delaying entering the initial state of the state chart by one frame, to ensure that the full tree is ready.

I'll build a new release with this change as i think this makes more sense than the current implementation but I will not be able to do it today. As a temporary workaround you can change this line in state_chart.gd which should delay the state entering by one frame:

image

Red3Tango commented 1 year ago

I always forget that call_deferred() exists. Tested and can confirm this works.

derkork commented 1 year ago

Released 0.4.3 which has the fix built-in.