abstract actor,call the subclass's function in father function.

hi,guys.
i want to have an abstract actor.
I’d like to use it as a base actor and expand on top of that as a subclass.
And I want to call the function of the subclass in the base actor, which is actually a virtual function of the base actor.
The pseudocode is as follows:

class ASoldierBase: public AActor
{
public:

    ASoldierBase()
    {
        PrepareBattle();    // I want the function of the subclass to be called here,This eliminates the need to write duplicate code in each subclass
    };

    virtual void PrepareBattle() = 0;

}

class APikemen : public ASoldierBase
{
public:

    PrepareBallte() override
    {
        // do some thing...
    }

}


void main()
{
    APikemen * Pikemen = GetWorld()->SpawnActor<APikemen>();  // Will the APikemen::PrepareBallte function be executed?
    
}

My C++ is not very good, there may be syntax or some basic errors on it, or even impossible to implement, please forgive me.
I looked up some information,but I still don’t know how to implements abstract class.

Hi there,

Your approach is almost correct, but there’s a small issue. In C++, the constructor of the base class won’t call the overridden function in the subclass because the base class constructor is called before the subclass constructor.

Instead, you can use a method that’s called after the object is fully constructed. In Unreal Engine, you can override the BeginPlay method for this purpose.

By moving the call to PrepareBattle to BeginPlay, you ensure that the subclass’s version of the function is called. This way, you don’t need to duplicate code in each subclass.

Hope this helps! If you need more clarification, feel free to ask.

Thank you for your reply. I tried, but failed. The editor told me:
C2259: ASoldierBase : cannot instantiate abstract class.
void ASoldierBase::PrepareBattle(void) : It’s abstract.
this is my code:

UCLASS(Abstract)   //I looked at the class specifier and thought that ”Abstract“ would work, but there was still an error.
class ASoldierBase : public AActor
{
	GENERATED_BODY()
	
public:	
	
    // I didn't call the PrepareBattle function in the constructor
	ASoldierBase();

	virtual void PrepareBattle() = 0;

};

UCLASS()
class APikemen : public ASoldierBase
{
	GENERATED_BODY()
	
public:

	virtual void PrepareBattle() override 
	{
	
	};

};

You’re on the right track with the UCLASS(Abstract) specifier. To fix the issue, make sure you don’t try to instantiate the ASoldierBase class directly. Instead, always instantiate the subclass, like APikemen.

Try something along these lines:

ASoldierBase:

UCLASS(Abstract)
class YOURPROJECT_API ASoldierBase : public AActor
{
    GENERATED_BODY()

public:
    ASoldierBase();

    virtual void BeginPlay() override;

    virtual void PrepareBattle() = 0;  // Pure virtual function
};

ASoldierBase:

ASoldierBase::ASoldierBase()
{
    // Constructor logic here (if any) but dont call PrepareBattle
}

void ASoldierBase::BeginPlay()
{
    Super::BeginPlay();
    PrepareBattle();  // Call the subclass PrepareBattle function
}

APikemen:

UCLASS()
class YOURPROJECT_API APikemen : public ASoldierBase
{
    GENERATED_BODY()

public:
    virtual void PrepareBattle() override
    {
        // Your subclass-specific implementation
    }
};

Example:

void SomeFunction()
{
    APikemen* Pikemen = GetWorld()->SpawnActor<APikemen>();  // This will call APikemen::PrepareBattle in BeginPlay
}

thank you.
Is this a error that only happens when instantiating abstract classes? I update the code with your help. Even if I didn’t put “APikemen” in the level or spawn “APikemen”, the editor would still show this error. I also don’t instantiate “ASoldierBase” anywhere in the program.Does Unreal engine instantiate an “ASoldierBase” by default?

I mean no UE does not instantiate an abstract class by default. The error you’re encountering might be due to another part of your code or an incorrect setup in your project.

Have you checked your logs for any input, if not might be better to start adding debug scripts in or try a clean/rebuild

I created the class with the editor’s “New C++ Class” button. Then I write the code for the two actors and didn’t do anything else.This error appeared at the end of the compilation.

This is incorrect. Unreal constructs an instance of every UCLASS and USTRUCT in order to act as the Class Default Object for that type. The Abstract specifier will cause the engine to prevent any further instances from being created, but at least 1 does need to be created.

This limitation is why pure virtual functions (virtual = 0) are not allowed in UCLASS’s. You can emulate them by using the PURE_VIRTUAL macro the engine provides, but it will only generate an error when non-overriden function is called. You can declare them in interfaces, but the same limitation means that your UCLASS has to provide an implementation of the pure virtual functions in order to compile properly.

@CorgiofLiu Marking your ASoldierBase class as Abstract and providing an empty implementation of PrepareBattle when you initially declare it virtual is the expected setup. You can use the PURE_VIRTUAL macro I mentioned before if you want.

You are right, thank you very much.
I looked up the material about “PURE_VIRTUAL” and improved my code. now the editor doesn’t show any errors.
Thanks to everyone for their help.:smiling_face_with_three_hearts:

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