Browse Source

Ensure that SceneTree is initialized and finalized at correct time

SceneTree should be fully initialized before any tree operation with any node and finalized only after all nodes exited tree.
Nickolai Korshunov 2 years ago
parent
commit
83f065c0ef
2 changed files with 6 additions and 10 deletions
  1. 6 9
      scene/main/scene_tree.cpp
  2. 0 1
      scene/main/scene_tree.h

+ 6 - 9
scene/main/scene_tree.cpp

@@ -444,9 +444,8 @@ void SceneTree::set_group(const StringName &p_group, const String &p_name, const
 
 
 void SceneTree::initialize() {
 void SceneTree::initialize() {
 	ERR_FAIL_NULL(root);
 	ERR_FAIL_NULL(root);
-	initialized = true;
-	root->_set_tree(this);
 	MainLoop::initialize();
 	MainLoop::initialize();
+	root->_set_tree(this);
 }
 }
 
 
 bool SceneTree::physics_process(double p_time) {
 bool SceneTree::physics_process(double p_time) {
@@ -618,20 +617,18 @@ void SceneTree::finalize() {
 
 
 	_flush_ugc();
 	_flush_ugc();
 
 
-	initialized = false;
-
-	MainLoop::finalize();
-
 	if (root) {
 	if (root) {
 		root->_set_tree(nullptr);
 		root->_set_tree(nullptr);
 		root->_propagate_after_exit_tree();
 		root->_propagate_after_exit_tree();
 		memdelete(root); //delete root
 		memdelete(root); //delete root
 		root = nullptr;
 		root = nullptr;
+
+		// In case deletion of some objects was queued when destructing the `root`.
+		// E.g. if `queue_free()` was called for some node outside the tree when handling NOTIFICATION_PREDELETE for some node in the tree.
+		_flush_delete_queue();
 	}
 	}
 
 
-	// In case deletion of some objects was queued when destructing the `root`.
-	// E.g. if `queue_free()` was called for some node outside the tree when handling NOTIFICATION_PREDELETE for some node in the tree.
-	_flush_delete_queue();
+	MainLoop::finalize();
 
 
 	// Cleanup timers.
 	// Cleanup timers.
 	for (Ref<SceneTreeTimer> &timer : timers) {
 	for (Ref<SceneTreeTimer> &timer : timers) {

+ 0 - 1
scene/main/scene_tree.h

@@ -138,7 +138,6 @@ private:
 
 
 	HashMap<StringName, Group> group_map;
 	HashMap<StringName, Group> group_map;
 	bool _quit = false;
 	bool _quit = false;
-	bool initialized = false;
 
 
 	StringName tree_changed_name = "tree_changed";
 	StringName tree_changed_name = "tree_changed";
 	StringName node_added_name = "node_added";
 	StringName node_added_name = "node_added";