Fix crash when using Actor GetScript or GetChild with invalid index

#462
This commit is contained in:
Wojtek Figat
2021-04-20 10:12:57 +02:00
parent 6387e87a30
commit 20331bc858
2 changed files with 14 additions and 9 deletions

View File

@@ -347,6 +347,12 @@ void Actor::SetOrderInParent(int32 index)
}
}
Actor* Actor::GetChild(int32 index) const
{
CHECK_RETURN(index >= 0 && index < Children.Count(), nullptr);
return Children[index];
}
Actor* Actor::GetChild(const StringView& name) const
{
for (int32 i = 0; i < Children.Count(); i++)
@@ -354,7 +360,6 @@ Actor* Actor::GetChild(const StringView& name) const
if (Children[i]->GetName() == name)
return Children[i];
}
return nullptr;
}
@@ -482,6 +487,12 @@ void Actor::SetName(const StringView& value)
Level::callActorEvent(Level::ActorEventType::OnActorNameChanged, this, nullptr);
}
Script* Actor::GetScript(int32 index) const
{
CHECK_RETURN(index >= 0 && index < Scripts.Count(), nullptr);
return Scripts[index];
}
Script* Actor::GetScript(const MClass* type) const
{
CHECK_RETURN(type, nullptr);

View File

@@ -192,10 +192,7 @@ public:
/// </summary>
/// <param name="index">The child actor index.</param>
/// <returns>The child actor (always valid).</returns>
API_FUNCTION() FORCE_INLINE Actor* GetChild(int32 index) const
{
return Children[index];
}
API_FUNCTION() Actor* GetChild(int32 index) const;
/// <summary>
/// Gets the child actor with the given name.
@@ -266,10 +263,7 @@ public:
/// </summary>
/// <param name="index">The script index.</param>
/// <returns>The script (always valid).</returns>
API_FUNCTION() FORCE_INLINE Script* GetScript(int32 index) const
{
return Scripts[index];
}
API_FUNCTION() Script* GetScript(int32 index) const;
/// <summary>
/// Gets the script of the given type from this actor.