@@ -2585,7 +2585,7 @@ void EditorNode::_dialog_action(String p_file) {
25852585 case SCENE_SAVE_SCENE:
25862586 case SCENE_MULTI_SAVE_AS_SCENE:
25872587 case SCENE_SAVE_AS_SCENE: {
2588- int scene_idx = (current_menu_option == SCENE_SAVE_SCENE || current_menu_option == SCENE_SAVE_AS_SCENE || current_menu_option == SCENE_MULTI_SAVE_AS_SCENE) ? -1 : tab_closing_idx ;
2588+ int scene_idx = (current_menu_option == SCENE_MULTI_SAVE_AS_SCENE) ? -1 : tab_context_idx ;
25892589
25902590 if (file->get_file_mode () == EditorFileDialog::FILE_MODE_SAVE_FILE) {
25912591 bool same_open_scene = false ;
@@ -2617,6 +2617,7 @@ void EditorNode::_dialog_action(String p_file) {
26172617 _proceed_save_asing_scene_tabs ();
26182618 }
26192619
2620+ tab_context_idx = -1 ;
26202621 } break ;
26212622
26222623 case SAVE_AND_RUN: {
@@ -2640,7 +2641,7 @@ void EditorNode::_dialog_action(String p_file) {
26402641
26412642 case SAVE_AND_SET_MAIN_SCENE: {
26422643 _save_scene (p_file);
2643- _menu_option_confirm (SCENE_TAB_SET_AS_MAIN_SCENE , true );
2644+ _menu_option_confirm (SCENE_TAB_TAB_SET_AS_AS_MAIN_SCENE , true );
26442645 } break ;
26452646
26462647 case FILE_EXPORT_MESH_LIBRARY: {
@@ -3232,6 +3233,7 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
32323233
32333234 switch (p_option) {
32343235 case SCENE_NEW_SCENE: {
3236+ tab_context_idx = -1 ; // Don't use a specific tab as context.
32353237 new_scene ();
32363238
32373239 } break ;
@@ -3263,14 +3265,17 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
32633265 quick_open_dialog->popup_dialog ({ " Script" }, callable_mp (this , &EditorNode::_quick_opened));
32643266 } break ;
32653267 case SCENE_OPEN_PREV: {
3268+ tab_context_idx = -1 ; // Don't use a specific tab as context.
32663269 if (!prev_closed_scenes.is_empty ()) {
32673270 load_scene (prev_closed_scenes.back ()->get ());
32683271 }
32693272 } break ;
32703273 case EditorSceneTabs::SCENE_CLOSE_OTHERS: {
32713274 tab_closing_menu_option = -1 ;
3275+ int selected_tab = tab_context_idx >= 0 ? tab_context_idx : editor_data.get_edited_scene ();
3276+ tab_context_idx = -1 ;
32723277 for (int i = 0 ; i < editor_data.get_edited_scene_count (); i++) {
3273- if (i == editor_data. get_edited_scene () ) {
3278+ if (i == selected_tab ) {
32743279 continue ;
32753280 }
32763281 tabs_to_close.push_back (editor_data.get_scene_path (i));
@@ -3279,7 +3284,9 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
32793284 } break ;
32803285 case EditorSceneTabs::SCENE_CLOSE_RIGHT: {
32813286 tab_closing_menu_option = -1 ;
3282- for (int i = editor_data.get_edited_scene () + 1 ; i < editor_data.get_edited_scene_count (); i++) {
3287+ int selected_tab = tab_context_idx >= 0 ? tab_context_idx : editor_data.get_edited_scene ();
3288+ tab_context_idx = -1 ;
3289+ for (int i = selected_tab + 1 ; i < editor_data.get_edited_scene_count (); i++) {
32833290 tabs_to_close.push_back (editor_data.get_scene_path (i));
32843291 }
32853292 _proceed_closing_scene_tabs ();
@@ -3292,11 +3299,13 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
32923299 _proceed_closing_scene_tabs ();
32933300 } break ;
32943301 case SCENE_CLOSE: {
3295- _scene_tab_closed (editor_data.get_edited_scene ());
3302+ int scene_to_close = tab_context_idx >= 0 ? tab_context_idx : editor_data.get_edited_scene ();
3303+ tab_context_idx = -1 ;
3304+ _scene_tab_closed (scene_to_close);
32963305 } break ;
32973306 case SCENE_TAB_CLOSE:
32983307 case SCENE_SAVE_SCENE: {
3299- int scene_idx = (p_option == SCENE_SAVE_SCENE) ? -1 : tab_closing_idx ;
3308+ int scene_idx = (p_option == SCENE_SAVE_SCENE && tab_context_idx < 0 ) ? -1 : tab_context_idx ;
33003309 Node *scene = editor_data.get_edited_scene_root (scene_idx);
33013310 if (scene && !scene->get_scene_file_path ().is_empty ()) {
33023311 if (DirAccess::exists (scene->get_scene_file_path ().get_base_dir ())) {
@@ -3313,13 +3322,14 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
33133322 } else {
33143323 show_save_accept (vformat (TTR (" %s no longer exists! Please specify a new save location." ), scene->get_scene_file_path ().get_base_dir ()), TTR (" OK" ));
33153324 }
3325+ tab_context_idx = -1 ;
33163326 break ;
33173327 }
33183328 [[fallthrough]];
33193329 }
33203330 case SCENE_MULTI_SAVE_AS_SCENE:
33213331 case SCENE_SAVE_AS_SCENE: {
3322- int scene_idx = (p_option == SCENE_SAVE_SCENE || p_option == SCENE_SAVE_AS_SCENE || p_option == SCENE_MULTI_SAVE_AS_SCENE) ? -1 : tab_closing_idx ;
3332+ int scene_idx = (p_option == SCENE_MULTI_SAVE_AS_SCENE) ? -1 : tab_context_idx ;
33233333
33243334 Node *scene = editor_data.get_edited_scene_root (scene_idx);
33253335
@@ -3376,24 +3386,37 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
33763386 } break ;
33773387
33783388 case SCENE_TAB_SET_AS_MAIN_SCENE: {
3379- const String scene_path = editor_data.get_scene_path (editor_data.get_edited_scene ());
3389+ int selected_tab = tab_context_idx >= 0 ? tab_context_idx : editor_data.get_edited_scene ();
3390+ const String scene_path = editor_data.get_scene_path (selected_tab);
33803391 if (scene_path.is_empty ()) {
33813392 current_menu_option = SAVE_AND_SET_MAIN_SCENE;
33823393 _menu_option_confirm (SCENE_SAVE_AS_SCENE, true );
33833394 file->set_title (TTR (" Save new main scene..." ));
3395+ // tab_context_idx will be reset when the dialog completes.
33843396 } else {
3397+ tab_context_idx = -1 ;
33853398 ProjectSettings::get_singleton ()->set (" application/run/main_scene" , ResourceUID::path_to_uid (scene_path));
33863399 ProjectSettings::get_singleton ()->save ();
33873400 FileSystemDock::get_singleton ()->update_all ();
33883401 }
33893402 } break ;
33903403
33913404 case SCENE_SAVE_ALL_SCENES: {
3405+ tab_context_idx = -1 ; // Don't use a specific tab as context.
33923406 _save_all_scenes ();
33933407 } break ;
33943408
33953409 case EditorSceneTabs::SCENE_RUN: {
3396- project_run_bar->play_current_scene ();
3410+ if (tab_context_idx >= 0 && tab_context_idx != editor_data.get_edited_scene ()) {
3411+ String scene_path = editor_data.get_scene_path (tab_context_idx);
3412+ tab_context_idx = -1 ;
3413+ if (!scene_path.is_empty ()) {
3414+ project_run_bar->play_custom_scene (scene_path);
3415+ }
3416+ } else {
3417+ tab_context_idx = -1 ;
3418+ project_run_bar->play_current_scene ();
3419+ }
33973420 } break ;
33983421
33993422 case PROJECT_EXPORT: {
@@ -3494,7 +3517,9 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
34943517 } break ;
34953518
34963519 case EditorSceneTabs::SCENE_SHOW_IN_FILESYSTEM: {
3497- String path = editor_data.get_scene_path (editor_data.get_edited_scene ());
3520+ int selected_tab = tab_context_idx >= 0 ? tab_context_idx : editor_data.get_edited_scene ();
3521+ String path = editor_data.get_scene_path (selected_tab);
3522+ tab_context_idx = -1 ;
34983523 if (!path.is_empty ()) {
34993524 FileSystemDock::get_singleton ()->navigate_to_path (path);
35003525 }
@@ -4016,14 +4041,14 @@ void EditorNode::_discard_changes(const String &p_str) {
40164041 switch (current_menu_option) {
40174042 case SCENE_CLOSE:
40184043 case SCENE_TAB_CLOSE: {
4019- Node *scene = editor_data.get_edited_scene_root (tab_closing_idx );
4044+ Node *scene = editor_data.get_edited_scene_root (tab_context_idx );
40204045 if (scene != nullptr ) {
40214046 _update_prev_closed_scenes (scene->get_scene_file_path (), true );
40224047 }
40234048
40244049 // Don't close tabs when exiting the editor (required for "restore_scenes_on_load" setting).
40254050 if (!_is_closing_editor ()) {
4026- _remove_scene (tab_closing_idx );
4051+ _remove_scene (tab_context_idx );
40274052 scene_tabs->update_scene_tabs ();
40284053 }
40294054 _proceed_closing_scene_tabs ();
@@ -5371,7 +5396,7 @@ bool EditorNode::close_scene() {
53715396 return false ;
53725397 }
53735398
5374- tab_closing_idx = tab_index;
5399+ tab_context_idx = tab_index;
53755400 current_menu_option = SCENE_CLOSE;
53765401 _discard_changes ();
53775402 changing_scene = false ;
@@ -6489,7 +6514,7 @@ void EditorNode::_restart_editor(bool p_goto_project_manager) {
64896514
64906515void EditorNode::_scene_tab_closed (int p_tab) {
64916516 current_menu_option = SCENE_TAB_CLOSE;
6492- tab_closing_idx = p_tab;
6517+ tab_context_idx = p_tab;
64936518 Node *scene = editor_data.get_edited_scene_root (p_tab);
64946519 if (!scene) {
64956520 _discard_changes ();
0 commit comments