Repository: godot
Issue: Editor Crash After Deleting Terrain In Inspector While It is Selected In TileSet Atlas
Labels: bug,topic:editor,crash,topic:2d

### Godot version

4.1.2.stable [399c9dc39]

### System information

Godot v4.1.2.stable.mono - Ubuntu 22.04.3 LTS 22.04 - X11 - Vulkan (Mobile) - dedicated NVIDIA GeForce GTX 1050 Ti (nvidia; 535.129.03) - 12th Gen Intel(R) Core(TM) i5-12400F (12 Threads)

### Issue description

Editor crashes after deleting a terrain in the Inspector while the terrain to delete is selected in the TileSet Atlas.

Editor crashes with:

```
ERROR: FATAL: Index p_index = 1 is out of bounds (count = 1).
```

Editor output with `--verbose` flag set:

```
EditorSettings: Load OK!
WARNING: Blend file import is enabled in the project settings, but no Blender path is configured in the editor settings. Blend files will not be imported.
     at: _editor_init (modules/gltf/register_types.cpp:73)
Found .NET Sdk version '7.0.113': /usr/lib/dotnet/sdk/7.0.113
Loaded system CA certificates
Creating VMA small objects pool for memory type index 9
EditorSettings: Save OK!
ERROR: Condition "plugins_list.has(p_plugin)" is true.
   at: add_plugin (editor/editor_node.cpp:8223)
ERROR: Condition "plugins_list.has(p_plugin)" is true.
   at: add_plugin (editor/editor_node.cpp:8223)
ERROR: Condition "format != p_src->format" is true.
   at: blit_rect (core/io/image.cpp:2776)
ERROR: Condition "format != p_src->format" is true.
   at: blit_rect (core/io/image.cpp:2776)
ERROR: FATAL: Index p_index = 1 is out of bounds (count = 1).
   at: operator[] (./core/templates/local_vector.h:161)

================================================================
handle_crash: Program crashed with signal 4
Engine version: Godot Engine v4.1.2.stable.mono.official (399c9dc393f6f84c0b4e4d4117906c70c048ecf2)
Dumping the backtrace. Please include this when reporting the bug to the project developer.
[1] /usr/lib/dotnet/shared/Microsoft.NETCore.App/7.0.13/libcoreclr.so(+0x5f6d0e) [0x7f0a665f6d0e] (??:0)
[2] /lib/x86_64-linux-gnu/libc.so.6(+0x42520) [0x7f0a97842520] (??:0)
[3] ./Godot_v4.1.2-stable_mono_linux.x86_64() [0x3947c84] (??:0)
[4] ./Godot_v4.1.2-stable_mono_linux.x86_64() [0x2c59edf] (??:0)
[5] ./Godot_v4.1.2-stable_mono_linux.x86_64() [0x2c236b6] (??:0)
[6] ./Godot_v4.1.2-stable_mono_linux.x86_64() [0xfe9daa] (??:0)
[7] ./Godot_v4.1.2-stable_mono_linux.x86_64() [0x483b8e8] (??:0)
[8] ./Godot_v4.1.2-stable_mono_linux.x86_64() [0x468c014] (??:0)
[9] ./Godot_v4.1.2-stable_mono_linux.x86_64() [0x4e904e5] (??:0)
[10] ./Godot_v4.1.2-stable_mono_linux.x86_64() [0x489f850] (??:0)
[11] ./Godot_v4.1.2-stable_mono_linux.x86_64() [0x2dc5da3] (??:0)
[12] ./Godot_v4.1.2-stable_mono_linux.x86_64() [0xf251f6] (??:0)
[13] ./Godot_v4.1.2-stable_mono_linux.x86_64() [0xe55254] (??:0)
[14] /lib/x86_64-linux-gnu/libc.so.6(+0x29d90) [0x7f0a97829d90] (??:0)
[15] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80) [0x7f0a97829e40] (??:0)
[16] ./Godot_v4.1.2-stable_mono_linux.x86_64() [0xe87bae] (??:0)
-- END OF BACKTRACE --
================================================================
Aborted (core dumped)
```

### Steps to reproduce

- Create a TileMap and add tileset image to it in TileSet panel/dock
- Create two terrainsin the TileMap Inspector (e.g. "Ground" & "Roads")
- Assign terrain to tiles by painting according terrain
- Draw tiles in the TileMap into scene
- Go to TileSet panel/dock and select Terrain Set and the terrain to delete ("Ground")
- In Inspector of the TileMap delete the currently selected terrain ("Ground")

When using attached minimal reproduction project just follow the last two steps.

![screenshot_steps_to_reproduce2](https://github.com/godotengine/godot/assets/8314019/dab0e51a-5527-4d18-bc0b-5631b9296933)


### Minimal reproduction project

[tilemap_crash_project.zip](https://github.com/godotengine/godot/files/13256603/tilemap_crash_project.zip)