Local variable understanding

Hello, I’m not sure if I’m understanding how a local variable work, I’m doing the “Unreal Engine 5 C++ Developer: Learn C++ & Make Video Games”.
I’m in section 3 between video 50 and 53.

Well in the video 50 we’ll see the local variables and the code in that is:

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

	FVector LocalVector = MyVector;
	LocalVector.Z = LocalVector.Z + 100;
	MyVector.Y = MyVector.Y + 1;
	SetActorLocation(LocalVector);

This code only affect in the SetActorLocation and I understand after execution the LocalVector It gets reset every single time the function it’s called.

After that in Video 53 we end with the code:

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

// Move platform forward
	// Get Current location
	FVector CurrentLocation = GetActorLocation();
	// Add vector to that location
		CurrentLocation = CurrentLocation + PlatformVelocity * DeltaTime;
	// Set the location
	SetActorLocation(CurrentLocation);
// Send platform back if gone too far
	// Check how far we've moved
	// Reverse direction of motion if gone too far
}

In that code we will see that the vector gets the actor location and I understend the FVector CurrentLocation It gets reset every single time the function it’s called and in the new tick they get another time the values from actor location and the platform can move.

Sorry for my bad english.

Thank you.

Where would that happen?

Again, where are these variables initialised to 0?

Also what is your question?

Sorry maybe I don’t express correct what I try to say:
“It gets reset every single time the function it’s called”

I’ll modify the post with the correct sentence so maybe it’s clear to understand what I’m trying to say.

My question is why one local variable only work one time and the another works every tick.

Thank you.

I’m not quite sure what you mean, which one isn’t?

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

	FVector LocalVector = MyVector;
	LocalVector.Z = LocalVector.Z + 100;
	MyVector.Y = MyVector.Y + 1;
	SetActorLocation(LocalVector);

Here, the LocalVector only works one time adding 100 units in Z and affect SetActorLocation.

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

// Move platform forward
	// Get Current location
	FVector CurrentLocation = GetActorLocation();
	// Add vector to that location
		CurrentLocation = CurrentLocation + PlatformVelocity * DeltaTime;
	// Set the location
	SetActorLocation(CurrentLocation);
// Send platform back if gone too far
	// Check how far we've moved
	// Reverse direction of motion if gone too far

In these code the vector CurrentLocation works every tick and we can move the platform.

So sorry for my bad english and my bad expression.

Because it’s calling GetActorLocation() which is going to change every frame. The other one is initialised to the same value each frame.

1 Like

Oh god, now I see it clear. Thank you I was three days thinking in that.
Thank you!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.