Add on_entity_spawned and on_entity_despawned events

This commit is contained in:
Xertis 2026-04-04 01:53:49 +03:00
parent 936a5c8525
commit 518d5d5ae4
8 changed files with 75 additions and 1 deletions

View file

@ -180,6 +180,10 @@ rig:get_color() -> vec3
rig:set_color(color: vec3)
```
> [!WARNING]
> When an entity moves outside the loaded area, it is removed, triggering the event.
> When it re-enters the loaded area, the entity is spawned again.
## Component events
```lua

View file

@ -152,6 +152,17 @@ function on_block_interact(blockid, x, y, z, playerid) -> bool
Called on block RMB click interaction. Prevents block placing if **true** returned.
```lua
function on_entity_spawned(uid)
```
Called after an entity is spawned.
```lua
function on_entity_despawned(uid)
```
Called after an entity is removed. At the time of the call, the entity with the given uid has already been deleted.
### Chunk Events (world.lua)
```lua

View file

@ -180,6 +180,10 @@ rig:get_color() -> vec3
rig:set_color(color: vec3)
```
> [!WARNING]
> При выходе сущности за пределы зоны прогрузки она удаляется, вызывая события.
> При повторном попадании в зону прогрузки сущность спавнится заново.
## События компонента
```lua

View file

@ -156,6 +156,18 @@ function on_block_interact(blockid, x, y, z, playerid) -> bool
Вызывается при нажатии на блок ПКМ. Предотвращает установку блоков, если возвращает `true`
```lua
function on_entity_spawned(uid)
```
Вызывается после спавна сущности
```lua
function on_entity_despawned(uid)
```
Вызывается после удаления сущности, на момент вызова события сущность с переданным uid уже удалена
```lua
function on_player_tick(playerid: int, tps: int)
```

View file

@ -127,6 +127,8 @@ struct WorldFuncsSet {
bool onchunkremove;
bool oninventoryopen;
bool oninventoryclosed;
bool onentityspawned;
bool onentitydespawned;
};
class ContentPackRuntime {

View file

@ -555,6 +555,34 @@ void scripting::on_player_tick(Player* player, int tps) {
}
}
void scripting::on_entity_spawned(entityid_t eid) {
auto L = lua::get_main_state();
for (auto& pack : content_control->getAllContentPacks()) {
lua::emit_event(
L,
pack.id + ":.entityspawned",
[&](lua::State* L) {
lua::pushinteger(L, eid);
return 1;
}
);
}
}
void scripting::on_entity_despawned(entityid_t eid) {
auto L = lua::get_main_state();
for (auto& pack : content_control->getAllContentPacks()) {
lua::emit_event(
L,
pack.id + ":.entitydespawned",
[&](lua::State* L) {
lua::pushinteger(L, eid);
return 1;
}
);
}
}
bool scripting::on_item_use(Player* player, const ItemDef& item) {
std::string name = item.name + ".use";
return lua::emit_event(
@ -781,6 +809,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.onentityspawned =
register_event(env, "on_entity_spawned", prefix + ":.entityspawned");
funcsset.onentitydespawned =
register_event(env, "on_entity_despawned", prefix + ":.entitydespawned");
}
void scripting::load_content_script(

View file

@ -112,6 +112,9 @@ namespace scripting {
void on_player_tick(Player* player, int tps);
void on_entity_spawned(entityid_t eid);
void on_entity_despawned(entityid_t eid);
/// @brief Called on RMB click with the item selected
/// @return true if prevents default action
bool on_item_use(Player* player, const ItemDef& item);

View file

@ -129,6 +129,8 @@ void scripting::on_entity_spawn(
for (auto& component : components) {
create_component(L, -1, *component, args, saved);
}
scripting::on_entity_spawned(eid);
}
static void process_entity_callback(
@ -171,9 +173,13 @@ void scripting::on_entity_despawn(const Entity& entity) {
entity, "on_despawn", &EntityFuncsSet::on_despawn, nullptr
);
auto L = lua::get_main_state();
entityid_t uid = entity.getUID();
lua::get_from(L, "stdcomp", "remove_Entity", true);
lua::pushinteger(L, entity.getUID());
lua::pushinteger(L, uid);
lua::call(L, 1, 0);
scripting::on_entity_despawned(uid);
}
void scripting::on_entity_grounded(const Entity& entity, float force) {