UE4 – Tip of the Day 2: Get the previous value of any variable after being replicated

Did you know…

You can get the previous value of any variable after being replicated. Just add one parameter to the OnRep_ function with the same type of the replicated variable. When the OnRep_ function is called, the variable will have the new value, and the parameter will have the previous value.

Example: Let’s create the Health variable and the OnRep_Health function as RepNotify

//File: TipOfTheDayCharacter.h
//...
public:

UPROPERTY(BlueprintReadWrite, ReplicatedUsing = OnRep_Health, Category = "Stats")
float Health;

UFUNCTION()
void OnRep_Health(float LastHealthValue);

void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;

//...

//File: TipOfTheDayCharacter.cpp

//...

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

	DOREPLIFETIME(ATipOfTheDayCharacter, Health);
}

void ATipOfTheDayCharacter::OnRep_Health(float LastHealthValue)
{
	UE_LOG(LogTemp, Log, TEXT("Current Health Value: %f. Last Health Value: %f"), Health, LastHealthValue);
}

//...

Imagine that the Health variable is equal to 100 and the server change its value to 90. When Health variable is modified by the server, the OnRep_Health is called on remote clients, printing in the ouput log the following text: “Current Health Value: 90.0 Last Health Value: 100.0″

😉

WARNING: English is not my first language, because of that, this text may have some grammatical errors.

Note : This post has been developed using Unreal Engine 4.12.5 . If you are working with another version, you may find some differences since the engine is constantly updating. If so, leave me your comments at the end of the post and we’ll look for a solution together.

Spanish version: UE4 – Consejo del día 2: Obtener el valor que tenía una variable antes de ser replicada

Fernando Castillo Coello
Follow me

Fernando Castillo Coello

Gameplay Programmer at GameOlic
I've been into games development with Unreal Engine since 2014 and want to share with you some of the tips and tricks that I learned during my journey.
Fernando Castillo Coello
Follow me

One thought on “UE4 – Tip of the Day 2: Get the previous value of any variable after being replicated

  1. Ignacio

    Hola… buenos tutoriales 😀
    Yo ocupo BP con el plugin Advanced Sessions, etc…
    Y tengo una pregunta hace ya mucho tiempo que espero puedas responder si es que puedes.
    Puedo “crear” un servidor propio asi como los que usan las empresas grandes? (tampoco un sv tan profesional pero por ejemplo que un amigo que vive en otra ciudad se pueda conectar a mi sv y podamos jugar?)
    Gracias de Antemano :)

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>