Hi guys, Im trying to create a simple Actor with replication.
This is my header file
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Clock.generated.h"
UCLASS()
class VISUALTEST_API AClock : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AClock();
UFUNCTION(BlueprintCallable, Category = "Clock")
void UpdateClock();
virtual void GetLifetimeReplicatedProps(TArray< FLifetimeProperty >& OutLifetimeProps) const override;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
FTimerHandle TimerHandle;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Clock", Replicated)
int32 hour;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Clock", ReplicatedUsing = OnRep_Minute)
int32 minute;
UFUNCTION()
void OnRep_Minute();
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
And this is my .cpp file:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Clock.h"
#include "Net/UnrealNetwork.h"
#include "Engine/Engine.h"
// Sets default values
AClock::AClock()
{
// 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;
hour = 8;
minute = 0;
}
// Called when the game starts or when spawned
void AClock::BeginPlay()
{
Super::BeginPlay();
if (GetLocalRole() == ROLE_Authority)
{
GetWorldTimerManager().SetTimer(TimerHandle, this, &AClock::UpdateClock, 7.5f, true);
}
}
// Called every frame
void AClock::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AClock::UpdateClock()
{
minute = minute + 15;
if (minute >= 60)
{
minute = 0;
hour++;
if (hour >= 24)
{
hour = 0;
}
}
}
void AClock::GetLifetimeReplicatedProps(TArray< FLifetimeProperty >& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AClock, hour);
DOREPLIFETIME(AClock, minute);
}
void AClock::OnRep_Minute()
{
FString TimeString = FString::Printf(TEXT("%d:%d"), hour, minute);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TimeString);
}
Everytime I run the engine, I get this error and I have not found too much information on how to solve it:
Assertion failed: bIsValid [LocalPath] [Line: 184] UHT Generated Rep Indices do not match runtime populated Rep Indices for properties in AClock
What I am doing wrong?