3#include "../IBaseApi.h"
11 game_api->GetCommands()->AddOnTimerCallback(
"API.TimerUpdate", std::bind(&Timer::Update,
this));
16 game_api->GetCommands()->RemoveOnTimerCallback(
"API.TimerUpdate");
21 static Timer instance;
27 const auto now =
std::chrono::system_clock::now();
28 const auto exec_time = now + std::chrono::seconds(delay_seconds);
30 timer_funcs_.emplace_back(std::make_shared<TimerFunc>(exec_time, callback,
true, 1, 0, identifier, moduleName));
34 int execution_counter,
bool async,
const FString& identifier,
const FString& moduleName)
38 std::thread([callback, execution_interval, execution_counter]()
40 if (execution_counter == -1)
45 std::this_thread::sleep_for(std::chrono::seconds(execution_interval));
49 for (
int i = 0; i < execution_counter; ++i)
52 std::this_thread::sleep_for(std::chrono::seconds(execution_interval));
58 const auto now =
std::chrono::system_clock::now();
59 timer_funcs_.emplace_back(
60 std::make_shared<TimerFunc>(now, callback,
false, execution_counter, execution_interval, identifier, moduleName));
66 timer_funcs_.erase(std::remove_if(timer_funcs_.begin(), timer_funcs_.end(),
69 return data->identifier.Equals(identifier);
70 }), timer_funcs_.end());
75 timer_funcs_.erase(std::remove_if(timer_funcs_.begin(), timer_funcs_.end(),
78 return data->moduleName.Equals(moduleName);
79 }), timer_funcs_.end());
84 if (timer_funcs_.empty())
89 const auto now =
std::chrono::system_clock::now();
92 const auto timer_funcs = timer_funcs_;
93 for (
const auto& data : timer_funcs)
95 if (data ==
nullptr)
continue;
97 if (now >= data->next_time)
105 if (data->execution_counter > 0)
107 --data->execution_counter;
109 else if (data->execution_counter != -1)
115 data->next_time = now + std::chrono::seconds(data->execution_interval);
124 timer_funcs_.erase(std::remove_if(timer_funcs_.begin(), timer_funcs_.end(), [&now](
const auto& data)
126 return (now >= data->next_time && data->exec_once) ||
127 (!data->exec_once && data->execution_counter == 0);
128 }), timer_funcs_.end());
ARK_API void DelayExecuteInternal(const std::function< void()> &callback, int delay_seconds, const FString &identifier="", const FString &moduleName="")
static ARK_API Timer & Get()
ARK_API void UnloadTimerInternal(const FString &identifier)
ARK_API void UnloadTimersFromModule(const FString &moduleName)
ARK_API void RecurringExecuteInternal(const std::function< void()> &callback, int execution_interval, int execution_counter, bool async, const FString &identifier, const FString &moduleName)