Errors with C++ Variable Replication

Hey everyone,

So I’ve run across this weird issue where if I make a UPROPERTY() variable like

UPROPERTY(Replicated)
int Score;

I get a bunch of errors vomited back at me and I can’t make heads or tails of them. Here they are:

Severity	Code	Description	Project	File	Line	Suppression State
Error	LNK1120	1 unresolved externals	MyProject	D:\Personal Files\Documents\Unreal Projects\ProjectArtemis\Binaries\Win64\UE4Editor-MyProject-7575.dll	1	

Severity	Code	Description	Project	File	Line	Suppression State
Error	LNK2001	unresolved external symbol "public: virtual void __cdecl ABaseCharacter::GetLifetimeReplicatedProps(class TArray<class FLifetimeProperty,class FDefaultAllocator> &)const " (?GetLifetimeReplicatedProps@ABaseCharacter@@UEBAXAEAV?$TArray@VFLifetimeProperty@@VFDefaultAllocator@@@@@Z)	MyProject	D:\Personal Files\Documents\Unreal Projects\ProjectArtemis\Intermediate\ProjectFiles\BaseCharacter.cpp.obj	1	

Can anyone help me out with this?

Well, for starters, you shouldn’t use “int”. Use int32.

You’re getting a linker error. You should try to toggle the replication field in UPROPERTY() macro and see if the linker error can be toggled on and off. If it can be, that narrows down the scope of your investigation.

Whenever I remove the Replicated field from the UPROPERTY(), the error goes away. And when I reenter the field, the error comes back. Not sure what else to go on though. I’ve seen certain guides and it seems that all you need is that Replicated field in the UPROPERTY().

You need to add the function GetLifetimeReplicatedProps to your cpp file to describe the replicated variable. You may also need to include UnrealNetwork.h

Further reading:

https://www.unrealengine.com/blog/network-tips-and-tricks

.

Here is the example from the ShooterGame project:

.cpp

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

	// only to local owner: weapon change requests are locally instigated, other clients don't need it
	DOREPLIFETIME_CONDITION(AShooterCharacter, Inventory, COND_OwnerOnly);

	// everyone except local owner: flag change is locally instigated
	DOREPLIFETIME_CONDITION(AShooterCharacter, bIsTargeting, COND_SkipOwner);
	DOREPLIFETIME_CONDITION(AShooterCharacter, bWantsToRun, COND_SkipOwner);

	DOREPLIFETIME_CONDITION(AShooterCharacter, LastTakeHitInfo, COND_Custom);

	// everyone
	DOREPLIFETIME(AShooterCharacter, CurrentWeapon);
	DOREPLIFETIME(AShooterCharacter, Health);
}

After a spending a little bit of time, I managed to get everything down! The project now compiles without a hitch. Thanks a lot!