NebulaGames

C++ to Blueprints World Date And Time

Mar 4th, 2019
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.23 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #pragma once
  4.  
  5. #include "CoreMinimal.h"
  6. #include "Runtime/Engine/Classes/GameFramework/Actor.h"
  7. #include "Runtime/CoreUObject/Public/UObject/Class.h"
  8. #include "Components/ActorComponent.h"
  9. #include "Runtime/Engine/Public/TimerManager.h"
  10. #include "Kismet/KismetStringLibrary.h"
  11. #include "EarthTime.generated.h"
  12.  
  13. // Enum of Months
  14.  
  15. UENUM(BlueprintType)                                   
  16. enum class ECalendarMonths : uint8
  17. {
  18.     January, February, March, April, May, June, July,
  19.     August, September, October, November, December
  20. };
  21.  
  22. // Input struct containing all necessary values to determine where to begin counting time
  23.  
  24. USTRUCT(BlueprintType)                                 
  25. struct FStart_Date
  26. {
  27.     GENERATED_USTRUCT_BODY()           
  28.  
  29. public:                                                                                                            
  30.  
  31.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Start Date", meta = (ClampMin = "0", ClampMax = "59", UIMin = "0", UIMax = "59"))
  32.         int32 Seconds;
  33.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Start Date", meta = (ClampMin = "0", ClampMax = "59", UIMin = "0", UIMax = "59"))
  34.         int32 Minutes;
  35.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Start Date", meta = (ClampMin = "0", ClampMax = "23", UIMin = "0", UIMax = "23"))
  36.         int32 Hours;
  37.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Start Date", meta = (ClampMin = "1", ClampMax = "31", UIMin = "1", UIMax = "31"))
  38.         int32 Day;
  39.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Start Date", meta = (ClampMin = "1", ClampMax = "12", UIMin = "1", UIMax = "12"))
  40.         ECalendarMonths Month;
  41.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Start Date", meta = (ClampMin = "0", UIMin = "0"))
  42.         int32 Year;
  43. };
  44.  
  45.  
  46. UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
  47. class CODE_TEST_API UEarthTime : public UActorComponent
  48. {
  49.     GENERATED_BODY()
  50.  
  51. public:
  52.     // Sets default values for this component's properties
  53.  
  54.     UEarthTime();
  55.  
  56.     // Called every frame
  57.  
  58.     virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
  59.  
  60.     // "Read Only" blueprint variables to give access to function data for UI purposes
  61.        
  62.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")                      
  63.         FString World_Clock;
  64.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")                      
  65.         FString World_Date;
  66.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")                      
  67.         int32 Current_Seconds;
  68.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")                      
  69.         int32 Current_Minutes;
  70.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")                      
  71.         int32 Current_Hours;
  72.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")                      
  73.         int32 Current_Day;
  74.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")                      
  75.         int32 Current_Month;
  76.     UPROPERTY(BlueprintReadOnly, Category = "Earth Time")                      
  77.         int32 Current_Year;
  78.  
  79.     // Blueprint exposed functions to manipulate World Clock settings
  80.  
  81.     UFUNCTION(BlueprintCallable)                                                                   
  82.         void Earth_Genesis(UPARAM(ref)FStart_Date &Start_Date, float Rate, UPARAM(DisplayName = "Military Time") bool TwentyFour);     
  83.     UFUNCTION(BlueprintCallable)                                                                   
  84.         void AlterTime(float New_Rate, UPARAM(DisplayName = "Military Time") bool NewClock);
  85.     UFUNCTION(BlueprintCallable)                                                                   
  86.         void Pause();
  87.     UFUNCTION(BlueprintCallable)                                                                   
  88.         void Unpause();
  89.     UFUNCTION(BlueprintCallable)                                                                   
  90.         void ClearTimer();
  91.     UFUNCTION(BlueprintCallable)
  92.         void SaveDate(FStart_Date &End_Date);
  93.  
  94.     // Internal Functions
  95.  
  96.     UFUNCTION()
  97.         void AgeOfEarth();
  98.     UFUNCTION()
  99.         void GenerateWorld_Clock();
  100.     UFUNCTION()
  101.         void GenerateWorld_Date();
  102.     UFUNCTION()
  103.         void GenerateDaysReset(int32 ActiveMonth);
  104.     UFUNCTION()
  105.         FString MonthAsString(int32 ConvertMonth);
  106.  
  107.     // Global variables necessary for Earth Time functionality
  108.  
  109.     bool Time_Altered;                                                         
  110.     bool Military_Time;
  111.     ECalendarMonths Calendar_Months;
  112.     int32 DaysReset;
  113.     FTimerHandle Universal_Age;
  114.     AActor* Parent_Actor;
  115.  
  116. protected:
  117.     // Called when the game starts
  118.     virtual void BeginPlay() override;
  119.  
  120. };
  121.  
  122.  
  123.  
  124. --------------------------------------------------------------------------------------------------------------------------------------------
  125.  
  126.  
  127.  
  128. #include "EarthTime.h"
  129.  
  130. UEarthTime::UEarthTime()
  131. {
  132.     // Sets default values for this component's properties
  133.  
  134.     PrimaryComponentTick.bCanEverTick = false;              // Disable tick for this component
  135.     Parent_Actor = GetOwner();                              // Retrieve actor component's Parent Actor
  136.     Time_Altered = false;
  137.     Military_Time = false;                     
  138.     int32 DaysReset = 32;
  139.  
  140. }
  141.  
  142.  
  143. // Called when the game starts
  144. void UEarthTime::BeginPlay()
  145. {
  146.  
  147.     Super::BeginPlay();
  148.  
  149. }
  150.  
  151.  
  152. // Called every frame
  153. void UEarthTime::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
  154. {
  155.  
  156.     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
  157.  
  158. }
  159.  
  160. // Set date and time values for timer function
  161. void UEarthTime::Earth_Genesis(UPARAM(ref)FStart_Date &Start_Date, float Rate, UPARAM()bool TwentyFour)
  162. {
  163.     Military_Time = TwentyFour;                             // Set if we are using a 12 or 24 hr clock
  164.  
  165.     if (Time_Altered == false)                              // Should we start counting from initial input values or do we already have values
  166.     {
  167.         Current_Seconds = Start_Date.Seconds;              
  168.         Current_Minutes = Start_Date.Minutes;
  169.         Current_Hours = Start_Date.Hours;
  170.         Current_Day = Start_Date.Day;
  171.         Current_Month = static_cast<int>(Start_Date.Month);
  172.         Current_Year = Start_Date.Year;
  173.         GenerateDaysReset(Current_Month);
  174.     }
  175.  
  176.     if (Rate == 0)                                          // Prevent division by "0" from improper user input
  177.     {
  178.         Rate = 1;
  179.     }  
  180.  
  181.     Parent_Actor->GetWorldTimerManager().SetTimer(Universal_Age, this, &UEarthTime::AgeOfEarth, 1/Rate, true);      // Create a timer
  182.  
  183. }
  184.  
  185. // Function that actually tracks date and time
  186. void UEarthTime::AgeOfEarth()                                
  187. {
  188.     int32 SecondsReset = 60;
  189.     int32 SecondsResetValue = 0;
  190.     int32 MinutesReset = 60;
  191.     int32 MinutesResetValue = 0;
  192.     int32 HoursReset = 24;
  193.     int32 HoursResetValue = 0;
  194.     int32 MonthsReset = 12;
  195.  
  196.     Current_Seconds++;                         
  197.  
  198.     if (Current_Seconds == SecondsReset)                   
  199.     {
  200.         Current_Seconds = SecondsResetValue;
  201.         Current_Minutes ++;
  202.  
  203.         if (Current_Minutes == MinutesReset)               
  204.         {
  205.             Current_Minutes = MinutesResetValue;
  206.             Current_Hours ++;
  207.         }
  208.  
  209.         if (Current_Hours == HoursReset)
  210.         {
  211.             Current_Hours = HoursResetValue;
  212.             Current_Day ++;
  213.         }
  214.         if (Current_Day == DaysReset)              
  215.         {
  216.             Current_Day = 1;
  217.             Current_Month ++;
  218.             GenerateDaysReset(Current_Month);
  219.         }
  220.         if (Current_Month == MonthsReset)
  221.         {
  222.             Current_Month = 0;
  223.             Current_Year ++;
  224.         }
  225.  
  226.     }
  227.  
  228.     // Function calls to create World Date and Clock string values
  229.     GenerateWorld_Clock();                                     
  230.     GenerateWorld_Date();
  231.  
  232. }
  233.  
  234. // Function to convert time values into "00:00:00" format (AM/PM if necessary)
  235. void UEarthTime::GenerateWorld_Clock()                         
  236. {
  237.     FString HoursOutput;
  238.     FString MinutesOutput;
  239.     FString SecondsOutput;
  240.     FString TimeOfDay = "AM";
  241.     int32 StandardHours = Current_Hours;
  242.     bool PM = false;
  243.    
  244.  
  245.     if (Military_Time == false && StandardHours == 0)
  246.     {
  247.         StandardHours = 12;
  248.         TimeOfDay = "AM";
  249.     }
  250.     else if (Military_Time == false && StandardHours > 12)
  251.     {
  252.         StandardHours -= 12;
  253.         PM = true;
  254.         TimeOfDay = "PM";
  255.     }
  256.     else if (Military_Time == false && StandardHours == 12)
  257.     {
  258.         PM = true;
  259.         TimeOfDay = "PM";
  260.     }
  261.  
  262.     HoursOutput = FString::FromInt(StandardHours);             
  263.     if (HoursOutput.Len() < 2)                                 
  264.     {
  265.         HoursOutput = "0" + HoursOutput;
  266.     }
  267.     MinutesOutput = FString::FromInt(Current_Minutes);             
  268.     if (MinutesOutput.Len() < 2)                                   
  269.     {
  270.         MinutesOutput = "0" + MinutesOutput;
  271.     }
  272.     SecondsOutput = FString::FromInt(Current_Seconds);
  273.     if (SecondsOutput.Len() < 2)
  274.     {
  275.         SecondsOutput = "0" + SecondsOutput;
  276.     }
  277.    
  278.     // If not on military time output string will include "AM/PM" designation
  279.     if (Military_Time == false)
  280.     {
  281.         World_Clock = (HoursOutput + " : " + MinutesOutput + " : " + SecondsOutput + " " + TimeOfDay);     
  282.     }
  283.     else
  284.     {
  285.         World_Clock = (HoursOutput + " : " + MinutesOutput + " : " + SecondsOutput);       
  286.     }
  287. }
  288.  
  289. // Function to convert month, day, year values into "MM/DD/YYYY" format
  290. void UEarthTime::GenerateWorld_Date()                                      
  291. {
  292.     FString MonthOutput;
  293.     FString DayOutput;
  294.     FString YearOutput;
  295.  
  296.     MonthOutput = MonthAsString(Current_Month);
  297.  
  298.     DayOutput = FString::FromInt(Current_Day);
  299.     if (DayOutput.Len() < 2)
  300.     {
  301.         DayOutput = "0" + DayOutput;
  302.     }
  303.  
  304.     YearOutput = FString::FromInt(Current_Year);
  305.    
  306.  
  307.     World_Date = (MonthOutput + " " + DayOutput + ", " + YearOutput);      
  308.  
  309. }
  310.  
  311. // Function to "Pause" World Time
  312. void UEarthTime::Pause()                                                   
  313. {
  314.     Parent_Actor->GetWorldTimerManager().PauseTimer(Universal_Age);
  315. }
  316.  
  317. // Function to "Unpause" World Time
  318. void UEarthTime::Unpause()                                                 
  319. {
  320.     Parent_Actor->GetWorldTimerManager().UnPauseTimer(Universal_Age);
  321. }
  322.  
  323. // Function to "Clear" World Time
  324. void UEarthTime::ClearTimer()                                              
  325. {
  326.     Parent_Actor->GetWorldTimerManager().ClearTimer(Universal_Age);
  327. }
  328.  
  329. // Function to "Save" current World Time and Date
  330. void UEarthTime::SaveDate(FStart_Date &End_Date)
  331. {
  332.     Pause();
  333.  
  334.     End_Date.Month = static_cast<ECalendarMonths>(Current_Month);
  335.     End_Date.Day = Current_Day;
  336.     End_Date.Year = Current_Year;
  337.     End_Date.Hours = Current_Hours;
  338.     End_Date.Minutes = Current_Minutes;
  339.     End_Date.Seconds = Current_Seconds;
  340.  
  341.     ClearTimer();
  342. }
  343.  
  344. // Function to change rate of World Time passage
  345. void UEarthTime::AlterTime(float New_Rate, bool NewClock)                                  
  346. {
  347.     FStart_Date New_Start_Date;
  348.     Time_Altered = true;
  349.     Parent_Actor->GetWorldTimerManager().ClearTimer(Universal_Age);
  350.     New_Start_Date.Seconds = Current_Seconds;
  351.     New_Start_Date.Minutes = Current_Minutes;
  352.     New_Start_Date.Hours = Current_Hours;
  353.     New_Start_Date.Day = Current_Day;
  354.     New_Start_Date.Month = static_cast<ECalendarMonths>(Current_Month);
  355.     New_Start_Date.Year = Current_Year;
  356.     Earth_Genesis(New_Start_Date, New_Rate, NewClock);
  357. }
  358.  
  359. // Function to convert Month Enum to string for output to blueprints
  360. FString UEarthTime::MonthAsString(int32 ConvertMonth)
  361. {
  362.     FString EnumMonth;
  363.  
  364.     switch (ConvertMonth)
  365.     {
  366.     case 0:
  367.         EnumMonth = "January";
  368.         break;
  369.     case 1:
  370.         EnumMonth = "February";
  371.         break;
  372.     case 2:
  373.         EnumMonth = "March";
  374.         break;
  375.     case 3:
  376.         EnumMonth = "April";
  377.         break;
  378.     case 4:
  379.         EnumMonth = "May";
  380.         break;
  381.     case 5:
  382.         EnumMonth = "June";
  383.         break;
  384.     case 6:
  385.         EnumMonth = "July";
  386.         break;
  387.     case 7:
  388.         EnumMonth = "August";
  389.         break;
  390.     case 8:
  391.         EnumMonth = "September";
  392.         break;
  393.     case 9:
  394.         EnumMonth = "October";
  395.         break;
  396.     case 10:
  397.         EnumMonth = "November";
  398.         break;
  399.     case 11:
  400.         EnumMonth = "December";
  401.         break;
  402.     default:
  403.         EnumMonth = "Bad Month";
  404.         break;
  405.     }
  406.  
  407.     return EnumMonth;
  408. }
  409.  
  410. //Function to determine when to "reset" day count (adjusts for Leap year)
  411. void UEarthTime::GenerateDaysReset(int32 ActiveMonth)
  412. {
  413.     switch (ActiveMonth)
  414.     {
  415.     case 0: case 2: case 4: case 6: case 7: case 9: case 11:
  416.         DaysReset = 32;
  417.         break;
  418.     case 1:
  419.         if (Current_Year % 4 == 0)
  420.         {
  421.             DaysReset = 30;
  422.         }
  423.         else
  424.         {
  425.             DaysReset = 29;
  426.         }
  427.         break;
  428.     case 3: case 5: case 8: case 10:
  429.         DaysReset = 31;
  430.         break;
  431.     default:
  432.         DaysReset = 31;
  433.         break;
  434.     }
  435.  
  436.     if (Current_Day >= DaysReset)
  437.     {
  438.         Current_Day = (DaysReset- 1);
  439.     }
  440. }
Advertisement
Add Comment
Please, Sign In to add comment