Minor fixes

This commit is contained in:
Wojtek Figat
2023-10-01 10:56:30 +02:00
parent af3f6d0003
commit 075f40b93a
3 changed files with 52 additions and 47 deletions

View File

@@ -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

View File

@@ -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<ScriptingObjectReferenceBase, &ScriptingObjectReferenceBase::OnDeleted>(this);
_object = e = object;
if (e)
e->Deleted.Bind<ScriptingObjectReferenceBase, &ScriptingObjectReferenceBase::OnDeleted>(this);
Changed();
}
}
void ScriptingObjectReferenceBase::OnDeleted(ScriptingObject* obj)
{
ASSERT(_object == obj);
_object->Deleted.Unbind<ScriptingObjectReferenceBase, &ScriptingObjectReferenceBase::OnDeleted>(this);
_object = nullptr;
Changed();
}
ScriptingObject* Scripting::FindObject(Guid id, MClass* type)
{
if (!id.IsValid())

View File

@@ -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:
/// <summary>
/// Action fired when reference gets changed.
/// </summary>
EventType Changed;
public:
/// <summary>
/// Initializes a new instance of the <see cref="ScriptingObjectReferenceBase"/> class.
/// </summary>
@@ -56,7 +52,6 @@ public:
}
public:
/// <summary>
/// Gets the object ID.
/// </summary>
@@ -90,32 +85,13 @@ public:
}
protected:
/// <summary>
/// Sets the object.
/// </summary>
/// <param name="object">The object.</param>
void OnSet(ScriptingObject* object)
{
auto e = _object;
if (e != object)
{
if (e)
e->Deleted.Unbind<ScriptingObjectReferenceBase, &ScriptingObjectReferenceBase::OnDeleted>(this);
_object = e = object;
if (e)
e->Deleted.Bind<ScriptingObjectReferenceBase, &ScriptingObjectReferenceBase::OnDeleted>(this);
Changed();
}
}
void OnSet(ScriptingObject* object);
void OnDeleted(ScriptingObject* obj)
{
ASSERT(_object == obj);
_object->Deleted.Unbind<ScriptingObjectReferenceBase, &ScriptingObjectReferenceBase::OnDeleted>(this);
_object = nullptr;
Changed();
}
void OnDeleted(ScriptingObject* obj);
};
/// <summary>
@@ -125,11 +101,9 @@ template<typename T>
API_CLASS(InBuild) class ScriptingObjectReference : public ScriptingObjectReferenceBase
{
public:
typedef ScriptingObjectReference<T> Type;
public:
/// <summary>
/// Initializes a new instance of the <see cref="ScriptingObjectReference"/> class.
/// </summary>
@@ -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<ScriptingObject*>(FindObject(id, T::GetStaticClass())));