Download

Endless increasing Value?

Hello, I already know how to program, so i wanted to take a look at C++ programming on the Unreal Engine. I started with the documentation, and there was a thing, that makes me pensive.
Here is the code, which i found in the Unreal Engine Documentation:

FloatingActor.h:



// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#pragma once

#include "GameFramework/Actor.h"
#include "FloatingActor.generated.h"

UCLASS()
class QUICKSTART_API AFloatingActor : public AActor
{
    GENERATED_BODY()

public: 
    // Sets default values for this actor's properties
    AFloatingActor();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public:
    // Called every frame
    virtual void Tick( float DeltaSeconds ) override;

    float RunningTime;
};


FloatingActor.cpp:



// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.

#include "QuickStart.h"
#include "FloatingActor.h"

// Sets default values
AFloatingActor::AFloatingActor()
{
    // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AFloatingActor::BeginPlay()
{
    Super::BeginPlay();

}

// Called every frame
void AFloatingActor::Tick( float DeltaTime )
{
    Super::Tick( DeltaTime );

    FVector NewLocation = GetActorLocation();
    float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
    NewLocation.Z += DeltaHeight * 20.0f;       //Scale our height by a factor of 20
    RunningTime += DeltaTime;
    SetActorLocation(NewLocation);
}


So, i understand everything here, but one thing seems stupid to me.
There is the float “Running Time” and the code increases the float every tick. After a amount of Time, the highest possible number of the float should be reached and this will make an error, right?
Is there a better solution?
Btw, sorry for my bad english :wink:

Increasing / decreasing any primitive type value in C++ past its maximum value will result in the value wrapping. For example increasing a signed char of the value 127 by 1 will result in a value of -128, similarly increasing an unsigned int32 of the value 4,294,967,295 by 1 will result in 0. I suggest you brush up on your binary basics to understand why this occurs. If you’re concerned about value wrapping; clamp the values at a min and max.

The code you posted is example code, not production code. It’s there to help you understand, not for you to use in an actual game. It’s clearly written in a way that understands it’s not meant to be a long running application considering RunningTime wraps.

If you ran this code the actor’s location will likely snap to a very high negative value once the max of float has been breached.

Thank you very much, you helped me a lot. I was not planning to use this code in any project, I just want to take a look into programing the Unreal Engine :wink:
I definitely should brush up my binary basics, so i will do this before I go any further to program things in the Unreal Engine.
But again, thank you very much!