我无法使用蓝图 (UE4) 使用 UPROPERTY(EditAnywhere) 修改我在 C++ 中声明的变量

I can't modify variables I declared in C++ with UPROPERTY(EditAnywhere) using Blueprint (UE4)

我是 UE4 的新手,但我对 C++ 有一些基本的了解,并且我一直在开发一款对我来说有点挑战的 FPS 射击游戏。我在这里,有一个问题对我来说仍然没有解决。

这是我的代码的一部分:

FPSCharacter.h:

#pragma once

...
#include "Weapon.h"
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "FPSCharacter.generated.h"

UCLASS()
class GAME_API AFPSCharacter : public ACharacter
{
    GENERATED_BODY()

public:
...
    
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon")
    UWeapon* Primary;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon")
    UWeapon* Secondary;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon")
    UWeapon* Tertiary;

...
};

Weapon.h(我认为这在这种情况下并不重要,但仍然):

#pragma once

#include "CoreMinimal.h"
#include "Item.h"
#include "Weapon.generated.h"

UCLASS()
class GAME_API UWeapon : public UItem
{
    GENERATED_BODY()

public:

    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item")
    float MagSize = 30.f;

    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item")
    float FireRate;

    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item")
    float BulletRange = 100000000.f;

    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item")
    float BaseDamage = 35.f;

    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Item")
    float DamageDropRate = 0.8f;

protected:

    virtual void Use(class AFPSCharacter* Character) override;
    
};

然后我根据 UWeapon class 创建了一些蓝图 classes,等待分配给 FPSCharacter.h 上的那三个变量,它们是 UWeapon* PrimaryUWeapon* SecondaryUWeapon* Tertiary.

如我所愿,我进入了 FPSCharacter 的蓝图 class 并将这三个变量分配给我专门为它们创建的蓝图 class。

修改这些值: https://imgur.com/a/5TVe2t6

点击编译按钮: https://imgur.com/a/fuauwKn

单击编译按钮后,所有这些变量都恢复为默认值。 (价值None)

这有什么问题吗?我做错了什么吗?请帮我解决这个问题。提前致谢 ;)

您应该将 Instanced 关键字添加到武器变量的 UPROPERTY 说明符中。这样,一个对象的实例将被创建并分配给 属性,正确保存

您可能还需要将 EditInlineNew 作为 UCLASS 说明符添加到您的武器 class。

FPSCharacter.h

UCLASS()
class GAME_API AFPSCharacter : public ACharacter
{
     // Other code...

     UPROPERTY(Instanced, EditAnywhere, BlueprintReadWrite, Category = "Weapon")
     UWeapon* Tertiary;
}

Weapon.h

UCLASS(EditInlineNew)
class GAME_API UWeapon : public UItem
{
    // Weapon stuff
}