diff --git a/src/EditorWindows/SceneWindows.cpp b/src/EditorWindows/SceneWindows.cpp index 0111901..e96a8dd 100644 --- a/src/EditorWindows/SceneWindows.cpp +++ b/src/EditorWindows/SceneWindows.cpp @@ -1614,6 +1614,7 @@ void Engine::renderInspectorPanel() { ScriptContext ctx; ctx.engine = this; ctx.object = &obj; + ctx.script = ≻ // Scope script inspector to avoid shared ImGui IDs across objects or multiple instances std::string inspectorId = "ScriptInspector##" + std::to_string(obj.id) + sc.path; ImGui::PushID(inspectorId.c_str()); diff --git a/src/ScriptRuntime.cpp b/src/ScriptRuntime.cpp index 5199a95..d133878 100644 --- a/src/ScriptRuntime.cpp +++ b/src/ScriptRuntime.cpp @@ -2,6 +2,7 @@ #include "Engine.h" #include "SceneObject.h" #include +#include #include #if defined(_WIN32) @@ -10,6 +11,24 @@ #include #endif +namespace { +std::string makeScriptInstanceKey(const ScriptContext& ctx) { + if (!ctx.script) return {}; + std::string key = (!ctx.script->path.empty()) + ? ctx.script->path + : std::to_string(reinterpret_cast(ctx.script)); + if (ctx.object) { + key += "|obj:" + std::to_string(ctx.object->id); + auto it = std::find_if(ctx.object->scripts.begin(), ctx.object->scripts.end(), + [&](const ScriptComponent& s) { return &s == ctx.script; }); + if (it != ctx.object->scripts.end()) { + key += "|slot:" + std::to_string(std::distance(ctx.object->scripts.begin(), it)); + } + } + return key; +} +} + SceneObject* ScriptContext::FindObjectByName(const std::string& name) { if (!engine) return nullptr; return engine->findObjectByName(name); @@ -208,7 +227,7 @@ void ScriptContext::AutoSetting(const std::string& key, bool& value) { [&](const AutoSettingEntry& e){ return e.key == key; })) return; static std::unordered_map defaults; - std::string scriptId = (!script->path.empty()) ? script->path : std::to_string(reinterpret_cast(script)); + std::string scriptId = makeScriptInstanceKey(*this); std::string id = scriptId + "|" + key; bool defaultVal = value; auto itDef = defaults.find(id); @@ -233,7 +252,7 @@ void ScriptContext::AutoSetting(const std::string& key, glm::vec3& value) { [&](const AutoSettingEntry& e){ return e.key == key; })) return; static std::unordered_map defaults; - std::string scriptId = (!script->path.empty()) ? script->path : std::to_string(reinterpret_cast(script)); + std::string scriptId = makeScriptInstanceKey(*this); std::string id = scriptId + "|" + key; glm::vec3 defaultVal = value; auto itDef = defaults.find(id); @@ -258,7 +277,7 @@ void ScriptContext::AutoSetting(const std::string& key, char* buffer, size_t buf [&](const AutoSettingEntry& e){ return e.key == key; })) return; static std::unordered_map defaults; - std::string scriptId = (!script->path.empty()) ? script->path : std::to_string(reinterpret_cast(script)); + std::string scriptId = makeScriptInstanceKey(*this); std::string id = scriptId + "|" + key; std::string defaultVal = defaults.count(id) ? defaults[id] : std::string(buffer); defaults.try_emplace(id, defaultVal);