Fix using Scripts in async

This commit is contained in:
Wojtek Figat
2021-03-01 10:42:45 +01:00
parent a7bb25b7d5
commit dbec1389e8

View File

@@ -77,14 +77,12 @@ void Script::SetParent(Actor* value, bool canBreakPrefabLink)
// Check if value won't change // Check if value won't change
if (_parent == value) if (_parent == value)
return; return;
if (!IsInMainThread()) if (IsDuringPlay() && !IsInMainThread())
{ {
LOG(Error, "Editing scene hierarchy is only allowed on a main thread."); LOG(Error, "Editing scene hierarchy is only allowed on a main thread.");
return; return;
} }
Level::ScenesLock.Lock();
// Unlink from the old one // Unlink from the old one
if (_parent) if (_parent)
{ {
@@ -107,8 +105,6 @@ void Script::SetParent(Actor* value, bool canBreakPrefabLink)
_parent->Scripts.Add(this); _parent->Scripts.Add(this);
} }
Level::ScenesLock.Unlock();
// Break prefab link for prefab instance objects // Break prefab link for prefab instance objects
if (HasPrefabLink() && IsDuringPlay() && canBreakPrefabLink) if (HasPrefabLink() && IsDuringPlay() && canBreakPrefabLink)
{ {
@@ -137,7 +133,7 @@ void Script::SetParent(Actor* value, bool canBreakPrefabLink)
Enable(); Enable();
} }
} }
else if (!previous && value->IsDuringPlay() && value->IsActiveInHierarchy() && GetEnabled()) else if (!previous && value && value->IsDuringPlay() && value->IsActiveInHierarchy() && GetEnabled())
{ {
// Call enable when script is added to actor (previous actor was null) // Call enable when script is added to actor (previous actor was null)
Enable(); Enable();