Merge pull request #826 from Xertis/dev

Add on_entity_spawned and on_entity_despawned events
This commit is contained in:
MihailRis 2026-04-08 23:32:01 +03:00 • committed by GitHub
commit ce32a25d31
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 61 additions and 1 deletions

View file

@ -781,6 +781,10 @@ void scripting::load_world_script(
register_event(env, "on_inventory_open", prefix + ":.inventoryopen");
funcsset.oninventoryclosed =
register_event(env, "on_inventory_closed", prefix + ":.inventoryclosed");
funcsset.onentityspawn =
register_event(env, "on_entity_spawn", prefix + ":.entityspawn");
funcsset.onentitydespawn =
register_event(env, "on_entity_despawn", prefix + ":.entitydespawn");
}
void scripting::load_content_script(

View file

@ -6,6 +6,8 @@
#include "objects/Entity.hpp"
#include "objects/Player.hpp"
#include "util/stringutil.hpp"
#include "content/Content.hpp"
#include "content/ContentPack.hpp"
using namespace scripting;
@ -129,6 +131,15 @@ void scripting::on_entity_spawn(
for (auto& component : components) {
create_component(L, -1, *component, args, saved);
}
for (auto& [packid, pack] : content->getPacks()) {
if (pack->worldfuncsset.onentityspawn) {
lua::emit_event(L, packid + ":.entityspawn", [&](lua::State* L) {
lua::pushinteger(L, eid);
return 1;
});
}
}
}
static void process_entity_callback(
@ -170,9 +181,21 @@ void scripting::on_entity_despawn(const Entity& entity) {
process_entity_callback(
entity, "on_despawn", &EntityFuncsSet::on_despawn, nullptr
);
auto L = lua::get_main_state();
entityid_t uid = entity.getUID();
for (auto& [packid, pack] : content->getPacks()) {
if (pack->worldfuncsset.onentitydespawn) {
lua::emit_event(L, packid + ":.entitydespawn", [&](lua::State* L) {
lua::pushinteger(L, uid);
return 1;
});
}
}
lua::get_from(L, "stdcomp", "remove_Entity", true);
lua::pushinteger(L, entity.getUID());
lua::pushinteger(L, uid);
lua::call(L, 1, 0);
}