Dienstag, 21. April 2015

Unreal Engine 4 - Pure virtual functions

Unreal Engine 4 - Pure virtual functions

This is a little guide for everyone who tries to make use of pure virtual functions in Unreal Engine:

You cannot simply create an abstract UCLASS(), because Unreal Engine expects every UClass Element to be instantiable. Therefore you have to use the PURE_VIRTUAL macro. This is a small extract from my current Pokemon game I'm working on:

UCLASS(abstract)
class UE4POKEMON1_API ABaseActor : public AActor
{
    GENERATED_BODY()

protected:
    virtual void SetupAssets() PURE_VIRTUAL(ABaseActor::SetupAssets, ;);
    ...
}

You can mark the UCLASS as abstract. In this case "SetupAssets" is a void function so after the comma after the declaration "ABaseActor::SetupAssets" in PURE_VIRTUAL, you can leave it free. If you have any other return type for example int32: you can write "return 4;".
You don't have to implement the function in the .cpp file, but you have to pay attention, if you try to call that function from the constructor. If you do this, the following error occures:

"Pure virtual not implemented"
 To avoid this you can call this function in BeginPlay() (not that elegant) or in the constructor of the deriving class (better).

1 Kommentar:

  1. Seems like the ";" at the end are not necessary. Staff member said in the Answer hub.

    AntwortenLöschen