Tick on spline component can't override

Hi guys ! (it’s me again…)

I’m currently working on UE 4.7.6 and I’m stuck with something. I made my custom USplineComponent class in order to add some features to it. Everything is fine on that side.
In order to display some information, I wanted to use Tick but oddly when I try to override Tick, it says that UActorComponent::Tick was declared as final (so of course, overriding is a no-go). I never encountered this problem before.

Can’t we use Tick on components anymore ? I looked around the answerhub for clues but most people just had to add bCanEverTick = true; to solve it.

Or maybe it is USplineComponent itself ? I am facing another problem with it : Error 2 error C2512: ‘USplineComponent’ : no appropriate default constructor available

Here’s my code :

#include "Components/SplineComponent.h"
#include "MySplineComponent.generated.h"

UCLASS()
class MyGame_API UMySplineComponent : public USplineComponent
{
	GENERATED_BODY()

public:
	UMySplineComponent(const FObjectInitializer& ObjectInitializer)
	{
		PrimaryComponentTick.bCanEverTick = true;
		bTickInEditor = true;
	}

#if WITH_EDITOR	
	int32 GetSelectedSplinePoint();  	
#endif	
	
        virtual void Tick(float DeltaSeconds) override;
	
        UPROPERTY(EditAnywhere, Category = "Debug Point")
	    FVector CurrentPoint; 

};

Any ideas ?

1 Like

I’m surprised nobody has answered this yet.
That error is telling you that your class doesn’t contain a default constructor (constructor with no args).
To fix it, modify your class like this:

UCLASS()
 class MyGame_API UMySplineComponent : public USplineComponent
 {
     GENERATED_BODY()
 
 public:
     UMySplineComponent();
     UMySplineComponent(const FObjectInitializer& ObjectInitializer)
     {
         PrimaryComponentTick.bCanEverTick = true;
         bTickInEditor = true;
     }
     //...

And then in your CPP file, don’t forget to include the implementation.
If you’re not sure what to put in it, just remember that default constructors are meant to initialize default variables and data. So for example, you would use it to set all member variable pointers to nullptr, or set all FVectors to be FVector::ZeroVector… I’m sure you get the idea.

In your case, I would also use it to call the default constructor on the superclass Super::USplineComponent();

I added the default constructor but still get the same error: final means “can’t override” or am I wrong?

The first, USplineComponent is a UActorComponent, you should override the TickComponent method instead of Tick method. Because the Tick method is defined as final in base class of UActorComponent. You can refer the source code of ActorComponent.h for this problem.

Second, you use GENERATED_BODY() macro, so you should define UMySplineComponent::UMySplineComponent() { … } as default constructor. If you want use the UMySplineComponent(const FObjectInitializer& ObjectInitializer) as your constructor, you should use GENERATED_UCLASS_BODY() macro. And you don’t have to declare the constructor with FObjectInitializer in your header file.

Please try this:

/* Header file */
UCLASS()
 class MyGame_API UMySplineComponent : public USplineComponent
 {
     GENERATED_BODY()
 
 public:
     UMySplineComponent();

      virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction);
     //...
/* Source File */
UMySplineComponent::UMySplineComponent()
{
    PrimaryComponentTick.bCanEverTick = true;
    bTickInEditor = true;
}

void UMySplineComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
   Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

   ...
}

OR

/* Header file */
UCLASS()
 class MyGame_API UMySplineComponent : public USplineComponent
 {
     GENERATED_UCLASS_BODY()
 
 public:

      virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction);
     //...
/* Source File */
UMySplineComponent::UMySplineComponent(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
{
    PrimaryComponentTick.bCanEverTick = true;
    bTickInEditor = true;
}

void UMySplineComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
   Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

   ...
}