mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
Merge branch 'dev' of https://github.com/MihailRis/voxelcore into dev
This commit is contained in:
commit
5ee54c666a
7 changed files with 61 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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_spawn(uid)
|
||||
```
|
||||
Called after an entity is spawned.
|
||||
|
||||
```lua
|
||||
function on_entity_despawn(uid)
|
||||
```
|
||||
|
||||
Called before an entity is despawned.
|
||||
|
||||
### Chunk Events (world.lua)
|
||||
|
||||
```lua
|
||||
|
|
|
|||
|
|
@ -180,6 +180,10 @@ rig:get_color() -> vec3
|
|||
rig:set_color(color: vec3)
|
||||
```
|
||||
|
||||
> [!WARNING]
|
||||
> При выходе сущности за пределы зоны прогрузки она удаляется, вызывая события.
|
||||
> При повторном попадании в зону прогрузки сущность спавнится заново.
|
||||
|
||||
## События компонента
|
||||
|
||||
```lua
|
||||
|
|
|
|||
|
|
@ -156,6 +156,18 @@ function on_block_interact(blockid, x, y, z, playerid) -> bool
|
|||
|
||||
Вызывается при нажатии на блок ПКМ. Предотвращает установку блоков, если возвращает `true`
|
||||
|
||||
```lua
|
||||
function on_entity_spawn(uid)
|
||||
```
|
||||
|
||||
Вызывается после спавна сущности
|
||||
|
||||
```lua
|
||||
function on_entity_despawn(uid)
|
||||
```
|
||||
|
||||
Вызывается перед удалением сущности
|
||||
|
||||
```lua
|
||||
function on_player_tick(playerid: int, tps: int)
|
||||
```
|
||||
|
|
|
|||
|
|
@ -127,6 +127,8 @@ struct WorldFuncsSet {
|
|||
bool onchunkremove;
|
||||
bool oninventoryopen;
|
||||
bool oninventoryclosed;
|
||||
bool onentityspawn;
|
||||
bool onentitydespawn;
|
||||
};
|
||||
|
||||
class ContentPackRuntime {
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue