Download

Defining / Declaring RPC calls is a bit odd (Correct?).

So am just wondering if am doing somthing wrong (did i forget somthing?)
So in my Character class i have made a function.



	UFUNCTION(Reliable, Server, WithValidation, BlueprintCallable, Category = "Player Health Interface")
		void DecresePlayerHealth(float HealthModifyedValue); // This function Subtract from the players current health. (Use TakeDamage to apply damage.).


And what happens if i define this function is that i get a error telling me its redefined.
So i removed the Defenition from my .cpp file and left only.



bool AMyProjectPlayerClass::DecresePlayerHealth_Validate(float HealthModifyedValue)
{
	return true;
}

void AMyProjectPlayerClass::DecresePlayerHealth_Implementation(float HealthModifyedValue)
{
	const float HealthCheck = (Health - HealthModifyedValue);
	if (HealthCheck >= 0)
	{
		Health = HealthCheck;
	}
	else if (HealthCheck < 0)
	{
		Health = 0.f;
                // KillPlayer();
	}
	else if (HealthCheck == 0)
	{
                // KillPlayer();
	}

	if (Health == 0)
	{
                // KillPlayer();
	}

}

And i call it in my TakeDamage implementation.


float AMyProjectPlayerClass::TakeDamage(float Damage, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, class AActor* DamageCauser)
{
	Super::TakeDamage(Damage, DamageEvent, EventInstigator, DamageCauser);

        DecresePlayerHealth(Damage);
	return Damage;
}

This works and i get the expected results its just in every example i seen the


if (Role < ROLE_Authority)

Am i suposed to check that in TakeDamage(…) ?

Anyone have any tips or pointers on the subject?

Hey. I’m also just starting to learn this part, but as far as I could gather from the Blueprint Networking Tutorial series, I’ll put the “if (Role < ROLE_Authority)” part in the class “applying” the damage, ie. the rocket or bullet. When the rocket/bullet fires the “OnHit” method you put the “if (Role < ROLE_Authority)” in, which will then fire the “ApplyDamage” method depending on the Role. I’ll also put the “if (Role < ROLE_Authority)” in your “TakeDamage” method as a second layer of protection. Hope I got this right :slight_smile:

So had a 3rd look at this and it seems that you need two functions?
One thats just for “local” use and then call the server implimentation also?

On the wiki A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums
He has the SetSomeBool(.) and ServerSetSomeBool(.)
And he calls the SetSomeBool and set it to the new boolean value.
And then checks the Authority if (Role < ROLE_Authority)

And if so then it calls the RPC method witch in turn calls the SetSomeBool again.
Very confusing indeed wont it just keep calling the fucntion over and over again ? :P.

I be very greatfull for a short explination on the logic behind it.

So from the Wiki Example, maybe it would be easier if I just add some comments in there to explain!



void AYourPlayerController::SetSomeBool(bool bNewSomeBool)
{
    // Change the value of the bSomeBool property
    bSomeBool = bNewSomeBool;
 
    // If this next check succeeds, we are *not* the authority, meaning we are a network client.
    // In this case we also want to call the server function to tell it to change the bSomeBool property as well.
    if (Role < ROLE_Authority)
    {
        ServerSetSomeBool(bNewSomeBool);
    }
}
 
void AYourPlayerController::ServerSetSomeBool_Implementation(bool bNewSomeBool)
{
    // This function is only called on the server (where Role == ROLE_Authority), called over the network by clients.
    // We need to call SetSomeBool() to actually change the value of the bool now!
    // Inside that function, Role == ROLE_Authority, so it won't try to call ServerSetSomeBool() again.
    SetSomeBool(bNewSomeBool);
}


Hopefully that clears things up! I’ll go edit the Wiki as well!

P.S. - In this example there is an implicit assumption that only the client is triggering a change in the bool value, since there is no way for the server to send the value to clients.

Thank you Mr Middleton aprichiate the explination.
I think i sort of get it now,


if (Role < ROLE_Authority)

Is what we do to make sure the value gets changes on all clients.

If it was like this


if (Role >= ROLE_Authority) // prob should be == 

Then “we” are the server and we are all good. ?

Yep, that’s correct!