From b927e2e70954f7d56f5d2aedc5f6a6d2e36242e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Mon, 14 Sep 2026 22:30:10 +0200 Subject: [PATCH] Fixed "Save component to edit mode" and also jankily save script data --- .../src/EditWindows/ComponentEditWindow.bf | 22 ++++++++++++++----- GlitchyEditor/src/EditorApp.bf | 2 ++ .../src/World/Components/Components.bf | 9 +++++++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf index 359e726..10bca6e 100644 --- a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf +++ b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf @@ -205,17 +205,29 @@ namespace GlitchyEditor.EditWindows positionX -= buttonSize.X + ImGui.GetStyle().FramePadding.x; ImGui.SameLine(positionX); - if (ImGui.ImageButton("save", EditorIcons.Instance.Icon_Save, (.)buttonSize)) + if (ImGui.ImageButton("save_to_editor", EditorIcons.Instance.Icon_Save, (.)buttonSize)) { if (Entity editorEntity = Editor.Instance.EditorScene.GetEntityByID(entity.UUID)) { Scene.CopyComponent(entity, editorEntity); - + if (typeof(TComponent) == typeof(ScriptComponent)) { - // TODO: Copy script data into editor - // TODO: Somehow get the scriptserializer used to serialize the editor scene - //scriptSerializer.SerializeScriptInstance(((ScriptComponent*)component).Instance); + // Dirty hack to get to the serialized script instances of the editor scene. + var serializer = EditorApp.Instance.EditorLayer.[Friend]_scriptSerializer; + + // Dirty hack to remove the old data + if (serializer.SerializedObjects.GetAndRemove(entity.UUID) case .Ok(let kvPair)) + { + delete kvPair.value; + } + + // Just save the new data into the context (what could possibly go wrong?!) + // this could probably serialize too much data into our context (because we can serialize reference types) + // and leave a bunch of dangling data in it (because we can serialize reference types) + serializer.SerializeScriptInstance(((ScriptComponent*)component).Instance); + // This is also a massive hack to make sure we don't keep references to types, but this also mean we might serialize too much? + serializer.FinishSerialization(); } } else diff --git a/GlitchyEditor/src/EditorApp.bf b/GlitchyEditor/src/EditorApp.bf index 094edfe..1d465c9 100644 --- a/GlitchyEditor/src/EditorApp.bf +++ b/GlitchyEditor/src/EditorApp.bf @@ -34,6 +34,8 @@ namespace GlitchyEditor private EditorLayer _editorLayer; public Settings Settings { get; private set; } = new .() ~ delete _; + + public EditorLayer EditorLayer => _editorLayer; public this(String[] args) { diff --git a/GlitchyEngine/src/World/Components/Components.bf b/GlitchyEngine/src/World/Components/Components.bf index d5d4755..81e780b 100644 --- a/GlitchyEngine/src/World/Components/Components.bf +++ b/GlitchyEngine/src/World/Components/Components.bf @@ -670,7 +670,7 @@ namespace GlitchyEngine.World } } - struct ScriptComponent : IDisposableComponent + struct ScriptComponent : IDisposableComponent, ICopyComponent { private String _scriptClassName = null; @@ -705,5 +705,12 @@ namespace GlitchyEngine.World delete _scriptClassName; ReleaseRefAndNullify!(_instance); } + + public static void Copy(ScriptComponent* source, ScriptComponent* target) + { + target.ScriptClassName = source.ScriptClassName; + + // TODO: This is just half the work, because we actually have to somehow handle the script instance + } } } \ No newline at end of file