Refactor so GameModeBase is the runnable

This commit is contained in:
2023-06-22 15:17:49 -04:00
parent c6558ac0b4
commit 9a85092afb
7 changed files with 83 additions and 91 deletions

View File

@@ -24,13 +24,18 @@ EngineWrapper AIntegrationGameModeBase::Luprex;
AIntegrationGameModeBase::AIntegrationGameModeBase()
{
Worker = nullptr;
Thread = nullptr;
ThreadStopRequested = false;
//PrimaryActorTick.bCanEverTick = true; // Probably wrong
//PrimaryActorTick.bTickEvenWhenPaused = true; // Probably wrong
//PrimaryActorTick.TickGroup = TG_PrePhysics; // Probably wrong
SetActorTickEnabled(true); // Probably wrong
SetActorTickInterval(1.0f); // Probably wrong
SetActorTickEnabled(true);
SetActorTickInterval(1.0f);
}
AIntegrationGameModeBase::~AIntegrationGameModeBase()
{
WaitForThread();
}
void AIntegrationGameModeBase::HandleConsoleOutput()
@@ -46,14 +51,44 @@ void AIntegrationGameModeBase::HandleConsoleOutput()
ConsoleOutput.Append(fs);
}
void AIntegrationGameModeBase::WaitForWorkerThread()
void AIntegrationGameModeBase::LaunchThread()
{
if (Thread == nullptr) {
ThreadStopRequested = false;
Thread = FRunnableThread::Create(this, TEXT("Worker Thread"));
}
}
// Init routine called by the worker thread.
bool AIntegrationGameModeBase::Init()
{
engineutil::DPrint("WorkerRunnable::Init");
return true;
}
// Run routine called by the worker thread.
uint32 AIntegrationGameModeBase::Run()
{
while (!ThreadStopRequested)
{
engineutil::DPrint("WorkerRunnable::Run");
FPlatformProcess::Sleep(1.0);
}
engineutil::DPrint("WorkerRunnable Done");
return 0;
}
void AIntegrationGameModeBase::Stop()
{
ThreadStopRequested = true;
}
#pragma optimize( "", off )
void AIntegrationGameModeBase::WaitForThread()
{
if (Thread == nullptr) return;
//Worker->Stop();
//Thread->WaitForCompletion();
delete Thread;
delete Worker;
Worker = nullptr;
Stop(); // Notifies the thread to clean up and exit.
delete Thread; // This waits for the thread to complete.
Thread = nullptr;
}
@@ -68,10 +103,10 @@ void AIntegrationGameModeBase::Tick(float DeltaSeconds)
EngineSeconds += DeltaSeconds;
Luprex.play_invoke_event_update(&Luprex, EngineSeconds);
}
for (const FString& fs : engineutil::DPrintGetStored()) {
TArray<FString> prints = engineutil::DPrintGetStored();
for (const FString& fs : prints) {
ConsoleOutput.AppendLine(fs);
}
engineutil::DPrintClearStored();
if (ConsoleOutput.IsDirty()) {
ConsoleSetOutput(ConsoleOutput.Get());
ConsoleOutput.ClearDirty();
@@ -94,7 +129,7 @@ void AIntegrationGameModeBase::ConsoleSendInput(const FString& fs)
void AIntegrationGameModeBase::BeginPlay()
{
engineutil::RawPrint("In BeginPlay");
engineutil::DPrint("In BeginPlay");
Super::BeginPlay();
if (!luprex_initialized()) {
engineutil::init_wrapper(&Luprex);
@@ -122,16 +157,12 @@ void AIntegrationGameModeBase::BeginPlay()
ConsoleOutput.AppendLine(FString("Initialize Luprex Success"));
}
EngineSeconds = 0;
if (Thread == nullptr) {
Worker = new FWorkerRunnable();
Thread = FRunnableThread::Create(Worker, TEXT("Worker Thread"));
}
LaunchThread();
}
void AIntegrationGameModeBase::EndPlay(const EEndPlayReason::Type EndPlayReason) {
engineutil::RawPrint("In EndPlay");
WaitForWorkerThread();
engineutil::DPrint("In EndPlay");
WaitForThread();
}