mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-13 06:51:51 +00:00
Add on_entity_spawned and on_entity_despawned events
This commit is contained in:
parent
936a5c8525
commit
518d5d5ae4
8 changed files with 75 additions and 1 deletions
|
|
@ -180,6 +180,10 @@ rig:get_color() -> vec3
|
||||||
rig:set_color(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
|
## Component events
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
|
|
|
||||||
|
|
@ -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.
|
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)
|
### Chunk Events (world.lua)
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
|
|
|
||||||
|
|
@ -180,6 +180,10 @@ rig:get_color() -> vec3
|
||||||
rig:set_color(color: vec3)
|
rig:set_color(color: vec3)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> [!WARNING]
|
||||||
|
> При выходе сущности за пределы зоны прогрузки она удаляется, вызывая события.
|
||||||
|
> При повторном попадании в зону прогрузки сущность спавнится заново.
|
||||||
|
|
||||||
## События компонента
|
## События компонента
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
|
|
|
||||||
|
|
@ -156,6 +156,18 @@ function on_block_interact(blockid, x, y, z, playerid) -> bool
|
||||||
|
|
||||||
Вызывается при нажатии на блок ПКМ. Предотвращает установку блоков, если возвращает `true`
|
Вызывается при нажатии на блок ПКМ. Предотвращает установку блоков, если возвращает `true`
|
||||||
|
|
||||||
|
```lua
|
||||||
|
function on_entity_spawned(uid)
|
||||||
|
```
|
||||||
|
|
||||||
|
Вызывается после спавна сущности
|
||||||
|
|
||||||
|
```lua
|
||||||
|
function on_entity_despawned(uid)
|
||||||
|
```
|
||||||
|
|
||||||
|
Вызывается после удаления сущности, на момент вызова события сущность с переданным uid уже удалена
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
function on_player_tick(playerid: int, tps: int)
|
function on_player_tick(playerid: int, tps: int)
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -127,6 +127,8 @@ struct WorldFuncsSet {
|
||||||
bool onchunkremove;
|
bool onchunkremove;
|
||||||
bool oninventoryopen;
|
bool oninventoryopen;
|
||||||
bool oninventoryclosed;
|
bool oninventoryclosed;
|
||||||
|
bool onentityspawned;
|
||||||
|
bool onentitydespawned;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ContentPackRuntime {
|
class ContentPackRuntime {
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
bool scripting::on_item_use(Player* player, const ItemDef& item) {
|
||||||
std::string name = item.name + ".use";
|
std::string name = item.name + ".use";
|
||||||
return lua::emit_event(
|
return lua::emit_event(
|
||||||
|
|
@ -781,6 +809,10 @@ void scripting::load_world_script(
|
||||||
register_event(env, "on_inventory_open", prefix + ":.inventoryopen");
|
register_event(env, "on_inventory_open", prefix + ":.inventoryopen");
|
||||||
funcsset.oninventoryclosed =
|
funcsset.oninventoryclosed =
|
||||||
register_event(env, "on_inventory_closed", prefix + ":.inventoryclosed");
|
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(
|
void scripting::load_content_script(
|
||||||
|
|
|
||||||
|
|
@ -112,6 +112,9 @@ namespace scripting {
|
||||||
|
|
||||||
void on_player_tick(Player* player, int tps);
|
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
|
/// @brief Called on RMB click with the item selected
|
||||||
/// @return true if prevents default action
|
/// @return true if prevents default action
|
||||||
bool on_item_use(Player* player, const ItemDef& item);
|
bool on_item_use(Player* player, const ItemDef& item);
|
||||||
|
|
|
||||||
|
|
@ -129,6 +129,8 @@ void scripting::on_entity_spawn(
|
||||||
for (auto& component : components) {
|
for (auto& component : components) {
|
||||||
create_component(L, -1, *component, args, saved);
|
create_component(L, -1, *component, args, saved);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
scripting::on_entity_spawned(eid);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void process_entity_callback(
|
static void process_entity_callback(
|
||||||
|
|
@ -171,9 +173,13 @@ void scripting::on_entity_despawn(const Entity& entity) {
|
||||||
entity, "on_despawn", &EntityFuncsSet::on_despawn, nullptr
|
entity, "on_despawn", &EntityFuncsSet::on_despawn, nullptr
|
||||||
);
|
);
|
||||||
auto L = lua::get_main_state();
|
auto L = lua::get_main_state();
|
||||||
|
entityid_t uid = entity.getUID();
|
||||||
|
|
||||||
lua::get_from(L, "stdcomp", "remove_Entity", true);
|
lua::get_from(L, "stdcomp", "remove_Entity", true);
|
||||||
lua::pushinteger(L, entity.getUID());
|
lua::pushinteger(L, uid);
|
||||||
lua::call(L, 1, 0);
|
lua::call(L, 1, 0);
|
||||||
|
|
||||||
|
scripting::on_entity_despawned(uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_entity_grounded(const Entity& entity, float force) {
|
void scripting::on_entity_grounded(const Entity& entity, float force) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue