How to increment from the tick function c++

I want to increment an Actors Z value from the tick function.

 void UMoveUp::BeginPlay()
 {
Super::BeginPlay();
AActor* Owner = GetOwner();
FVector ActorLocation = Owner->GetActorLocation();
Owner->SetActorLocation(ActorLocation, false) ;

}

      void UMoveUp::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction*
ThisTickFunction)

 { Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
ActorLocation.Z++;
// ...
   }

Sorry for noob question I can increment in blueprint no problem though :frowning:

First, in TickComponent you use ActorLocation but that’s not defined; you need to define that as a member variable and in BeginPlay you would have soemthing like this: ActorLocation = Owner->GetActorLocation();; then it will be available in TickComponent.

Second, after you increment the Z value in your stored ActorLocation, you still need to call SetActorLocation to change the actor location.

Nope, you did: FVector ActorLocation = Owner->GetActorLocation(); which means it’s just a local variable. Once the BeginPlay exists that’s gone. You need to go to the header file and add in FVector ActorLocation = nullptr;. Then in BeginPlay you set the ActorLocation without FVector in front of it.

You need to call RisingBall->SetActorLocation(RisingZ, false); whenever you update the location variable (RisingZ); that means in the TickComponent as well:

RisingZ.Z++;
RisingBall->SetActorLocation(RisingZ, false);

Maybe you’re thinking that SetActorLocation will “link” the passed vector to actor location and whenever that variable changes the actor will be updated automatically; that’s not true. You need to call that function everytime you want to update the actor’s locaiton.

I guess you’re new to programming or just to C++. If yes, you should learn some C++ first. Here’s a course that teaches C++ with Unreal Engine: https://www.udemy.com/unrealcourse/

A pure C++ course it’s recommended too.

I believe I defined it already in the begin play?

#h: private:
AActor* RisingBall = nullptr;
FVector RisingZ = nullptr;

#cpp
// Called when the game starts
void URisingball::BeginPlay()
{
Super::BeginPlay();
RisingBall = GetOwner();
RisingZ = RisingBall->GetActorLocation();
RisingBall->SetActorLocation(RisingZ, false);
// …

}

// Called every frame
void URisingball::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

RisingZ.Z++;
// ...

}

Ok, I changed some names, but still, I cannot get the actor to rise… This does prevent the editor from crashing when I play it though.

#.h
private:
AActor* RisingBall = nullptr;
FVector RisingZ = nullptr;

#.cpp
// Called when the game starts
void URisingball::BeginPlay()
{
	Super::BeginPlay();
	RisingBall = GetOwner();
	RisingZ = RisingBall->GetActorLocation();
	RisingBall->SetActorLocation(RisingZ, false);
	// ...
	
}


// Called every frame
void URisingball::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
	
	RisingZ.Z++;
	// ...
}

Sorry about formatting :frowning:

Thank you. I am new, and funnily enough, I am in that course lol. I guess I should start over, I am just where you start using Unreal. He goes pretty fast though lol.

Rising.Z++;