Yey! Scripts are now ID based.

This commit is contained in:
Anemunt
2025-12-18 18:35:06 -05:00
parent 129d596bca
commit 4d82874157
2 changed files with 23 additions and 3 deletions

View File

@@ -1614,6 +1614,7 @@ void Engine::renderInspectorPanel() {
ScriptContext ctx; ScriptContext ctx;
ctx.engine = this; ctx.engine = this;
ctx.object = &obj; ctx.object = &obj;
ctx.script = ≻
// Scope script inspector to avoid shared ImGui IDs across objects or multiple instances // Scope script inspector to avoid shared ImGui IDs across objects or multiple instances
std::string inspectorId = "ScriptInspector##" + std::to_string(obj.id) + sc.path; std::string inspectorId = "ScriptInspector##" + std::to_string(obj.id) + sc.path;
ImGui::PushID(inspectorId.c_str()); ImGui::PushID(inspectorId.c_str());

View File

@@ -2,6 +2,7 @@
#include "Engine.h" #include "Engine.h"
#include "SceneObject.h" #include "SceneObject.h"
#include <algorithm> #include <algorithm>
#include <iterator>
#include <unordered_map> #include <unordered_map>
#if defined(_WIN32) #if defined(_WIN32)
@@ -10,6 +11,24 @@
#include <dlfcn.h> #include <dlfcn.h>
#endif #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<uintptr_t>(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) { SceneObject* ScriptContext::FindObjectByName(const std::string& name) {
if (!engine) return nullptr; if (!engine) return nullptr;
return engine->findObjectByName(name); 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; [&](const AutoSettingEntry& e){ return e.key == key; })) return;
static std::unordered_map<std::string, bool> defaults; static std::unordered_map<std::string, bool> defaults;
std::string scriptId = (!script->path.empty()) ? script->path : std::to_string(reinterpret_cast<uintptr_t>(script)); std::string scriptId = makeScriptInstanceKey(*this);
std::string id = scriptId + "|" + key; std::string id = scriptId + "|" + key;
bool defaultVal = value; bool defaultVal = value;
auto itDef = defaults.find(id); 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; [&](const AutoSettingEntry& e){ return e.key == key; })) return;
static std::unordered_map<std::string, glm::vec3> defaults; static std::unordered_map<std::string, glm::vec3> defaults;
std::string scriptId = (!script->path.empty()) ? script->path : std::to_string(reinterpret_cast<uintptr_t>(script)); std::string scriptId = makeScriptInstanceKey(*this);
std::string id = scriptId + "|" + key; std::string id = scriptId + "|" + key;
glm::vec3 defaultVal = value; glm::vec3 defaultVal = value;
auto itDef = defaults.find(id); 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; [&](const AutoSettingEntry& e){ return e.key == key; })) return;
static std::unordered_map<std::string, std::string> defaults; static std::unordered_map<std::string, std::string> defaults;
std::string scriptId = (!script->path.empty()) ? script->path : std::to_string(reinterpret_cast<uintptr_t>(script)); std::string scriptId = makeScriptInstanceKey(*this);
std::string id = scriptId + "|" + key; std::string id = scriptId + "|" + key;
std::string defaultVal = defaults.count(id) ? defaults[id] : std::string(buffer); std::string defaultVal = defaults.count(id) ? defaults[id] : std::string(buffer);
defaults.try_emplace(id, defaultVal); defaults.try_emplace(id, defaultVal);