More custom fields support

This commit is contained in:
Federico Cecchetto 2021-03-13 15:02:31 +01:00
parent 3659fbeaee
commit ea733bba53
4 changed files with 37 additions and 1 deletions

View File

@ -7,6 +7,7 @@
#include "game/scripting/entity.hpp"
#include "game/scripting/event.hpp"
#include "game/scripting/lua/engine.hpp"
#include "game/scripting/execution.hpp"
#include "scheduler.hpp"
@ -35,6 +36,11 @@ namespace scripting
e.arguments.emplace_back(*value);
}
if (e.name == "connected")
{
scripting::clear_entity_fields(e.entity);
}
lua::engine::notify(e);
}
}

View File

@ -125,7 +125,25 @@ namespace scripting
void set_custom_field(const entity& entity, const std::string& field, const script_value& value)
{
custom_fields[entity.get_entity_id()].insert(std::make_pair(field, value));
const auto id = entity.get_entity_id();
if (custom_fields[id].find(field) != custom_fields[id].end())
{
custom_fields[id][field] = value;
return;
}
custom_fields[id].insert(std::make_pair(field, value));
}
void clear_entity_fields(const entity& entity)
{
const auto id = entity.get_entity_id();
if (custom_fields.find(id) != custom_fields.end())
{
custom_fields[id].clear();
}
}
void clear_custom_fields()

View File

@ -21,6 +21,7 @@ namespace scripting
return call<script_value>(name, arguments).as<T>();
}
void clear_entity_fields(const entity& entity);
void clear_custom_fields();
void set_entity_field(const entity& entity, const std::string& field, const script_value& value);

View File

@ -185,6 +185,17 @@ namespace scripting::lua
return convert(s, entity.call(function, arguments));
};
entity_type[sol::meta_function::new_index] = [](const entity& entity, const std::string& field,
const sol::lua_value& value)
{
entity.set(field, convert(value));
};
entity_type[sol::meta_function::index] = [](const entity& entity, const sol::this_state s, const std::string& field)
{
return convert(s, entity.get(field));
};
struct game
{
};