简体   繁体   English

虚幻引擎摘要class实例c++

[英]Unreal engine Abstract class example c++

i have been looking for quite some time online for a good Abstract class (UCLASS(Abstract)) example but haven't came accross a good one yet.我一直在网上寻找一个好的摘要 class (UCLASS(Abstract)) 示例,但还没有找到一个好的示例。

Is there anyone with a good Link i can goto or Anyone who could show me a simple example, i would appreciate alot.有没有人有一个好的链接我可以转到或者任何人可以给我一个简单的例子,我将不胜感激。

WeaponBase.h武器库.h

UCLASS(Abstract, Blueprintable)
class FPS_API AWeaponBase : public AActor
{
    GENERATED_BODY()

public:
    // Sets default values for this actor's properties
    AWeaponBase();

    /** This will be used in sub classes */
    UFUNCTION(BlueprintCallable, Category = "Functions")
    virtual void OnFire(AFPSCharacter* Character);
}

Weapon_Assault.h Weapon_Assault.h

UCLASS()
class FPS_API AWeapon_Assault : public AWeaponBase
{
    GENERATED_BODY()
    
public:

    FVector spread;

    AWeapon_Assault();

};

Weapon_Assault.cpp Weapon_Assault.cpp 文件

#include "Weapon_Assault.h"

AWeapon_Assault::AWeapon_Assault()
{
    AWeaponBase();
    spread = FVector(0.5f, 0.0f, 100.0f);
}

// this function from abstract super class
void OnFire(AFPSCharacter* Character)
{
}

The original code is quite big so i don't want to post it here, but this is basically what it looks like, and i keep getting errors.原始代码很大,所以我不想在这里发布它,但这基本上就是它的样子,而且我不断收到错误。 Also i can't even declare "OnFire" in main class and subclass at the same time?!我什至不能同时在主 class 和子类中声明“OnFire”?!

  1. All class definitions must have ;所有 class 定义必须有; after last } Like this:在最后}之后像这样:
UCLASS(Abstract)
class UAnimalBase : public UObject
{
  GENERATED_BODY()
  public:
    UAnimalBase(const FObjectInitializer& ObjectInitializer);
};
  1. You need to add a declaration of an overridden function to your Weapon_Assault.h您需要将覆盖 function 的声明添加到您的Weapon_Assault.h
UCLASS()
class FPS_API AWeapon_Assault : public AWeaponBase
{
    GENERATED_BODY()
    
public:
    FVector Spread;

    AWeapon_Assault();

    virtual void OnFire(AFPSCharacter* Character) override; // THIS ONE

};

Note that you don't need UFUNCTION() specifier above the overridden function, only on the first declaration.请注意,您不需要在覆盖的 function 之上的UFUNCTION()说明符,仅在第一个声明中。

  1. AWeapon_Assault::AWeapon_Assault() also has a mistake. AWeapon_Assault::AWeapon_Assault()也有错误。 You don't call constructors of parent classes in C++ they will be called automatically.您不调用 C++ 中父类的构造函数,它们将被自动调用。
AWeapon_Assault::AWeapon_Assault()
{
   // AWeaponBase();  THIS LINE IS WRONG
    Spread = FVector(0.5f, 0.0f, 100.0f);
}
  1. Create a definition for your functions in the abstract class (they can stay empty).在摘要 class 中为您的函数创建一个定义(它们可以留空)。 Yes, it doesn't make sense though it does.是的,虽然它确实没有意义。 Abstract is a specifier only inside of UnrealEngine but code still needs to be compiled with C++ standards. Abstract 仅在 UnrealEngine 内部是一个说明符,但代码仍需要使用 C++ 标准进行编译。 The absence of these definitions will cause compile errors.缺少这些定义将导致编译错误。

  2. We use UpperCamelCase or PascalCase in Unreal Coding Standart which is nice to have.我们在Unreal Coding Standart中使用 UpperCamelCase 或 PascalCase,这很好。 But that one is not necessary.但那个是没有必要的。 So your variable should be FVector Spread;所以你的变量应该是FVector Spread; Also you probably shall wrap it with UPROPERTY() macro but that's a different topic.此外,您可能应该用UPROPERTY()宏包装它,但这是一个不同的主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM