Fix entity assignment
This commit is contained in:
parent
f497338f7b
commit
99a85c73d8
@ -11,9 +11,9 @@ namespace game
|
||||
{
|
||||
const auto chai = context->get_chai();
|
||||
|
||||
chai->add(chaiscript::user_type<entity>(), "entity");
|
||||
chai->add(chaiscript::constructor<entity()>(), "entity");
|
||||
chai->add(chaiscript::constructor<entity(const entity&)>(), "entity");
|
||||
chai->add(chaiscript::user_type<entity>(), "_entity");
|
||||
chai->add(chaiscript::constructor<entity()>(), "_entity");
|
||||
chai->add(chaiscript::constructor<entity(const entity&)>(), "_entity");
|
||||
|
||||
chai->add(chaiscript::fun([](entity& lhs, const entity& rhs) -> entity&
|
||||
{
|
||||
|
@ -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)
|
||||
{
|
||||
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_)
|
||||
{
|
||||
native::RemoveRefToValue(native::SCRIPT_OBJECT, {static_cast<int>(this->entity_id_)});
|
||||
this->release();
|
||||
}
|
||||
|
||||
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,
|
||||
@ -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<int>(this->entity_id_)});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<void(const std::vector<chaiscript::Boxed_Value>&)>& callback,
|
||||
bool is_volatile) const;
|
||||
@ -33,6 +37,9 @@ namespace game
|
||||
private:
|
||||
context* context_;
|
||||
unsigned int entity_id_;
|
||||
|
||||
void add() const;
|
||||
void release() const;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -9,9 +9,9 @@ namespace game
|
||||
{
|
||||
const auto chai = this->context_->get_chai();
|
||||
|
||||
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(const 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(const event_listener_handle&)>(), "_event_listener_handle");
|
||||
|
||||
chai->add(chaiscript::fun([](event_listener_handle& lhs, const event_listener_handle& rhs) -> event_listener_handle&
|
||||
{
|
||||
|
@ -9,9 +9,9 @@ namespace game
|
||||
{
|
||||
const auto chai = this->context_->get_chai();
|
||||
|
||||
chai->add(chaiscript::user_type<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::user_type<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::fun([](task_handle& lhs, const task_handle& rhs) -> task_handle&
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user