Force tool scripts to run _ready() on reload in editor

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

Hi all,

Using Godot 3.2.2 My issue is simple.

  • I have a tool script that does some work in _ready() that sets up the scene.
  • When first loading everything is good, since _ready() is run.
  • However, If I open another scene in a tab, an then switch back to a scene with this node in it, it will not run _ready().
  • If I close the tab, then reopen, it is again consistent, and runs _ready()

This is a problem for me, as _ready() does initialization like seeding rand with a constant in order to generate consistent (but easily randomizable), decorations in a sub sceen (also a tool).

Is there any way to force _ready() to run again in a tool script, when it regains focus, (Tab focus etc) ?

:bust_in_silhouette: 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 :wink:

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 :wink:

I dont see that behavior changing in the near future :slight_smile:


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 Nodes 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