Hey there,
I got a simple question about character movement being replicated over a network.
I created a custom character movement component and added it inside the constructor like this:
// Sets default values
AZA_PlayerCharacter::AZA_PlayerCharacter(const FObjectInitializer& ObjectInitializer)
:Super(ObjectInitializer.SetDefaultSubobjectClass<UZA_PlayerMovement(ACharacter::CharacterMovementComponentName))
{
// Code here ...
}
The playercharacter class inherites from an AZA_AbstractCharacter because I also have an enemy class and he will inherit from this class too. The constructor of this class looks like this:
// Sets default values
AZA_AbstractCharacter::AZA_AbstractCharacter(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
// Code here ...
}
I also set the booleans Replicates and Replicate Movement to true inside my character.
And the last thing I did was creating some server methods for all my movement.
For example:
void UZA_CharacterMovement::SetSprint(bool bNewSprint)
{
_bIsSprinting = bNewSprint;
//We stop aiming when we are sprinting
if (_bIsSprinting)
StopAim();
//OrientRotation to movement is the opposite of the sprinting variable
bOrientRotationToMovement = !_bIsSprinting;
//Let the character only rotate over the yaw axis
GetOwningCharacter()->bUseControllerRotationYaw = _bIsSprinting;
MaxWalkSpeed = bNewSprint ? _doubleMaxWalkSpeed : _defaultMaxWalkSpeed;
if (GetOwningCharacter()->Role < ROLE_Authority)
ServerSetSprint(bNewSprint);
}
Now this method gets fired from the character controller, because according to the unreal documentation:
PlayerControllers are used by human players to control Pawns.
In other words when I press the right mouse button the controller fires a method like this:
void AZA_PlayerController::StartSprint()
{
if (GetOwningPlayerMovement())
GetOwningPlayerMovement()->StartSprint();
}
And this method will tell the movementcomponent that the player wants to sprint and set the sprinting boolean to true.
Also this variable is replicated like this:
Header:
UPROPERTY(BlueprintReadOnly, Category = Movement, Replicated)
bool _bIsSprinting;
Cpp:
void UZA_CharacterMovement::GetLifetimeReplicatedProps(TArray< class FLifetimeProperty > & OutLifetimeProps) const
{
DOREPLIFETIME_CONDITION(UZA_CharacterMovement, _bIsSprinting, COND_SkipOwner);
}
You can see that this method is fired from a base class CharacterMovement, and that the playercontroller uses a class called UZA_PlayerMovement this is because Playermovement as some extra behaviour that is needed to handle the camera. and when I apply UZA_CharacterMovement to an enemy he only needs the movementbehaviour and not the camera handling because an enemy does not have a camera.
Maybe I forgot something but I checked everything exept the right thing ofcourse -_-’
Kind Regards
Dyronix