add c++ to tick?

Hi all, I am trying to write a function that makes one actor follow another. So I have this:


void ACntrls::FollowActor(AStaticMeshActor* itemA, AStaticMeshActor* itemB)
{

	itemA->SetActorLocation(itemB->GetActorLocation());
	itemA->SetActorRotation(itemB->GetActorRotation());

}


which works fine when the followed actor is static. How best to make the function fire every tick, when it is activated?

I assume i need to call Tick in the function somewhere, but how?

many thanks!

I think using a Pawn instead of an Actor would be better. Since it has this functionality (MoveTo).
Or you can use parenting / attach one actor to another.

Thanks! Parenting/attach sounds like it is what i want. I will look into that. For the sake of argument though, is there a way to add c++ functions to the tick event?

Yes!

In the header file add:



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


In the cpp file add:


// Called every frame
void ACntrls::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
        // Call your functions
        MyFunction1();
        MyFunction2();

}

Thanks! It doesn’t like the type names though, am I messing this up somehow?


void ACntrls::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
	FollowActor(AStaticMeshActor* itemA, AStaticMeshActor* itemB);
}

gives ‘type name is not allowed’ errors on the arguments, and if i remove them it’s ‘too few arguments’.


void ACntrls::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
	FollowActor(itemA, itemB);
}

When you call the function, you do not have to specify type of parametr, that you pass to a function.

That gives me:

‘ItemA is not defined’

undeclared identifier errors on both. Do I need to declare them outside of the other function somewhere?

Thanks again for you time.

//.h

UFUNCTION(BlueprintCallable, Category = "VPCONSTRAINTS")
	static void FollowActor(AStaticMeshActor* itemA, AStaticMeshActor* itemB);

//.cpp

void ACntrls::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
FollowActor(itemA, itemB);
}

void ACntrls::FollowActor(AStaticMeshActor* itemA, AStaticMeshActor* itemB)
{

itemA->SetActorLocation(itemB->GetActorLocation());
itemA->SetActorRotation(itemB->GetActorRotation());

}

Whilst itemA and itemB are declared for the ‘FollowActor’ function, they are not declared for the ‘Tick’ function. Somewhere, you would need a declaration of: -


AStaticMeshActor* itemA
AStaticMeshActor* itemB


If you do this within the ‘Tick’ function, then you would also need some extra code to get the actors you want to move as the code above would be ‘null’. Alternatively, you can put this in the header file. If you’ve created your actors within this class, then this is probably the easiest. However, if you did do this, you need to change the names of ‘itemA’ and ‘itemB’ within the ‘FollowActor’ function as they would clash with the header declared variables; maybe call them ‘Leader’ and ‘Follower’.

.h file


UFUNCTION(BlueprintCallable, Category = "VPCONSTRAINTS")
static void FollowActor(AStaticMeshActor* Follower, AStaticMeshActor* Leader);

UPROPERTY()
AStaticMeshActor* itemA

UPROPERTY()
AStaticMeshActor* itemB


.cpp file


void ACntrls::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
// check that both pointers have been assigned before calling the function
if ((itemA != NULL) && (itemB != NULL))
{
FollowActor(itemA, itemB);
}
}


void ACntrls::FollowActor(AStaticMeshActor* Follower, AStaticMeshActor* Leader)
{

Follower->SetActorLocation(Leader->GetActorLocation());
Follower->SetActorRotation(Leader->GetActorRotation());

} 

Did you use foreward declaration in the header file? Like
class AStaticMeshActor;
If yes, then you have to include “StaticMeshActor.h” in the CPP file or the compiler doesn’t recognize the Object.
Or are itemA and itemB no member-variables of the parent Class? Than the previous answer should be right.