Blueprint to c++ problems

Hi, I have this MaxHealth variable that has the EditAnywhere specifier. The problem is, should i code the update of the MaxHealth variable value if i change it in the blueprints?
Another problem is, when i start the editor i have the max health (100) but when i restart it, instead of setting health to 100, it remains the value that was in the old instance (so if i end with 50 of health, i’ll restart the next time with 50 of health instead of 100) How do i solve this problem?
If you make any suggestions, please make them compatible with a multiplayer structure, thanks.

The code:

HealthComponent.h

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "HealthComponent.generated.h"

UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class ATTRITION_API UHealthComponent : public UActorComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UHealthComponent();
	
	UFUNCTION(BlueprintPure)
	int GetHealth() const { return Health; }
	UFUNCTION(BlueprintCallable, Category="Health")
	void SetHealth(const int NewHealth) { Health = NewHealth; }

	UFUNCTION(BlueprintPure)
	int GetMaxHealth() const { return MaxHealth; }
	UFUNCTION(BlueprintCallable, Category="Health")
	void SetMaxHealth(const int NewMaxHealth) { MaxHealth = NewMaxHealth; }

protected:
	UPROPERTY(Replicated)
	int Health;

	UPROPERTY(Replicated, EditAnywhere, Category="Variable", meta=(AllowPrivateAccess=true))
	int MaxHealth;
	
private:
	virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty> &OutLifetimeProps) const override;
};

HealthComponent.cpp

#include "Components/HealthComponent.h"
#include "Net/UnrealNetwork.h"

// Sets default values for this component's properties
UHealthComponent::UHealthComponent()
{
	PrimaryComponentTick.bCanEverTick = false;
	PrimaryComponentTick.bStartWithTickEnabled = false;
	
	SetIsReplicatedByDefault(true);

	Health = MaxHealth;
}

void UHealthComponent::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);

	DOREPLIFETIME_CONDITION(UHealthComponent, Health, COND_OwnerOnly);
	DOREPLIFETIME(UHealthComponent, MaxHealth)
}

You are not setting the default values in the header for Health or MaxHealth.

You can change the code to

protected:
	UPROPERTY(Replicated)
	int Health = 100;

	UPROPERTY(Replicated, EditAnywhere, Category="Variable", meta=(AllowPrivateAccess=true))
	int MaxHealth = 100;

And it should set both Health and MaxHealth to a 100;

Also I would advise to make SetHealth and SetMaxHealth

UFUNCTION(Server, Reliable, WithValidation)

with the implementation side having (example for max health) OFC you need the cpp file and can’t just use a header file then so you need to move the implementation to not be inline.

void ATTRITION_API ::SetMaxHealth_Implementation(const int NewMaxHealth) 
{ 
if (GetLocalRole() == ROLE_Authority) {
     MaxHealth = NewMaxHealth; 
}
}
bool ATTRITION_API ::SetMaxHealth_Validate(const int NewMaxHealth) 
{ 
if(NewMaxHealth < 10000) // eg: 10000 would be max limit otherwise cheater
return true; 
else 
return false;
}

Yeah i saw that, the point i’m trying to make is that i want the value to be whatever is written in the blueprint, so if i set there 100 it should be 100 in that specific instance, while if i set 200 on another blueprint it would 200 in that other blueprint, instead of hard-wiring the 100 health constant
Thanks for the comment anyway, appreciate the help

If you mark the UPROPERTY with BlueprintReadWrite you can modify the value once it’s placed in the world.
example:

UPROPERTY(BlueprintReadWrite, EditAnywhere)
int32 Health;

That way you can place 2 instances of the actor in the scene and put their health at different states.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.