Add InvokeOnUpdate to C++ scripting api

This commit is contained in:
Wojtek Figat
2025-08-12 23:35:12 +02:00
parent 303087c4c4
commit 6fd4ef735e
2 changed files with 34 additions and 0 deletions

View File

@@ -106,6 +106,7 @@ namespace
MMethod* _method_LateFixedUpdate = nullptr; MMethod* _method_LateFixedUpdate = nullptr;
MMethod* _method_Draw = nullptr; MMethod* _method_Draw = nullptr;
MMethod* _method_Exit = nullptr; MMethod* _method_Exit = nullptr;
Array<Function<void()>> UpdateActions;
Dictionary<StringAnsi, BinaryModule*, InlinedAllocation<64>> _nonNativeModules; Dictionary<StringAnsi, BinaryModule*, InlinedAllocation<64>> _nonNativeModules;
#if USE_EDITOR #if USE_EDITOR
bool LastBinariesLoadTriggeredCompilation = false; bool LastBinariesLoadTriggeredCompilation = false;
@@ -242,6 +243,27 @@ void ScriptingService::Update()
PROFILE_CPU_NAMED("Scripting::Update"); PROFILE_CPU_NAMED("Scripting::Update");
INVOKE_EVENT(Update); INVOKE_EVENT(Update);
// Flush update actions
_objectsLocker.Lock();
int32 count = UpdateActions.Count();
for (int32 i = 0; i < count; i++)
{
UpdateActions[i]();
}
int32 newlyAdded = UpdateActions.Count() - count;
if (newlyAdded == 0)
UpdateActions.Clear();
else
{
// Someone added another action within current callback
Array<Function<void()>> tmp;
for (int32 i = newlyAdded; i < UpdateActions.Count(); i++)
tmp.Add(UpdateActions[i]);
UpdateActions.Clear();
UpdateActions.Add(tmp);
}
_objectsLocker.Unlock();
#ifdef USE_NETCORE #ifdef USE_NETCORE
// Force GC to run in background periodically to avoid large blocking collections causing hitches // Force GC to run in background periodically to avoid large blocking collections causing hitches
if (Time::Update.TicksCount % 60 == 0) if (Time::Update.TicksCount % 60 == 0)
@@ -303,6 +325,13 @@ void Scripting::ProcessBuildInfoPath(String& path, const String& projectFolderPa
path = projectFolderPath / path; path = projectFolderPath / path;
} }
void Scripting::InvokeOnUpdate(const Function<void()>& action)
{
_objectsLocker.Lock();
UpdateActions.Add(action);
_objectsLocker.Unlock();
}
bool Scripting::LoadBinaryModules(const String& path, const String& projectFolderPath) bool Scripting::LoadBinaryModules(const String& path, const String& projectFolderPath)
{ {
PROFILE_CPU_NAMED("LoadBinaryModules"); PROFILE_CPU_NAMED("LoadBinaryModules");

View File

@@ -230,6 +230,11 @@ public:
static void ProcessBuildInfoPath(String& path, const String& projectFolderPath); static void ProcessBuildInfoPath(String& path, const String& projectFolderPath);
/// <summary>
/// Calls the given action on the next scripting update.
/// </summary>
/// <param name="action">The action to invoke.</param>
static void InvokeOnUpdate(const Function<void()>& action);
private: private:
static bool LoadBinaryModules(const String& path, const String& projectFolderPath); static bool LoadBinaryModules(const String& path, const String& projectFolderPath);