UFunction problem

NetMulticast is from server to client, and Server is from client to server, so you don’t need both there. If you already use Server you cannot use BlueprintCallable, you need to make extra function with BlueprintCallable and just call the function setHealth.

I’m getting errors while trying to set up C++ functions to be accessed from blueprints: I have no idea what I’m supposed to do to get this to work…

cpp:

AWTCPlayerState::AWTCPlayerState(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{

	bReplicates = true;
}

void AWTCPlayerState::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
	DOREPLIFETIME(AWTCPlayerState, Health); 
	//DOREPLIFETIME(AWTCPlayerState, setHealth());
}

int32 AWTCPlayerState::getHealth(){
	return Health;
}
void AWTCPlayerState::setHealth(int32 HP){
	
	if (Role < ROLE_Authority)
	{
		Health = HP;
	}
}

bool AWTCPlayerState::setHealth_Validate(int32 HP){ return true; }
void AWTCPlayerState::setHealth_Implementation(int32 HP){ Health = HP; }

.h:

public:
	AWTCPlayerState(const FObjectInitializer& ObjectInitializer);

	UPROPERTY(BlueprintReadWrite, Category = "Stats", EditAnywhere, Replicated)
		int32 Health;
	UFUNCTION(BlueprintCallable, Category = "Getters and Setters")
		int32 getHealth();
	UFUNCTION(BlueprintCallable, Reliable, Server, WithValidation, NetMulticast, Category = "Getters and Setters")
		void setHealth(int32 HP);
		virtual void setHealth_Implementation(int32 HP);
		virtual bool setHealth_Validate(int32 HP);

Do i even need the: WTCPlayerState::setHealth_Implementation and validation functions? If so what am i supposed to put in them?

yeah you need that, basically if setHealth is called in client, setHealth_Validate will validate the function, if you don’t need to check anything just return true here coz return false will terminate the client connection to server. And then setHealth_implementation will be executed in server.

For any with BlueprintCallable, it will also create another setHealth_implementation in generated.h file, what you call in blueprint will be setHealth_implementation, cause this 2 is conflict you have to use server and blueprintcallable separately.

For my game, i normally put Server in front any server function like ServerSetHealth, then Blueprintcallable SetHealth just call ServerSetHealth.

So, if in blueprint i want to set the health so that only the server can set health, I would have to do this?

void AWTCPlayerState::setHealth(int32 HP){
	
	if (Role < ROLE_Authority) // Should this be '==' ?
	{
		ServersetHealth(HP);
	}
}
void AWTCPlayerState::ServersetHealth(int32 HP){} //This can be ignored correct?
bool AWTCPlayerState::ServersetHealth_Validate(int32 HP){ return true; }
void AWTCPlayerState::ServersetHealth_Implementation(int32 HP){ Health = HP; }

Yeah correct, because you call ServersetHealth from client which Role < ROLE_Authority, and the implementation will be executed in server. If you put Role == ROLE_Authority and call ServersetHealth, the implementation will never be executed.

can you find getHealth ?

strange, after add a new function do you rebuild solution or use build solution ?

I have setHealth set to Blueprint callable, but i still cant find or call it in blueprints.

No, i cant. When i creates the blueprint, i used the c++ class as the parent.

In the details or default tab in blueprints, i do see the value health, but i can’t do anything with it

I compile the code using the button in the editor

when you add new function, you need to close the editor, rebuild in visual studio then open editor again, hot reload only work when you change some code in function.

Worked thanks! I only had to restart the editor the first time though.