I am trying to add crouch and prone functionality to my character.
i have tested if all the functions gets run correctly with logs, but still the bIsCrouching and bIsProning variable i made doesn’t seem to replicate. And the DoJump() function also doesn’t seem to replicate.
MyCharacter.cpp
// © The Survival Games Team
#include "TheSurvivalGames.h"
#include "MyCharacter.h"
#include "Engine.h"
#include "UnrealNetwork.h"
// Sets default values
AMyCharacter::AMyCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
bReplicates = true;
bReplicateMovement = true;
// Finds all Variables With Replicated Property And Replicates them
void AMyCharacter::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AMyCharacter, bIsCrouchButtonDown);
DOREPLIFETIME(AMyCharacter, bIsProneButtonDown);
}
// Validation of Replicated Functions
bool AMyCharacter::ServerStartCrouchMode_Validate()
{
return true;
}
bool AMyCharacter::ServerStopCrouchMode_Validate()
{
return true;
}
bool AMyCharacter::ServerStartProneMode_Validate()
{
return true;
}
bool AMyCharacter::ServerStopProneMode_Validate()
{
return true;
}
bool AMyCharacter::ServerJump_Validate()
{
return true;
}
// Handles Jumping
void AMyCharacter::Jump()
{
if (CanJump())
{
if (Role < ROLE_Authority)
{
ServerJump_Implementation();
}
GetCharacterMovement()->DoJump(true);
}
}
void AMyCharacter::ServerJump_Implementation()
{
AMyCharacter::MulticastJump();
}
void AMyCharacter::MulticastJump_Implementation()
{
GetCharacterMovement()->DoJump(true);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Jumped Executed With Multicast");
}
/************ HANDLES CROUCHING **************/
/* Start Crouching */
void AMyCharacter::StartCrouchMode()
{
if (Role < ROLE_Authority)
{
ServerStartCrouchMode_Implementation();
}
if (bIsProneButtonDown != true )
{
setIsCrouching(true);
}
}
/* Server RPC For Starting Crouching. Calls The NetMultiCast From The Server */
void AMyCharacter::ServerStartCrouchMode_Implementation()
{
NetMultiCast_StartCrouchMode_Implementation();
}
/* Show Everyone That I Am Crouching */
void AMyCharacter::NetMultiCast_StartCrouchMode_Implementation()
{
if (bIsProneButtonDown != true)
{
setIsCrouching(true);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "StartCrouchMode Executed On Server");
}
}
/* Stop Crouching */
void AMyCharacter::StopCrouchMode()
{
if (Role < ROLE_Authority)
ServerStopCrouchMode_Implementation();
setIsCrouching(false);
}
/* Server RPC For Stopping Crouch. Calls The NetMultiCast From The Server*/
void AMyCharacter::ServerStopCrouchMode_Implementation()
{
NetMultiCast_StopCrouchMode_Implementation();
}
/* Show Everyone That I Am NOT Crouching */
void AMyCharacter::NetMultiCast_StopCrouchMode_Implementation()
{
setIsCrouching(false);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "StopCrouchMode Executed On Server");
}
//************ HANDLES PRONING ***************\\
/* Start Proning */
void AMyCharacter::StartProneMode()
{
// Checks if player has server authority //
if (Role < ROLE_Authority)
ServerStartProneMode_Implementation();
// If Crouch Button Is Down Do Nothing. If Not, Then Prone //
if (bIsCrouchButtonDown != true)
{
// Start Proning
setIsProning(true);
}
}
/* Server RPC For Starting Prone. Calls The NetMultiCast From The Server */
void AMyCharacter::ServerStartProneMode_Implementation()
{
NetMultiCast_StartProneMode_Implementation();
}
/* Show Everyone That I Am Proning */
void AMyCharacter::NetMultiCast_StartProneMode_Implementation()
{
if (bIsCrouchButtonDown != true)
{
setIsProning(true);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "StartProneMode Executed On Server");
}
}
/* Stop Proning */
void AMyCharacter::StopProneMode()
{
if (Role < ROLE_Authority)
ServerStopProneMode_Implementation();
setIsProning(false);
}
/* Server RPC For Stopping Prone. Calls The NetMultiCast From The Server*/
void AMyCharacter::ServerStopProneMode_Implementation()
{
NetMultiCast_StopProneMode_Implementation();
}
/* Show Everyone That I Am NOT Proning */
void AMyCharacter::NetMultiCast_StopProneMode_Implementation()
{
setIsProning(false);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "StopProneMode Executed On Server");
}
/* Sets Crouching To True Or False */
void AMyCharacter::setIsCrouching(bool boolean)
{
bIsCrouchButtonDown = boolean;
}
/* Sets Proning To True Or False */
void AMyCharacter::setIsProning(bool boolean)
{
bIsProneButtonDown = boolean;
}
// MyCharacter.h
// © The Survival Games Team
#pragma once
#include "GameFramework/Character.h"
#include "Engine.h"
#include "OnlineSubsystem.h"
#include "MyCharacter.generated.h"
UCLASS()
class THESURVIVALGAMES_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AMyCharacter();
// Crouching
UPROPERTY(Replicated, EditAnywhere, BlueprintReadWrite, Category = "Movement")
bool bIsCrouchButtonDown;
UPROPERTY(EditDefaultsOnly, Category = "Movement")
float MaxCrouchSpeed;
UFUNCTION()
void StartCrouchMode();
UFUNCTION()
void StopCrouchMode();
UFUNCTION(Server, reliable, WithValidation)
void ServerStartCrouchMode();
void ServerStartCrouchMode_Implementation();
bool ServerStartCrouchMode_Validate();
UFUNCTION(Server, reliable, WithValidation)
void ServerStopCrouchMode();
void ServerStopCrouchMode_Implementation();
bool ServerStopCrouchMode_Validate();
UFUNCTION(NetMulticast, unreliable)
void NetMultiCast_StartCrouchMode();
void NetMultiCast_StartCrouchMode_Implementation();
UFUNCTION(NetMulticast, unreliable)
void NetMultiCast_StopCrouchMode();
void NetMultiCast_StopCrouchMode_Implementation();
UFUNCTION( Category = "Movement")
void setIsCrouching(bool b);
// Proning
UPROPERTY(Replicated, EditAnywhere, BlueprintReadWrite, Category = "Movement")
bool bIsProneButtonDown;
UPROPERTY(EditDefaultsOnly, Category = "Movement")
float MaxProneSpeed;
UFUNCTION()
void StartProneMode();
UFUNCTION()
void StopProneMode();
UFUNCTION(Server, reliable, WithValidation)
void ServerStartProneMode();
void ServerStartProneMode_Implementation();
bool ServerStartProneMode_Validate();
UFUNCTION(Server, reliable, WithValidation)
void ServerStopProneMode();
void ServerStopProneMode_Implementation();
bool ServerStopProneMode_Validate();
UFUNCTION(NetMulticast, unreliable)
void NetMultiCast_StartProneMode();
void NetMultiCast_StartProneMode_Implementation();
UFUNCTION(NetMulticast, unreliable)
void NetMultiCast_StopProneMode();
void NetMultiCast_StopProneMode_Implementation();
UFUNCTION(Category = "Movement")
void setIsProning(bool b);
// Jumping
UFUNCTION()
virtual void Jump();
UFUNCTION(Server, reliable, WithValidation)
virtual void ServerJump();
virtual void ServerJump_Implementation();
bool ServerJump_Validate();
UFUNCTION(NetMulticast, reliable)
virtual void MulticastJump();
virtual void MulticastJump_Implementation();
};