fix user component initialization exception handling & fix fatal error on uninitialized component access

This commit is contained in:
MihailRis 2026-07-26 15:27:38 +03:00
parent 4295af15cc
commit 28385f236c
2 changed files with 12 additions and 1 deletions

View file

@ -148,7 +148,12 @@ void scripting::on_entity_spawn(
lua::call(L, 1);
}
for (auto& component : components) {
create_component(L, -1, *component, args, saved);
try {
create_component(L, -1, *component, args, saved);
} catch (const std::runtime_error& err) {
logger.error() << "could not to initialize user component "
<< component->name << ": " << err.what();
}
}
for (auto& [packid, pack] : content->getPacks()) {
@ -166,6 +171,9 @@ static void process_entity_callback(
const std::string& name,
std::function<int(lua::State*)> args
) {
if (env == nullptr) {
return;
}
auto L = lua::get_main_state();
lua::pushenv(L, *env);
if (lua::hasfield(L, "__disabled")) {

View file

@ -71,6 +71,9 @@ dv::value Entity::serialize() const {
if (!scripts.components.empty()) {
auto& compsMap = root.object("comps");
for (auto& comp : scripts.components) {
if (comp->env == nullptr) {
continue;
}
auto data =
scripting::get_component_value(comp->env, SAVED_DATA_VARNAME);
compsMap[comp->name] = data;