How to replicate a variable in C++?

I am trying to replicate a variable bCrouch. but on Play the engine crash.
My implementation:

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

	DOREPLIFETIME(AMyCharacter, bCrouch);
}

The file openes automatically after the engine crash:

// Setup lifetime replicated properties
	for ( int32 i = 0; i < LifetimeProps.Num(); i++ )
	{
		// Store the condition on the parent in case we need it
		Parents[LifetimeProps[i].RepIndex].Condition			= LifetimeProps[i].Condition;
		Parents[LifetimeProps[i].RepIndex].RepNotifyCondition	= LifetimeProps[i].RepNotifyCondition;

		if ( Parents[LifetimeProps[i].RepIndex].Flags & PARENT_IsCustomDelta )
		{
			continue;		// We don't handle custom properties in the FRepLayout class
		}

		Parents[LifetimeProps[i].RepIndex].Flags |= PARENT_IsLifetime;

		if ( LifetimeProps[i].RepIndex == RemoteRoleIndex )
		{
			// We handle remote role specially, since it can change between connections when downgraded
			// So we force it on the conditional list
			check( LifetimeProps[i].Condition == COND_None );
			LifetimeProps[i].Condition = COND_Custom;
			continue;		
		}

		if ( LifetimeProps[i].Condition == COND_None )
		{
			Parents[LifetimeProps[i].RepIndex].Flags &= ~PARENT_IsConditional;
		}			
	}

What I am doing wrong?

Did you mark the variable as replicated in .h?

UPROPERTY(Replicated)
bool bCrouch;
2 Likes

Thank You very much Sir, You just solved my problem :smiley: