12 13 UPROPERTY(EditAnywhere, Category = MultiThreading) int32 MaxPrime; Then, inside the header file of the character’s class declaration and right after it’s implementation, I’ve added the following namespace, which contains the function that performs the actual calculations: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 namespace ThreadingTest { static void CalculatePrimeNumbers(int32 UpperLimit) { //Calculating the prime numbers... for (int32 i = 1; i <= UpperLimit; i++) { bool isPrime = true; for (int32 j = 2; j <= i / 2; j++) { if (FMath::Fmod(i, j) == 0) { isPrime = false; break; } } if (isPrime) GLog->Log("Prime number #" + FString::FromInt(i) + ": " + FString::FromInt(i)); } } } Later on, we will add one more class inside the header file of the character but not inside the character’s class. We declared a namespace which contains the static function CalculatePrimeNumbers in order to be able to access the same function from different code classes. Having said that, here is the implementation of the CalculatePrimeNumbers function: 1 2 3 4 5 6 7 8 void AMultiThreadingCharacter::CalculatePrimeNumbers() { //Performing the prime numbers calculations in the game thread... ThreadingTest::CalculatePrimeNumbers(MaxPrime); GLog->Log("--------------------------------------------------------------------"); GLog->Log("End of prime numbers calculation on game thread");
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 /** Camera boom positioning the camera behind the character */ UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) class USpringArmComponent* CameraBoom; /** Follow camera */ UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true")) class UCameraComponent* FollowCamera; public: AMultiThreadingCharacter(); /** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */ UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) float BaseTurnRate; /** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */ UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera) float BaseLookUpRate; protected: /** Called for forwards/backward input */ void MoveForward(float Value); /** Called for side to side input */ void MoveRight(float Value); /** * Called via input to turn at a given rate. * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate */ void TurnAtRate(float Rate); /** * Called via input to turn look up/down at a given rate. * @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate */ void LookUpAtRate(float Rate); /** Handler for when a touch input begins. */ void TouchStarted(ETouchIndex::Type FingerIndex, FVector Location); /** Handler for when a touch input stops. */ void TouchStopped(ETouchIndex::Type FingerIndex, FVector Location);
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 protected: // APawn interface virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override; // End of APawn interface public: /** Returns CameraBoom subobject **/ FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; } /** Returns FollowCamera subobject **/ FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; } protected: /*Calculates prime numbers in the game thread*/ UFUNCTION(BlueprintCallable, Category = MultiThreading) void CalculatePrimeNumbers(); /*Calculates prime numbers in a background thread*/ UFUNCTION(BlueprintCallable, Category = MultiThreading) void CalculatePrimeNumbersAsync(); /*The max prime number*/ UPROPERTY(EditAnywhere, Category = MultiThreading) int32 MaxPrime; }; namespace ThreadingTest { static void CalculatePrimeNumbers(int32 UpperLimit) { //Calculating the prime numbers... for (int32 i = 1; i <= UpperLimit; i++) { bool isPrime = true; for (int32 j = 2; j <= i / 2; j++) { if (FMath::Fmod(i, j) == 0) { isPrime = false; break; } }