NoobsDeSroobs

Unreal Engine 4 threading and singleton issue

Feb 17th, 2015
1,208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.36 KB | None | 0 0
  1. /////////////////////////////CustumPlayerController.h////////////////////////////////////////////
  2. // Fill out your copyright notice in the Description page of Project Settings.
  3.  
  4. #pragma once
  5.  
  6. #include "GameFramework/PlayerController.h"
  7. #include "MyInput.h"
  8. #include "CustumPlayerController.generated.h"
  9.  
  10.  
  11. /**
  12.  *
  13.  */
  14. UCLASS(config=Game)
  15. class OCULUSTEST_API ACustumPlayerController: public APlayerController, public FRunnable
  16. {
  17.     GENERATED_BODY()
  18.    
  19. private:
  20.     //The boolean that controlls the lifetime of our thread. While being true the thread will run.
  21.     bool Playing;
  22.     /*                   Thread begin                   */
  23.  
  24.     /** Singleton instance, can access the thread any time via static accessor, if it is active! */
  25.     static  ACustumPlayerController* Runnable;
  26.     /** Thread to run the worker FRunnable on */
  27.     FRunnableThread* Thread;
  28.  
  29.     //Done?
  30.     bool IsFinished() const
  31.     {
  32.         return !Playing;
  33.     }
  34.  
  35.     // Begin FRunnable interface.
  36.     virtual uint32 Run();
  37.     virtual void Stop();
  38.     // End FRunnable interface
  39.  
  40.     /** Makes sure this thread has stopped properly */
  41.     void EnsureCompletion();
  42.     /** Shuts down the thread. Static so it can easily be called from outside the thread context */
  43.     void Shutdown();
  44.  
  45.     /*                    Thread end                    */
  46.  
  47.     ///////////////////////////////////////////////////////////
  48.                            Omitted variables
  49.                              and functions
  50.         ///////////////////////////////////////////////////////////
  51.  
  52.     //Constructors and destructors.
  53.     ACustumPlayerController(const class FObjectInitializer& PCIP);
  54.     ~ACustumPlayerController();
  55.  
  56. public:
  57.     //Get the singleton thread.
  58.     static ACustumPlayerController* GetThread();
  59.  
  60.     //Set from the editor for easy testing.
  61.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Timing")
  62.     float PositionUpdateTimer;
  63.  
  64.     //This function will be called from BP and used to teleport the character. Called every tick.
  65.     UFUNCTION(BlueprintCallable, Category = "Timing")
  66.     FVector GetNewLocation(float DeltaTime);
  67.  
  68.     //This function will be called via BP on the event BeginPLay
  69.     UFUNCTION(BlueprintCallable, Category = "Timing")
  70.     void Init();
  71. };
  72.  
  73.  
  74. ////////////////////////CustumPlayerController.cpp//////////////////////////////////
  75. // Fill out your copyright notice in the Description page of Project Settings.
  76.  
  77. #include "Other headers"
  78. #include "CustumPlayerController.h"
  79.  
  80. //***********************************************************
  81. //Thread Worker Starts as NULL, prior to being instanced
  82. //      This line is essential! Compiler error without it
  83. ACustumPlayerController* ACustumPlayerController::Runnable = NULL;
  84. //***********************************************************
  85.  
  86. //Sets up the PlayerController
  87. ACustumPlayerController::ACustumPlayerController(const class FObjectInitializer& PCIP) : Super(PCIP)
  88. {
  89.     PrimaryActorTick.bCanEverTick = true;
  90.  
  91.     float PositionUpdateTimer = 0.25f;
  92.     Playing = false;
  93.  
  94.     /////////////////////////////////////////////////////////////////////
  95.                             Initialize variables
  96.         ////////////////////////////////////////////////////////////////////
  97.  
  98.     const bool bAutoDeleteSelf = false;
  99.     const bool bAutoDeleteRunnable = false;
  100.     Thread = FRunnableThread::Create(this, TEXT("ACustumPlayerController"), bAutoDeleteSelf, bAutoDeleteRunnable, 0, TPri_BelowNormal); //windows default = 8mb for thread, could specify more
  101. };
  102.  
  103. ACustumPlayerController::~ACustumPlayerController()
  104. {
  105.     Thread->Kill();
  106.     delete Thread;
  107.     Thread = NULL;
  108. };
  109.  
  110. //Run
  111. uint32 ACustumPlayerController::Run()
  112. {
  113.     //Initial wait before starting
  114.     FPlatformProcess::Sleep(0.03);
  115.  
  116.     //While not told to stop this thread
  117.     //      and not yet finished finding Prime Numbers
  118.     while(!IsFinished()) {
  119.         //Run the heavy calculations in a different thread.
  120.         UpdateData();
  121.        
  122.         //prevent thread from using too many resources
  123.         FPlatformProcess::Sleep(0.01);
  124.        
  125.     }
  126.  
  127.     return 0;
  128. }
  129.  
  130. //stop
  131. void ACustumPlayerController::Stop()
  132. {
  133.     Thread->Kill();
  134. }
  135.  
  136. void ACustumPlayerController::Shutdown()
  137. {
  138.     if(Runnable) {
  139.         Runnable->Exit();
  140.         Runnable->FinishDestroy();
  141.         Runnable = NULL;
  142.     }  
  143. }
  144.  
  145. ACustumPlayerController* ACustumPlayerController::GetThread()
  146. {
  147.     if(!Runnable && FPlatformProcess::SupportsMultithreading()) {
  148.         Runnable = new ACustumPlayerController(FObjectInitializer::FObjectInitializer());
  149.     }
  150.     return Runnable;
  151. }
  152.  
  153. //Prime the controller.
  154. void ACustumPlayerController::Init()
  155. {
  156.     if(!Runnable && FPlatformProcess::SupportsMultithreading()) {
  157.         Runnable = new ACustumPlayerController(FObjectInitializer::FObjectInitializer());
  158.     }
  159.  
  160.     //Set the rest of the variables and states.
  161.    
  162. }
  163.  
  164. ////////////////////Code to be run in the thread/////////////////////////////////
  165. void ACustumPlayerController::Update()
  166. {
  167.     //Limit the number of updates.
  168.     if (CurrDeltaSec < PositionUpdateTimer) return;
  169.     CurrDeltaSec -= PositionUpdateTimer;
  170.  
  171.     ///////////////////////////////////////////////////////////////
  172.               Heavy calculations and slow input device interaction
  173.     ///////////////////////////////////////////////////////////////
  174. }
  175.  
  176. ///////////////////////////////End of code to be run in the thread/////////////////////////////////
  177.  
  178. //Return the new position. This function is called every tick.
  179. FVector ACustumPlayerController::GetNewLocation(float DeltaTime)
  180. {
  181.     CurrDeltaSec += DeltaTime;
  182.  
  183.     return LatestPosition;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment