Fix entity assignment

This commit is contained in:
momo5502 2019-01-21 23:21:38 +01:00
parent f497338f7b
commit 99a85c73d8
5 changed files with 78 additions and 21 deletions

View File

@ -11,9 +11,9 @@ namespace game
{ {
const auto chai = context->get_chai(); const auto chai = context->get_chai();
chai->add(chaiscript::user_type<entity>(), "entity"); chai->add(chaiscript::user_type<entity>(), "_entity");
chai->add(chaiscript::constructor<entity()>(), "entity"); chai->add(chaiscript::constructor<entity()>(), "_entity");
chai->add(chaiscript::constructor<entity(const entity&)>(), "entity"); chai->add(chaiscript::constructor<entity(const entity&)>(), "_entity");
chai->add(chaiscript::fun([](entity& lhs, const entity& rhs) -> entity& chai->add(chaiscript::fun([](entity& lhs, const entity& rhs) -> entity&
{ {

View File

@ -13,23 +13,54 @@ 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) entity::entity(context* context, const unsigned int entity_id) : context_(context), entity_id_(entity_id)
{ {
if (this->entity_id_) this->add();
{
native::VariableValue value{};
value.type = native::SCRIPT_OBJECT;
value.u.entityId = this->entity_id_;
native::AddRefToValue(&value);
}
} }
entity::~entity() entity::~entity()
{ {
if (this->entity_id_) this->release();
{
native::RemoveRefToValue(native::SCRIPT_OBJECT, {static_cast<int>(this->entity_id_)});
} }
entity& entity::operator=(const entity& other)
{
if (&other != this)
{
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, event_listener_handle entity::on_notify(const std::string& event,
@ -78,5 +109,24 @@ namespace game
{ {
return this->context_->get_executer()->get_entity_field(field, this->get_entity_id()); 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<int>(this->entity_id_)});
}
}
} }
} }

View File

@ -13,9 +13,13 @@ namespace game
public: public:
entity(); entity();
entity(const entity& other); entity(const entity& other);
entity(entity&& other) noexcept;
entity(context* context, unsigned int entity_id); entity(context* context, unsigned int entity_id);
~entity(); ~entity();
entity& operator=(const entity& other);
entity& operator=(entity&& other) noexcept;
event_listener_handle on_notify(const std::string& event, event_listener_handle on_notify(const std::string& event,
const std::function<void(const std::vector<chaiscript::Boxed_Value>&)>& callback, const std::function<void(const std::vector<chaiscript::Boxed_Value>&)>& callback,
bool is_volatile) const; bool is_volatile) const;
@ -33,6 +37,9 @@ namespace game
private: private:
context* context_; context* context_;
unsigned int entity_id_; unsigned int entity_id_;
void add() const;
void release() const;
}; };
} }
} }

View File

@ -9,9 +9,9 @@ namespace game
{ {
const auto chai = this->context_->get_chai(); const auto chai = this->context_->get_chai();
chai->add(chaiscript::user_type<event_listener_handle>(), "event_listener_handle"); chai->add(chaiscript::user_type<event_listener_handle>(), "_event_listener_handle");
chai->add(chaiscript::constructor<event_listener_handle()>(), "event_listener_handle"); chai->add(chaiscript::constructor<event_listener_handle()>(), "_event_listener_handle");
chai->add(chaiscript::constructor<event_listener_handle(const event_listener_handle&)>(), "event_listener_handle"); chai->add(chaiscript::constructor<event_listener_handle(const event_listener_handle&)>(), "_event_listener_handle");
chai->add(chaiscript::fun([](event_listener_handle& lhs, const event_listener_handle& rhs) -> event_listener_handle& chai->add(chaiscript::fun([](event_listener_handle& lhs, const event_listener_handle& rhs) -> event_listener_handle&
{ {

View File

@ -9,9 +9,9 @@ namespace game
{ {
const auto chai = this->context_->get_chai(); const auto chai = this->context_->get_chai();
chai->add(chaiscript::user_type<task_handle>(), "task_handle"); chai->add(chaiscript::user_type<task_handle>(), "_task_handle");
chai->add(chaiscript::constructor<task_handle()>(), "task_handle"); chai->add(chaiscript::constructor<task_handle()>(), "_task_handle");
chai->add(chaiscript::constructor<task_handle(const task_handle&)>(), "task_handle"); chai->add(chaiscript::constructor<task_handle(const task_handle&)>(), "_task_handle");
chai->add(chaiscript::fun([](task_handle& lhs, const task_handle& rhs) -> task_handle& chai->add(chaiscript::fun([](task_handle& lhs, const task_handle& rhs) -> task_handle&
{ {