diff --git a/Source/Engine/Level/Actor.cpp b/Source/Engine/Level/Actor.cpp index 94e829991..c7b5dc34a 100644 --- a/Source/Engine/Level/Actor.cpp +++ b/Source/Engine/Level/Actor.cpp @@ -190,10 +190,10 @@ void Actor::OnDeleteObject() #endif for (int32 i = 0; i < Children.Count(); i++) { - auto child = Children[i]; - ASSERT(child->_parent == this); - child->_parent = nullptr; - child->DeleteObject(); + auto e = Children[i]; + ASSERT(e->_parent == this); + e->_parent = nullptr; + e->DeleteObject(); } #if BUILD_DEBUG ASSERT(callsCheck == Children.Count()); @@ -206,10 +206,10 @@ void Actor::OnDeleteObject() #endif for (int32 i = 0; i < Scripts.Count(); i++) { - auto script = Scripts[i]; - ASSERT(script->_parent == this); - script->_parent = nullptr; - script->DeleteObject(); + auto e = Scripts[i]; + ASSERT(e->_parent == this); + e->_parent = nullptr; + e->DeleteObject(); } #if BUILD_DEBUG ASSERT(callsCheck == Scripts.Count()); @@ -401,8 +401,9 @@ Actor* Actor::GetChild(const StringView& name) const { for (int32 i = 0; i < Children.Count(); i++) { - if (Children[i]->GetName() == name) - return Children[i]; + auto e = Children.Get()[i]; + if (e->GetName() == name) + return e; } return nullptr; } @@ -852,15 +853,17 @@ void Actor::BeginPlay(SceneBeginData* data) // Update scripts for (int32 i = 0; i < Scripts.Count(); i++) { - if (!Scripts[i]->IsDuringPlay()) - Scripts[i]->BeginPlay(data); + auto e = Scripts.Get()[i]; + if (!e->IsDuringPlay()) + e->BeginPlay(data); } // Update children for (int32 i = 0; i < Children.Count(); i++) { - if (!Children[i]->IsDuringPlay()) - Children[i]->BeginPlay(data); + auto e = Children.Get()[i]; + if (!e->IsDuringPlay()) + e->BeginPlay(data); } // Fire events for scripting @@ -891,8 +894,9 @@ void Actor::EndPlay() // Call event deeper for (int32 i = 0; i < Children.Count(); i++) { - if (Children[i]->IsDuringPlay()) - Children[i]->EndPlay(); + auto e = Children.Get()[i]; + if (e->IsDuringPlay()) + e->EndPlay(); } // Fire event for scripting @@ -907,8 +911,9 @@ void Actor::EndPlay() // Inform attached scripts for (int32 i = 0; i < Scripts.Count(); i++) { - if (Scripts[i]->IsDuringPlay()) - Scripts[i]->EndPlay(); + auto e = Scripts.Get()[i]; + if (e->IsDuringPlay()) + e->EndPlay(); } // Cleanup managed object diff --git a/Source/Engine/Scripting/Scripting.cpp b/Source/Engine/Scripting/Scripting.cpp index c78bc01d0..34cc83054 100644 --- a/Source/Engine/Scripting/Scripting.cpp +++ b/Source/Engine/Scripting/Scripting.cpp @@ -821,6 +821,28 @@ FLAXENGINE_API ScriptingObject* FindObject(const Guid& id, MClass* type) return Scripting::FindObject(id, type); } +void ScriptingObjectReferenceBase::OnSet(ScriptingObject* object) +{ + auto e = _object; + if (e != object) + { + if (e) + e->Deleted.Unbind(this); + _object = e = object; + if (e) + e->Deleted.Bind(this); + Changed(); + } +} + +void ScriptingObjectReferenceBase::OnDeleted(ScriptingObject* obj) +{ + ASSERT(_object == obj); + _object->Deleted.Unbind(this); + _object = nullptr; + Changed(); +} + ScriptingObject* Scripting::FindObject(Guid id, MClass* type) { if (!id.IsValid()) diff --git a/Source/Engine/Scripting/ScriptingObjectReference.h b/Source/Engine/Scripting/ScriptingObjectReference.h index 5fcf6c854..0012c89ba 100644 --- a/Source/Engine/Scripting/ScriptingObjectReference.h +++ b/Source/Engine/Scripting/ScriptingObjectReference.h @@ -14,22 +14,18 @@ extern FLAXENGINE_API ScriptingObject* FindObject(const Guid& id, MClass* type); class FLAXENGINE_API ScriptingObjectReferenceBase { public: - typedef Delegate<> EventType; protected: - ScriptingObject* _object = nullptr; public: - /// /// Action fired when reference gets changed. /// EventType Changed; public: - /// /// Initializes a new instance of the class. /// @@ -56,7 +52,6 @@ public: } public: - /// /// Gets the object ID. /// @@ -90,32 +85,13 @@ public: } protected: - /// /// Sets the object. /// /// The object. - void OnSet(ScriptingObject* object) - { - auto e = _object; - if (e != object) - { - if (e) - e->Deleted.Unbind(this); - _object = e = object; - if (e) - e->Deleted.Bind(this); - Changed(); - } - } + void OnSet(ScriptingObject* object); - void OnDeleted(ScriptingObject* obj) - { - ASSERT(_object == obj); - _object->Deleted.Unbind(this); - _object = nullptr; - Changed(); - } + void OnDeleted(ScriptingObject* obj); }; /// @@ -125,11 +101,9 @@ template API_CLASS(InBuild) class ScriptingObjectReference : public ScriptingObjectReferenceBase { public: - typedef ScriptingObjectReference Type; public: - /// /// Initializes a new instance of the class. /// @@ -163,19 +137,21 @@ public: } public: - FORCE_INLINE bool operator==(T* other) const { return _object == other; } + FORCE_INLINE bool operator!=(T* other) const { return _object != other; } + FORCE_INLINE bool operator==(const ScriptingObjectReference& other) const { return _object == other._object; } + FORCE_INLINE bool operator!=(const ScriptingObjectReference& other) const { return _object != other._object; @@ -186,11 +162,13 @@ public: OnSet(other); return *this; } + ScriptingObjectReference& operator=(const ScriptingObjectReference& other) { OnSet(other._object); return *this; } + FORCE_INLINE ScriptingObjectReference& operator=(const Guid& id) { OnSet(static_cast(FindObject(id, T::GetStaticClass())));