diff --git a/src/game/scripting/context_initializer.cpp b/src/game/scripting/context_initializer.cpp index e6a4a95..77f6b98 100644 --- a/src/game/scripting/context_initializer.cpp +++ b/src/game/scripting/context_initializer.cpp @@ -11,9 +11,9 @@ namespace game { const auto chai = context->get_chai(); - chai->add(chaiscript::user_type(), "entity"); - chai->add(chaiscript::constructor(), "entity"); - chai->add(chaiscript::constructor(), "entity"); + chai->add(chaiscript::user_type(), "_entity"); + chai->add(chaiscript::constructor(), "_entity"); + chai->add(chaiscript::constructor(), "_entity"); chai->add(chaiscript::fun([](entity& lhs, const entity& rhs) -> entity& { diff --git a/src/game/scripting/entity.cpp b/src/game/scripting/entity.cpp index ef21002..ea92eb4 100644 --- a/src/game/scripting/entity.cpp +++ b/src/game/scripting/entity.cpp @@ -13,29 +13,60 @@ namespace game { } + entity::entity(entity&& other) noexcept + { + if (&other == this) return; + + this->context_ = other.context_; + this->entity_id_ = other.entity_id_; + + other.context_ = nullptr; + other.entity_id_ = 0; + } + entity::entity(context* context, const unsigned int entity_id) : context_(context), entity_id_(entity_id) { - if (this->entity_id_) - { - native::VariableValue value{}; - value.type = native::SCRIPT_OBJECT; - value.u.entityId = this->entity_id_; - native::AddRefToValue(&value); - } + this->add(); } entity::~entity() { - if (this->entity_id_) + this->release(); + } + + entity& entity::operator=(const entity& other) + { + if (&other != this) { - native::RemoveRefToValue(native::SCRIPT_OBJECT, {static_cast(this->entity_id_)}); + this->release(); + + this->context_ = other.context_; + this->entity_id_ = other.entity_id_; + + this->add(); } + + return *this; + } + + entity& entity::operator=(entity&& other) noexcept + { + if (&other != this) + { + this->context_ = other.context_; + this->entity_id_ = other.entity_id_; + + other.context_ = nullptr; + other.entity_id_ = 0; + } + + return *this; } event_listener_handle entity::on_notify(const std::string& event, - const std::function&)>& - callback, - const bool is_volatile) + const std::function&)>& + callback, + const bool is_volatile) const { event_listener listener; @@ -78,5 +109,24 @@ namespace game { return this->context_->get_executer()->get_entity_field(field, this->get_entity_id()); } + + void entity::add() const + { + if (this->entity_id_) + { + native::VariableValue value{}; + value.type = native::SCRIPT_OBJECT; + value.u.entityId = this->entity_id_; + native::AddRefToValue(&value); + } + } + + void entity::release() const + { + if (this->entity_id_) + { + native::RemoveRefToValue(native::SCRIPT_OBJECT, {static_cast(this->entity_id_)}); + } + } } } diff --git a/src/game/scripting/entity.hpp b/src/game/scripting/entity.hpp index a8fa8a0..e636ad6 100644 --- a/src/game/scripting/entity.hpp +++ b/src/game/scripting/entity.hpp @@ -13,9 +13,13 @@ namespace game public: entity(); entity(const entity& other); + entity(entity&& other) noexcept; entity(context* context, unsigned int entity_id); ~entity(); + entity& operator=(const entity& other); + entity& operator=(entity&& other) noexcept; + event_listener_handle on_notify(const std::string& event, const std::function&)>& callback, bool is_volatile) const; @@ -33,6 +37,9 @@ namespace game private: context* context_; unsigned int entity_id_; + + void add() const; + void release() const; }; } } diff --git a/src/game/scripting/event_handler.cpp b/src/game/scripting/event_handler.cpp index e9ce927..cc3b97f 100644 --- a/src/game/scripting/event_handler.cpp +++ b/src/game/scripting/event_handler.cpp @@ -9,9 +9,9 @@ namespace game { const auto chai = this->context_->get_chai(); - chai->add(chaiscript::user_type(), "event_listener_handle"); - chai->add(chaiscript::constructor(), "event_listener_handle"); - chai->add(chaiscript::constructor(), "event_listener_handle"); + chai->add(chaiscript::user_type(), "_event_listener_handle"); + chai->add(chaiscript::constructor(), "_event_listener_handle"); + chai->add(chaiscript::constructor(), "_event_listener_handle"); chai->add(chaiscript::fun([](event_listener_handle& lhs, const event_listener_handle& rhs) -> event_listener_handle& { diff --git a/src/game/scripting/scheduler.cpp b/src/game/scripting/scheduler.cpp index b9b7513..4c99ab1 100644 --- a/src/game/scripting/scheduler.cpp +++ b/src/game/scripting/scheduler.cpp @@ -9,9 +9,9 @@ namespace game { const auto chai = this->context_->get_chai(); - chai->add(chaiscript::user_type(), "task_handle"); - chai->add(chaiscript::constructor(), "task_handle"); - chai->add(chaiscript::constructor(), "task_handle"); + chai->add(chaiscript::user_type(), "_task_handle"); + chai->add(chaiscript::constructor(), "_task_handle"); + chai->add(chaiscript::constructor(), "_task_handle"); chai->add(chaiscript::fun([](task_handle& lhs, const task_handle& rhs) -> task_handle& {