add entity on_save event and SAVED_DATA variable
This commit is contained in:
parent
e301dbe164
commit
08cc78289d
@ -12,6 +12,11 @@ local rotation = mat4.rotate({
|
|||||||
math.random(), math.random(), math.random()
|
math.random(), math.random(), math.random()
|
||||||
}, 360)
|
}, 360)
|
||||||
|
|
||||||
|
function on_save()
|
||||||
|
SAVED_DATA.test = 5
|
||||||
|
print("SAVE ENTITY")
|
||||||
|
end
|
||||||
|
|
||||||
do -- setup visuals
|
do -- setup visuals
|
||||||
local matrix = mat4.idt()
|
local matrix = mat4.idt()
|
||||||
local icon = item.icon(dropitem.id)
|
local icon = item.icon(dropitem.id)
|
||||||
|
|||||||
@ -263,6 +263,15 @@ bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x,
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dynamic::Value scripting::get_component_value(const scriptenv& env, const std::string& name) {
|
||||||
|
auto L = lua::get_main_thread();
|
||||||
|
lua::pushenv(L, *env);
|
||||||
|
if (lua::getfield(L, name)) {
|
||||||
|
return lua::tovalue(L, -1);
|
||||||
|
}
|
||||||
|
return dynamic::NONE;
|
||||||
|
}
|
||||||
|
|
||||||
void scripting::on_entity_spawn(
|
void scripting::on_entity_spawn(
|
||||||
const EntityDef& def,
|
const EntityDef& def,
|
||||||
entityid_t eid,
|
entityid_t eid,
|
||||||
@ -279,12 +288,17 @@ void scripting::on_entity_spawn(
|
|||||||
lua::pushvalue(L, -1);
|
lua::pushvalue(L, -1);
|
||||||
}
|
}
|
||||||
for (auto& component : components) {
|
for (auto& component : components) {
|
||||||
auto compenv = create_component_environment(
|
auto compenv = create_component_environment(get_root_environment(), -1,
|
||||||
get_root_environment(), -1, component->name);
|
component->name);
|
||||||
lua::get_from(L, lua::CHUNKS_TABLE, component->name, true);
|
lua::get_from(L, lua::CHUNKS_TABLE, component->name, true);
|
||||||
lua::pushenv(L, *compenv);
|
lua::pushenv(L, *compenv);
|
||||||
|
|
||||||
lua::pushvalue(L, args);
|
lua::pushvalue(L, args);
|
||||||
lua::setfield(L, "ARGS");
|
lua::setfield(L, "ARGS");
|
||||||
|
|
||||||
|
lua::createtable(L, 0, 0);
|
||||||
|
lua::setfield(L, "SAVED_DATA");
|
||||||
|
|
||||||
lua::setfenv(L);
|
lua::setfenv(L);
|
||||||
lua::call_nothrow(L, 0, 0);
|
lua::call_nothrow(L, 0, 0);
|
||||||
|
|
||||||
@ -379,7 +393,6 @@ void scripting::on_trigger_enter(const Entity& entity, size_t index, entityid_t
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void scripting::on_trigger_exit(const Entity& entity, size_t index, entityid_t oid) {
|
void scripting::on_trigger_exit(const Entity& entity, size_t index, entityid_t oid) {
|
||||||
const auto& script = entity.getScripting();
|
const auto& script = entity.getScripting();
|
||||||
for (auto& component : script.components) {
|
for (auto& component : script.components) {
|
||||||
|
|||||||
@ -76,6 +76,7 @@ namespace scripting {
|
|||||||
/// @return true if prevents default action
|
/// @return true if prevents default action
|
||||||
bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z);
|
bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z);
|
||||||
|
|
||||||
|
dynamic::Value get_component_value(const scriptenv& env, const std::string& name);
|
||||||
void on_entity_spawn(
|
void on_entity_spawn(
|
||||||
const EntityDef& def,
|
const EntityDef& def,
|
||||||
entityid_t eid,
|
entityid_t eid,
|
||||||
|
|||||||
@ -112,6 +112,10 @@ void Entities::despawn(entityid_t id) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Entities::onSave(const Entity& entity) {
|
||||||
|
scripting::on_entity_save(entity);
|
||||||
|
}
|
||||||
|
|
||||||
dynamic::Value Entities::serialize(const Entity& entity) {
|
dynamic::Value Entities::serialize(const Entity& entity) {
|
||||||
auto root = dynamic::create_map();
|
auto root = dynamic::create_map();
|
||||||
auto& eid = entity.getID();
|
auto& eid = entity.getID();
|
||||||
@ -140,7 +144,6 @@ dynamic::Value Entities::serialize(const Entity& entity) {
|
|||||||
if (rig.config->getName() != def.rigName) {
|
if (rig.config->getName() != def.rigName) {
|
||||||
root->put("rig", rig.config->getName());
|
root->put("rig", rig.config->getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (def.save.rig.pose || def.save.rig.textures) {
|
if (def.save.rig.pose || def.save.rig.textures) {
|
||||||
auto& rigmap = root->putMap("rig");
|
auto& rigmap = root->putMap("rig");
|
||||||
if (def.save.rig.textures) {
|
if (def.save.rig.textures) {
|
||||||
@ -156,6 +159,14 @@ dynamic::Value Entities::serialize(const Entity& entity) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
auto& scripts = entity.getScripting();
|
||||||
|
if (!scripts.components.empty()) {
|
||||||
|
auto& compsMap = root->putMap("comps");
|
||||||
|
for (auto& comp : scripts.components) {
|
||||||
|
auto data = scripting::get_component_value(comp->env, "SAVED_DATA");
|
||||||
|
compsMap.put(comp->name, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -183,6 +183,7 @@ public:
|
|||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void onSave(const Entity& entity);
|
||||||
std::vector<Entity> getAllInside(AABB aabb);
|
std::vector<Entity> getAllInside(AABB aabb);
|
||||||
void despawn(entityid_t id);
|
void despawn(entityid_t id);
|
||||||
dynamic::Value serialize(const Entity& entity);
|
dynamic::Value serialize(const Entity& entity);
|
||||||
|
|||||||
@ -713,6 +713,7 @@ void Chunks::save(Chunk* chunk) {
|
|||||||
auto root = dynamic::create_map();
|
auto root = dynamic::create_map();
|
||||||
auto& list = root->putList("data");
|
auto& list = root->putList("data");
|
||||||
for (auto& entity : entities) {
|
for (auto& entity : entities) {
|
||||||
|
level->entities->onSave(entity);
|
||||||
list.put(level->entities->serialize(entity));
|
list.put(level->entities->serialize(entity));
|
||||||
}
|
}
|
||||||
if (!entities.empty()) {
|
if (!entities.empty()) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user