Okay, so I’m currently learning an Unreal Engine programming tutorial. Here is the code I’m confused with:
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);
}
I don’t understand the part where it says this:
void AFloatingActor::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
and this part:
float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
NewLocation.Z += DeltaHeight * 20.0f; // Scale our height by a factor of 20
What does it do? How does it do that? What is FMath::Sin? How does the “::” work in this case? It’s so confusing.
That’s it! Thank you for your time (and hopefully, help)!
Hi TheUniqueVirus,
The line Super::Tick( DeltaTime );
is telling the code to call the parent class’ version of the Tick() function before continuing on with its own custom implementation.
The next set of questions is a little more complex. The first line of code you quoted is creating a new float variable named DeltaHeight and assigning it a value. The value that is assigned is the result of a calculation of the Sine of (RunningTime + DeltaTime) minus the Sine of RunningTime. In this case, RunningTime is a variable that is keeping track of how long the game has been running, and DeltaTime is how long it has been since the last game Tick. Essentially this calculation is determining how much movement should have occurred since the last time the calculation was run.
The second line is taking the result of that calculation, multiplying it by 20 (using a magic number!) and adding it to the Z (vertical) value of the FVector NewLocation. The end result of this is that whatever Actor is calling this code will have an up and down bobbing motion while the game is running that will remain at the same rate regardless of whether the game’s FPS speeds up or slows down.
FMath::Sin()
tells the code to look for the Sin()
function in the FMath
class, and call that function. The ::
is a scope operator. It allows you to differentiate between a Sin() function in the current class or namespace, and one in a different class or namespace.
1 Like
Thank you for your help, but what do you mean by “DeltaTime is how long it has been since the last game Tick?” What does Tick mean? And for the second part, how does the Sin() function work? And how is it affecting the value in “RunningTime + DeltaTime?” I’m sorry for having so many questions at once. Thank you so much for the help!
“Tick” is the term used in Unreal Engine for one run through the game loop. Essentially everything that happens within your game is because the game is constantly running through a loop. Everything that makes your game function, from animation to physics to movement to lighting and rendering, all of those calculations happen once every time through the loop (every Tick), and there will (hopefully) be multiple Ticks every second. In its simplest form, a Tick is the same as one frame in your game, so when your game is playing at 60 frames per second, there will be 60 Ticks per second.
The actual number of Ticks that happen per second depend on how many calculations need to happen during that tick, which is why it is typically a bad idea to have thousands of moving physics objects on screen, casting dynamic shadows from multiple moving lights. The more calculations that need to take place, the longer each Tick will take, and the lower your resulting frames per second will be.
Unreal Engine automatically keeps track of how long it takes for each Tick to complete, and it stores the time it took the previous Tick to complete in DeltaTime. With DeltaTime, you can make sure your movement, animations, logic, etc. will always play and look the same regardless of the frames per second the user is seeing.
The Sin() function is simply completing a trigonometric Sine calculation, which results in an oscillating value between 1 and -1 (which is then multiplied by 20 in this case). By using the total time since the game started, and factoring in the time the last Tick took, this code will result in a smooth up and down movement that will look the same at 30 frames per second as it does at 60.
1 Like
Oh, now I got it! Thank you .
Sorry, but I got one more question, I noticed that RunningTime is a float that I made in the “.h” of the class, so how is RunningTime the variable that is keeping track of how long the game has been running?
RunningTime should be getting initialized to 0.0 at the beginning of the game. Then, with every Tick that happens, the time that the previous Tick took to complete is added to RunningTime. You see this happening in the line RunningTime += DeltaTime;
As long as you do not reset the value of RunningTime in the middle of the game, it will continue to accumulate the time that each previous Tick took to complete, which will let you know how long the game has been running.
Where is the documentation for Sin()
I’ve found this: FMath | Unreal Engine Documentation But the function is not there? Is the documentation incomplete?
Hi ,
The Sin()
function is defined in the FGenericPlatformMath
struct. The Sin()
function actually calls the sinf()
function that is located in the UCRT provided by Microsoft.
Hey, why we use the sin function instead of a current value of running time?