fix(scripts): prevent duplicate script attachment per entity

The per-entity script list allowed picking the same script class
twice; the name-keyed runtime silently ignores duplicates, leaving
the extra rows dead weight with unusable enable bits. Enforce
DisallowMultipleComponent semantics: the dropdown of each row only
offers unattached names (own name kept for echo), "+ Add Script"
disables when every registered script is attached, and scene loading
dedupes as a safety net for hand-edited files.
This commit is contained in:
2026-10-04 00:57:45 +08:00
parent fd2d8f9b34
commit 7d02d344e5
2 changed files with 59 additions and 13 deletions
+11 -1
View File
@@ -28,6 +28,7 @@
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
#include <algorithm>
#include <cmath>
#include <filesystem>
#include <fstream>
@@ -672,7 +673,16 @@ namespace Engine::Serialization {
ECS::ScriptComponent::Item item;
item.name = it["Name"].as<std::string>("");
item.active = it["Active"].as<bool>(true);
sc.items.push_back(std::move(item));
// 去重兜底:同名项只收第一份(UI 已杜绝重复,
// 手编场景仍可能写出重复)
const bool dup = std::any_of(
sc.items.begin(), sc.items.end(),
[&](const ECS::ScriptComponent::Item& e) {
return e.name == item.name;
});
if (!dup) {
sc.items.push_back(std::move(item));
}
}
} else {
// 旧格式:Script: {ScriptName: ..., Active: ...}