OnRep functions not being called in C++?

I’m having this problem in my main project, but I’ve also reproduced it in a more minimal tutorial project I was messing around with a while ago. Here are the relevant bits of code:

Header file:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Target.generated.h"

UCLASS()
class TESTREUBENTARGETGAME_API ATarget : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ATarget();

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

	UFUNCTION()
	void OnRep_Repped();

	UPROPERTY(Transient, ReplicatedUsing = OnRep_Repped)
	float Repped;


protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

CPP file:

#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetSystemLibrary.h"
#include "Net/UnrealNetwork.h"
#include "Target.h"
#include "Math/UnrealMathUtility.h"

// Sets default values
ATarget::ATarget()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	bReplicates = true;

}

void ATarget::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);
	DOREPLIFETIME(ATarget, Repped);
}

void ATarget::OnRep_Repped()
{
	UE_LOG(LogTemp, Warning, TEXT("OnRep_Repped Called"));
}

// Called when the game starts or when spawned
void ATarget::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void ATarget::Tick(float DeltaTime)
{
	UE_LOG(LogTemp, Warning, TEXT("Setting random"));
	Repped = FMath::RandRange(0.0f, 100.0f);
}

Every example I’ve found online, for instance, the network compendium, leads me to believe this is all I need. Yet, I see the “Setting random” log line, but not the “OnRep_Repped Called” log line. I’ve observed the actual value of the replicated field being replicated in my other project, but no matter how I mess around, I can’t get the OnRep function to be called.

Hey did you solve this? I think the problem here is that the On_Reps will only fire if the Value of the variable is being changed and only if it was changed by the server.

Hope that helps.

Hi,

Look at this

he has two parts

mvg

I know this is an old thread, however someone might find my answer useful.

The code presented here is correct, but to make it functional you need to unsure that your actor is replicated and always relevant.

Check the blueprint of your actor and that the checkboxes (“Replicates” and “Always Relevant”) are checked.

1 Like

In Blueprint the server can set a rep_notify variable and it will automatically execute the OnRep function. In C++ this is not the case. Server needs to explicitly call the OnRep function after setting the rep_notify variable.

You can do so via switch has authority (auth) → OnRep function.

Just FYI.

2 Likes