article

AndresGenvid avatar image
AndresGenvid posted

02. Unreal C++ Clock DataStreams guide   

Unreal DataStreams Guide

This guide was written as a quick reference step by step guide for setting up a simple Unreal datastream. For more in depth information on each step please check the documentation at https://www.genvidtech.com/for-developers/sdk-latest//

Download Clock Assets

  1. Download the folder containing the assets for this guide

  2. Import “clock_face” as a texture after creating your project (or you can drag and drop the clock folder into your content folder in explorer to get a pre-built clock)

Clock Setup

  1. If you dragged and dropped the clock folder into your content folder skip this section

  2. Create an object of type “Sprite” (Paper2d->Sprite)

    • Set the source texture as “clock_face”

  3. Create a material and set it’s color to be black

  4. Create a blueprint actor. Attach the following components:

    • A scene component attached to the default scene root named “Clock”

    • A scene component attached to “Clock” named “HourHand”

    • A cube component attached to “HourHand” named “HourHandCube”

      • Set the location to 75, 0, 0

      • Set the scale to 1.5, 1, .15

      • Set the material to the material created above

    • A scene component attached to “Clock” named “MinuteHand”

    • A cube component attached to “MinuteHand” named “MinuteHandCube”

      • Set the location to 0, 0, 100

      • Set the scale to .1, 1, 2

      • Set the material to the material created above

    • A scene component attached to “Clock” named “SecondHand”

      • Set the rotation to 0, 45, 0

    • A cube component attached to “SecondHand” named “SecondHandCube”

      • Set the location to 50, 0, 0

      • Set the scale to 1, 1, .075

      • Set the material to the material created above

    • A paper sprite component attached to “Clock” named “Face”

      • Set the Source Sprite property to the sprite created above


  5. Finished component list for the clock




  6. Create a function called “GetAngle”. This will be used to calculate an angle from a hour, minute, or second

    • It should have 3 float inputs: Min, Max, and Value

    • It should have 1 float output: Angle

    • Call “Normalize to range” passing in the value, min, and max inputs

    • Call “Lerp”. A is 360, B is 0, and Alpha is the value returned by “normalize to range”

    • Return the value returned by Lerp


  7. GetAngle function




  8. Create 3 variables of type float on the blueprint. Hour, Minute and Second.

  9. On tick we want to set the hour value

    • The hour value should be in a 0 - 12 hour format allowing for decimals along the way. This value should then be passed into GetAngle. See the image below


  10. Tick function for the clock




    • The Minute value should be between in a 0 - 60 format allowing for decimals along the way. This value should then be passed into Get Angle. See the image below


  11. Tick function for the clock (Continuation from above)




    • The Second value should be between in a 0 - 60 format allowing for decimals along the way. This value should then be passed into Get Angle. See the image below


  12. Tick function for the clock (Continuation from above)



    • Set the Pitch of the “HourHand”, “MinuteHand”, and “SecondHand” using “SetRelativeRotation” and the values calculated for each. See the below image

      • Note 60 will have to be added to the hour angle, and 90 will have to be added to the second hand

    • Add the Clock blueprint actor to the level in view of the camera. Confirm it is working correctly by hitting play



Finished clock in the level



C++ Implementation

  1. Open your C++ GenvidStreams class.

  2. Create a function named “ClockTimeSteam” that returns FGenvidStream and takes no inputs. Add the following code:

  3. FGenvidStream ClockStream;
    ClockStream.Name = "ClockTime";
    ClockStream.OnGenvidStreamDelegate.BindUFunction(this, "OnSubmitClockTime");
    ClockStream.Framerate = 30;
    return ClockStream;
  4. This code declares a FGenvidStream, set’s its name, binds it to a function that doesn’t exist (yet), and sets its framerate to 30 before returning it.

  5. Create a function called “OnSubmitClockTime”. Have it return void and take an input of type FString& named “Id”. Add the following code:

  6. FClockTime ClockTime = GetClockTime();
    FString ClockTimeString;
    FJsonObjectConverter::UStructToJsonObjectString(ClockTime, ClockTimeString);
    SubmitGameData(Id, ClockTimeString);
  7. Make sure it has a UFUNCTION tag so it is bindable

  8. You will need to add "JsonUtilities" to the “PublicDependencyModuleNames.AddRange” list in your .build.cs file

  9. This function gets the clocks time using a function that doesn’t exist (yet) named GetClockTime.

  10. Creates a JSON string that will have the final format of {“hour”:”<hour>”,”minute”:”<minute>”,”second”:”<second>”}

  11. Calls an existing function on all GenvidStream classes called “SubmitGameData” that sends the stream

  12. You may want to log the ClockTimeString and Id to make sure the values are correct

  13. Id is the input parameter

  14. Create a structure called FClockTime with an hour, minute, and second property:

  15. USTRUCT(BlueprintType)
    struct FClockTime
    {
          
        GENERATED_BODY()
    public:
        UPROPERTY(BlueprintReadWrite, EditAnywhere)
        int32 Hour = 0;
    
    
        UPROPERTY(BlueprintReadWrite, EditAnywhere)
        int32 Minute = 0;
    
    
        UPROPERTY(BlueprintReadWrite, EditAnywhere)
        int32 Second = 0;
    };
  16. Create a function called “GetClockTime” that returns a FClockTime with no inputs

    • Set the hour, minutes, and seconds in a new FClockTime instance

  17. FClockTime ClockStream;
    ClockStream.Hour = FDateTime::Now().GetHour12();
    ClockStream.Minute = FDateTime::Now().GetMinute();
    ClockStream.Second = FDateTime::Now().GetSecond();
    return ClockStream;
  18. Override the BeginPlay function

    • Add the following code:

  19. Streams.Add(ClockTimeStream());
    Super::BeginPlay();
    • This adds the stream and binds the “OnSubmitClockTime” function using the function ClockTimeStream created above



unreal engineunreal
10 |600

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Article

Contributors

AndresGenvid contributed to this article