Refactor scene objects initialization to call OnAwake before all OnStart

#520
This commit is contained in:
Wojtek Figat
2022-07-17 17:05:59 +02:00
parent 9aff782907
commit 3239150a61
14 changed files with 59 additions and 114 deletions

View File

@@ -114,7 +114,7 @@ void Script::SetParent(Actor* value, bool canBreakPrefabLink)
if (value && value->IsDuringPlay() && !IsDuringPlay())
{
// Prepare for gameplay
PostSpawn();
Initialize();
{
SceneBeginData beginData;
BeginPlay(&beginData);
@@ -122,10 +122,6 @@ void Script::SetParent(Actor* value, bool canBreakPrefabLink)
}
// Fire events for scripting
CHECK_EXECUTE_IN_EDITOR
{
OnAwake();
}
if (GetEnabled())
{
Start();
@@ -265,24 +261,21 @@ const Guid& Script::GetSceneObjectId() const
return GetID();
}
void Script::PostLoad()
void Script::Initialize()
{
ASSERT(!IsDuringPlay());
if (Flags & ObjectFlags::IsManagedType || Flags & ObjectFlags::IsCustomScriptingType)
SetupType();
// Use lazy creation for the managed instance, just register the object
if (!IsRegistered())
RegisterObject();
}
void Script::PostSpawn()
{
if (Flags & ObjectFlags::IsManagedType || Flags & ObjectFlags::IsCustomScriptingType)
SetupType();
// Create managed object
if (!HasManagedInstance())
CreateManaged();
CHECK_EXECUTE_IN_EDITOR
{
OnAwake();
}
}
void Script::BeginPlay(SceneBeginData* data)

View File

@@ -10,12 +10,11 @@
/// </summary>
API_CLASS(Abstract) class FLAXENGINE_API Script : public SceneObject
{
DECLARE_SCRIPTING_TYPE(Script);
DECLARE_SCRIPTING_TYPE(Script);
friend Actor;
friend SceneTicking;
friend class PrefabInstanceData;
protected:
int32 _enabled : 1;
int32 _tickFixedUpdate : 1;
int32 _tickUpdate : 1;
@@ -27,7 +26,6 @@ protected:
#endif
public:
/// <summary>
/// Gets value indicating if script is active.
/// </summary>
@@ -54,7 +52,6 @@ public:
API_PROPERTY() void SetActor(Actor* value);
public:
/// <summary>
/// Called after the object is loaded.
/// </summary>
@@ -77,7 +74,7 @@ public:
}
/// <summary>
/// Called before the object will be destroyed..
/// Called before the object will be destroyed.
/// </summary>
API_FUNCTION(Attributes="NoAnimate") virtual void OnDestroy()
{
@@ -126,14 +123,12 @@ public:
}
private:
void SetupType();
void Start();
void Enable();
void Disable();
public:
// [ScriptingObject]
String ToString() const override;
void OnDeleteObject() override;
@@ -143,8 +138,7 @@ public:
void SetParent(Actor* value, bool canBreakPrefabLink = true) override;
int32 GetOrderInParent() const override;
void SetOrderInParent(int32 index) override;
void PostLoad() override;
void PostSpawn() override;
void Initialize() override;
void BeginPlay(SceneBeginData* data) override;
void EndPlay() override;
void Serialize(SerializeStream& stream, const void* otherObj) override;