+4 votes

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) ?

in Engine by (32 points)

1 Answer

+4 votes
Best answer

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(): https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-request-ready

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.

by (29,116 points)
selected by

If you really want ready() to be called again, you can request it with requestready():

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 ;)

I assume you're suggesting calling requestready from within _exittree 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.

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
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.