Merge branch 'latefixedupdate' of https://github.com/GoaLitiuM/FlaxEngine into GoaLitiuM-latefixedupdate

This commit is contained in:
Wojtek Figat
2023-05-12 14:41:49 +02:00
10 changed files with 87 additions and 3 deletions

View File

@@ -66,6 +66,7 @@ Action Engine::FixedUpdate;
Action Engine::Update;
TaskGraph* Engine::UpdateGraph = nullptr;
Action Engine::LateUpdate;
Action Engine::LateFixedUpdate;
Action Engine::Draw;
Action Engine::Pause;
Action Engine::Unpause;
@@ -199,6 +200,7 @@ int32 Engine::Main(const Char* cmdLine)
if (Time::OnBeginPhysics())
{
OnFixedUpdate();
OnLateFixedUpdate();
Time::OnEndPhysics();
}
@@ -274,6 +276,17 @@ void Engine::OnFixedUpdate()
}
}
void Engine::OnLateFixedUpdate()
{
PROFILE_CPU_NAMED("Late Fixed Update");
// Call event
LateFixedUpdate();
// Update services
EngineService::OnLateFixedUpdate();
}
void Engine::OnUpdate()
{
PROFILE_CPU_NAMED("Update");

View File

@@ -54,6 +54,11 @@ public:
/// </summary>
static Action LateUpdate;
/// <summary>
/// Event called after engine update.
/// </summary>
static Action LateFixedUpdate;
/// <summary>
/// Event called during frame rendering and can be used to invoke custom rendering with GPUDevice.
/// </summary>
@@ -107,6 +112,11 @@ public:
/// </summary>
static void OnLateUpdate();
/// <summary>
/// Late fixed update callback.
/// </summary>
static void OnLateFixedUpdate();
/// <summary>
/// Draw callback.
/// </summary>

View File

@@ -33,6 +33,7 @@ static bool CompareEngineServices(EngineService* const& a, EngineService* const&
DEFINE_ENGINE_SERVICE_EVENT(FixedUpdate);
DEFINE_ENGINE_SERVICE_EVENT(Update);
DEFINE_ENGINE_SERVICE_EVENT(LateUpdate);
DEFINE_ENGINE_SERVICE_EVENT(LateFixedUpdate);
DEFINE_ENGINE_SERVICE_EVENT(Draw);
DEFINE_ENGINE_SERVICE_EVENT_INVERTED(BeforeExit);

View File

@@ -44,6 +44,7 @@ public:
DECLARE_ENGINE_SERVICE_EVENT(void, FixedUpdate);
DECLARE_ENGINE_SERVICE_EVENT(void, Update);
DECLARE_ENGINE_SERVICE_EVENT(void, LateUpdate);
DECLARE_ENGINE_SERVICE_EVENT(void, LateFixedUpdate);
DECLARE_ENGINE_SERVICE_EVENT(void, Draw);
DECLARE_ENGINE_SERVICE_EVENT(void, BeforeExit);
DECLARE_ENGINE_SERVICE_EVENT(void, Dispose);

View File

@@ -121,6 +121,19 @@ void SceneTicking::LateUpdateTickData::TickScripts(const Array<Script*>& scripts
}
}
SceneTicking::LateFixedUpdateTickData::LateFixedUpdateTickData()
: TickData(64)
{
}
void SceneTicking::LateFixedUpdateTickData::TickScripts(const Array<Script*>& scripts)
{
for (auto* script : scripts)
{
script->OnLateFixedUpdate();
}
}
void SceneTicking::AddScript(Script* obj)
{
ASSERT_LOW_LAYER(obj && obj->GetParent() && obj->GetParent()->GetScene());
@@ -130,6 +143,9 @@ void SceneTicking::AddScript(Script* obj)
Update.AddScript(obj);
if (obj->_tickLateUpdate)
LateUpdate.AddScript(obj);
if (obj->_tickLateFixedUpdate)
LateFixedUpdate.AddScript(obj);
}
void SceneTicking::RemoveScript(Script* obj)
@@ -141,6 +157,8 @@ void SceneTicking::RemoveScript(Script* obj)
Update.RemoveScript(obj);
if (obj->_tickLateUpdate)
LateUpdate.RemoveScript(obj);
if (obj->_tickLateFixedUpdate)
LateFixedUpdate.RemoveScript(obj);
}
void SceneTicking::Clear()
@@ -148,4 +166,5 @@ void SceneTicking::Clear()
FixedUpdate.Clear();
Update.Clear();
LateUpdate.Clear();
LateFixedUpdate.Clear();
}

View File

@@ -109,6 +109,13 @@ public:
void TickScripts(const Array<Script*>& scripts) override;
};
class FLAXENGINE_API LateFixedUpdateTickData : public TickData
{
public:
LateFixedUpdateTickData();
void TickScripts(const Array<Script*>& scripts) override;
};
public:
/// <summary>
/// Adds the script to scene ticking system.
@@ -142,4 +149,9 @@ public:
/// The late update tick function.
/// </summary>
LateUpdateTickData LateUpdate;
/// <summary>
/// The late fixed update tick function.
/// </summary>
LateFixedUpdateTickData LateFixedUpdate;
};

View File

@@ -26,6 +26,7 @@ Script::Script(const SpawnParams& params)
, _tickFixedUpdate(false)
, _tickUpdate(false)
, _tickLateUpdate(false)
, _tickLateFixedUpdate(false)
, _wasStartCalled(false)
, _wasEnableCalled(false)
{
@@ -181,6 +182,7 @@ void Script::SetupType()
_tickUpdate |= type.Script.ScriptVTable[8] != nullptr;
_tickLateUpdate |= type.Script.ScriptVTable[9] != nullptr;
_tickFixedUpdate |= type.Script.ScriptVTable[10] != nullptr;
_tickLateFixedUpdate |= type.Script.ScriptVTable[11] != nullptr;
}
typeHandle = type.GetBaseType();
}

View File

@@ -19,6 +19,7 @@ protected:
int32 _tickFixedUpdate : 1;
int32 _tickUpdate : 1;
int32 _tickLateUpdate : 1;
int32 _tickLateFixedUpdate : 1;
int32 _wasStartCalled : 1;
int32 _wasEnableCalled : 1;
#if USE_EDITOR
@@ -108,6 +109,13 @@ public:
{
}
/// <summary>
/// Called every fixed framerate frame (after FixedUpdate) if object is enabled.
/// </summary>
API_FUNCTION(Attributes = "NoAnimate") virtual void OnLateFixedUpdate()
{
}
/// <summary>
/// Called during drawing debug shapes in editor. Use <see cref="DebugDraw"/> to draw debug shapes and other visualization.
/// </summary>

View File

@@ -46,6 +46,7 @@ public:
void Update() override;
void LateUpdate() override;
void FixedUpdate() override;
void LateFixedUpdate() override;
void Draw() override;
void BeforeExit() override;
void Dispose() override;
@@ -100,6 +101,7 @@ namespace
MMethod* _method_Update = nullptr;
MMethod* _method_LateUpdate = nullptr;
MMethod* _method_FixedUpdate = nullptr;
MMethod* _method_LateFixedUpdate = nullptr;
MMethod* _method_Draw = nullptr;
MMethod* _method_Exit = nullptr;
Array<BinaryModule*, InlinedAllocation<64>> _nonNativeModules;
@@ -210,6 +212,12 @@ void ScriptingService::FixedUpdate()
INVOKE_EVENT(FixedUpdate);
}
void ScriptingService::LateFixedUpdate()
{
PROFILE_CPU_NAMED("Scripting::LateFixedUpdate");
INVOKE_EVENT(LateFixedUpdate);
}
void ScriptingService::Draw()
{
PROFILE_CPU_NAMED("Scripting::Draw");

View File

@@ -80,17 +80,22 @@ namespace FlaxEngine
public static event Action Update;
/// <summary>
/// Occurs on scripting 'late' update.
/// Occurs on scripting late update.
/// </summary>
public static event Action LateUpdate;
/// <summary>
/// Occurs on scripting `fixed` update.
/// Occurs on scripting fixed update.
/// </summary>
public static event Action FixedUpdate;
/// <summary>
/// Occurs on scripting `draw` update. Called during frame rendering and can be used to invoke custom rendering with GPUDevice.
/// Occurs on scripting late fixed update.
/// </summary>
public static event Action LateFixedUpdate;
/// <summary>
/// Occurs on scripting draw update. Called during frame rendering and can be used to invoke custom rendering with GPUDevice.
/// </summary>
public static event Action Draw;
@@ -302,6 +307,11 @@ namespace FlaxEngine
FixedUpdate?.Invoke();
}
internal static void Internal_LateFixedUpdate()
{
LateFixedUpdate?.Invoke();
}
internal static void Internal_Draw()
{
Draw?.Invoke();