Object's attribute is reset to NULL

I have BLAttributeSetBase UCLASS:

// BLAttributeSetBase.h
class BAUTILLAND_API UBLAttributeSetBase : public UAttributeSet, public ITickableAttributeSetInterface
{
	GENERATED_BODY()

public:
	UBLAttributeSetBase();

	UPROPERTY(BlueprintReadOnly, Category = "Charges")
	UBLChargesContainer* ChargesContainer;

}

// BLAttributeSetBase.cpp
UBLAttributeSetBase::UBLAttributeSetBase()
{
	ChargesContainer = NewObject<UBLChargesContainer>();
	ChargesContainer->AddAttribute(GetAttackChargeAttribute());
}

UBLChargesContainer is my custom UObject.

BLAttributeSetBase is initialised in ABLPlayerState:

\\ BLPlayerState.h
UCLASS()
class BAUTILLAND_API ABLPlayerState : public APlayerState, public IAbilitySystemInterface
{
private:
	GENERATED_BODY()

public:
	ABLPlayerState();

	// Implement IAbilitySystemInterface
	virtual UAbilitySystemComponent* GetAbilitySystemComponent() const override;

	class UBLAttributeSetBase* GetAttributeSetBase() const;
	
protected:
	UPROPERTY()
	class UBLAbilitySystemComponent* AbilitySystemComponent;

	UPROPERTY()
	class UBLAttributeSetBase* AttributeSet;
};
\\ BLPlayerState.cpp
ABLPlayerState::ABLPlayerState()
{
	AbilitySystemComponent = CreateDefaultSubobject<UBLAbilitySystemComponent>(TEXT("AbilitySystemComponent"));
	AbilitySystemComponent->SetIsReplicated(true);
	AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Full);
	AttributeSet = CreateDefaultSubobject<UBLAttributeSetBase>(TEXT("AttributeSet"));
	// It works	
	check(AttributeSet->ChargesContainer);
}

UAbilitySystemComponent* ABLPlayerState::GetAbilitySystemComponent() const
{
	return AbilitySystemComponent;
}

UBLAttributeSetBase* ABLPlayerState::GetAttributeSetBase() const
{
	// Here it fails: AttributeSet->ChargesContainer == NULL
	check(AttributeSet->ChargesContainer);
	return AttributeSet;
}

I am a new C++ developer, so mayber I just do not understand how it works. I used debugger and checked AttributeSet object address. It was the same. I can’t understand how ChargesContainer is reset to NULL and how to debug this.

Looks like I have found a solution. Constructor is used to create CDO, not each instance of a class. So I moved attributes initialisation to Init() method and explicitly call it.

1 Like