Hi all, I have read a lot of documentation and other forum posts about this, but I can not get my replication to work by what I have learned so far, so I need to ask.
When player pressed button, character plays anim montage (to slash his sword).
Everything works good but the replication does not work correctly.
Server can see himself slashing his sword
Server can see all clients using their swords.
Client can see himself using his sword
Client can not see any other player using his sword.
So this is the relevant stuff of my character header file:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "DrawDebugHelpers.h"
#include "RSCharacter.generated.h"
//Using delegates to trigger replicated blueprint events?
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FSlashDelegate);
UCLASS()
class CONCEPT17_API ARSCharacter : public ACharacter
{
GENERATED_BODY()
public:
//Function for replicating variables
void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const;
// Sets default values for this character's properties
ARSCharacter();
protected
//I dont need this yet, but maybe later?
UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated)
bool bPressedSlash;
UFUNCTION(BlueprintCallable, Category = "Attacks")
void PressedSlash();
UFUNCTION(reliable, server, WithValidation, Category = "Attacks")
void ServerPressedSlash();
virtual void ServerPressedSlash_Implementation();
virtual bool ServerPressedSlash_Validate();
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Attacks")
void SlashEvent();
void SlashEvent_Implementation();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(BlueprintAssignable, BlueprintCallable, Category = "Attacks")
FSlashDelegate SlashDelegate;
};
Cpp
#include "RSCharacter.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "UnrealNetwork.h"
void ARSCharacter::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
// Replicate to everyone
DOREPLIFETIME(ARSCharacter, bPressedSlash);
}
// Sets default values
ARSCharacter::ARSCharacter()
{
bPressedSlash = false;
}
void ARSCharacter::BeginPlay()
{
Super::BeginPlay();
//Add SlashEvent to EventDelegate
SlashDelegate.AddDynamic(this, &ARSCharacter::SlashEvent);
}
void ARSCharacter::PressedSlash()
{
SlashDelegate.Broadcast();
if (Role < ROLE_Authority)
{
ServerPressedSlash();
}
}
void ARSCharacter::ServerPressedSlash_Implementation()
{
PressedSlash();
}
bool ARSCharacter::ServerPressedSlash_Validate()
{
return true;
}
void ARSCharacter::SlashEvent_Implementation()
{
//Not used yet. I tried, but I think it didn`t work.
//This should be the C++ side of my (in BP called) event, right? Whatever.
}
This is the Blueprint relevant stuff of my character
And this is the Blueprint stuff of characters anim blueprint
But debugging shows that The Blueprint is not even fired on the client receiving side.
I found out that delegates have to be fired also on clients but
when < authority → let server do it. When should I trigger a client function?