Search code examples
c++unreal-engine4unreal-blueprint

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


I'm new to UE4 but I have some basic understanding to C++ and I've been developing a FPS Shooter Game which is kinda challenging to me. And here I am, with a problem that is still remain unsolved to me.

So here's a part my code:

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 (Which I think it doesn't matter in this case, but still):

#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;
    
};

And then I have some Blueprint classes created base on the UWeapon class waiting to be assigned to those three variables on FPSCharacter.h which are UWeapon* Primary, UWeapon* Secondary and UWeapon* Tertiary.

As what I intended, I went into FPSCharacter's Blueprint class and assigned those three variables to the Blueprint class I created just for them.

Modifying those values: https://i.sstatic.net/ZpCcc.jpg

Clicking the compile button: https://i.sstatic.net/fXkbp.jpg

After clicking the compile button, all of those variables just went back to default. (With value None)

What is wrong with this? Did I do anything wrong? Please help me with this issue. Thanks in advance ;)


Solution

  • You should add Instanced keyword to UPROPERTY specifiers of weapon variables. That way, an instance of an object will be created and assigned to a property, that saves correctly

    You might also need to add EditInlineNew as a UCLASS specifier to your weapon 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
    }