diff --git a/scene/main/world.cpp b/scene/main/world.cpp index 89d4976ad..c3d8bf7d6 100644 --- a/scene/main/world.cpp +++ b/scene/main/world.cpp @@ -178,9 +178,10 @@ void World::set_override_world(World *p_world) { return; } - _on_before_world_override_changed(); - World *old_world = get_override_world(); + Ref old_world_3d = find_world_3d(); + + _on_before_world_override_changed(); if (old_world) { old_world->_remove_overridden_world(this); @@ -192,6 +193,20 @@ void World::set_override_world(World *p_world) { _override_world->_add_overridden_world(this); } + if (old_world_3d.is_valid()) { + Ref new_world_3d = find_world_3d(); + + if (old_world_3d != new_world_3d) { + if (new_world_3d.is_valid()) { + old_world_3d->move_cameras_into(new_world_3d); + } else { + ERR_PRINT("!new_world_3d.is_valid()"); + } + } + } else { + ERR_PRINT("!old_world_3d.is_valid()"); + } + _on_after_world_override_changed(); } void World::set_override_world_bind(Node *p_world) { @@ -325,14 +340,13 @@ void World::_notification(int p_what) { } } break; case NOTIFICATION_EXIT_TREE: { - for (int i = 0; i < _overriding_worlds.size(); ++i) { - World *w = _overriding_worlds[i]; + set_override_world(NULL); + + while (_overriding_worlds.size() > 0) { + World *w = _overriding_worlds[0]; w->set_override_world(NULL); } - - _override_world = NULL; - } break; } } diff --git a/scene/resources/world_3d.cpp b/scene/resources/world_3d.cpp index 37d643480..1989de0ce 100644 --- a/scene/resources/world_3d.cpp +++ b/scene/resources/world_3d.cpp @@ -314,6 +314,20 @@ void World3D::get_camera_list(List *r_cameras) { } } +void World3D::move_cameras_into(Ref target) { + ERR_FAIL_COND(!target.is_valid()); + + List cameras; + get_camera_list(&cameras); + + for (List::Element *E = cameras.front(); E; E = E->next()) { + Camera *cam = E->get(); + + _remove_camera(cam); + target->_register_camera(cam); + } +} + void World3D::_bind_methods() { ClassDB::bind_method(D_METHOD("get_space"), &World3D::get_space); ClassDB::bind_method(D_METHOD("get_scenario"), &World3D::get_scenario); diff --git a/scene/resources/world_3d.h b/scene/resources/world_3d.h index 20956bc80..57b048076 100644 --- a/scene/resources/world_3d.h +++ b/scene/resources/world_3d.h @@ -83,6 +83,8 @@ public: PhysicsDirectSpaceState *get_direct_space_state(); + void move_cameras_into(Ref target); + World3D(); ~World3D(); };