|
|
|
|
Reply From: |
Zylann |
There are a few things you need to know about what’s actually happening:
When you switch tabs, Godot removes your scene from the scene tree. but it’s still there, the nodes are not destroyed, so there is no reason to call _ready
again to reload things a second time, at least not by default.
When you switch back, Godot then re-adds your scene to the tree.
You can detect this by implementingt the _enter_tree
and _exit_tree
methods, which will get called when this happens.
Only catch to be aware, is that _enter_tree
will also get called before the first _ready
.
Finally, when you close the tab and reopen it, Godot does remove AND delete your nodes, so that’s why when you reopen the scene, it of course calls _ready()
because Godot reloads it entirely.
If you really want _ready()
to be called again, you can request it with request_ready()
: Node — Godot Engine (stable) documentation in English
Note, if you use _ready()
to spawn child nodes, you may not want it to be called again, because you’ll end up creating the same nodes again.
If you really want _ready() to be called again, you can request it with request_ready():
Thanks. I assume you’re suggesting calling request_ready
from within _exit_tree
in this case?
Sounds like I could probably get away with replacing _ready
with doing this work in _enter_tree
instead. That is until the tab behavior changes in an later Godot version
Razzlegames | 2020-09-13 18:38
I assume you’re suggesting calling request_ready from within _exit_tree in this case?
According to the doc, I suppose you can even call it in _ready()
.
That is until the tab behavior changes in an later Godot version
I dont see that behavior changing in the near future
Some extra info: _exit_tree
and _enter_tree
are also called when you reparent the node.
Zylann | 2020-09-13 18:40
Note on request_ready()
:
func _ready():
doSomeProceduralStuff()
request_ready()
Another thing I noticed is on scene reload (get_tree().reload_current_scene()
), any cached Node
s references are null
onready var someNode = $SomeNode
To get around this I need to lookup the node again in any calling code. I also noticed that _enter_tree
is called before subnodes are ready.
I wasn’t 100% sure if the subnodes would be guaranteed to be initialized in the request_ready
lifecycle case, so I opted for this:
func _enter_tree():
call_deferred("doSomeProceduralStuff")
# ....
func doSomeProceduralStuff():
var someNode = $SomeNode`
# Do whatever with someNode
Razzlegames | 2020-09-13 19:30