Hello ,
I try to update a int variable in the player class, Im using a Dedicated Server.
When a client increment the variable and replicate to the server, the server update the variable for all clients.
But dont work , I dont know when I have to update the variable to the clients and how exactly.
Some help please ?
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Character.h"
#include "PlayerCharacter.generated.h"
UCLASS()
class BASEPROJECT_API APlayerCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
APlayerCharacter();
virtual void Tick(float DeltaTime) override;
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UFUNCTION()
void PlayMedia();
UFUNCTION()
void NextMedia();
UFUNCTION(Reliable, Server, WithValidation)
void ServerPlayMedia();
virtual void ServerPlayMedia_Implementation();
virtual bool ServerPlayMedia_Validate();
UFUNCTION()
virtual void OnRep_PlayMedia();
UFUNCTION(Reliable, Server, WithValidation)
void ServerNextMedia();
virtual void ServerNextMedia_Implementation();
virtual bool ServerNextMedia_Validate();
UFUNCTION()
virtual void OnRep_NextMedia();
void ChangeIndex(int idx);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
UPROPERTY(EditAnywhere, Replicated)
UMediaPlayer* MediaPlayer;
UPROPERTY(EditAnywhere, Replicated)
TArray<FString> RepPathList;
UPROPERTY(EditAnywhere, ReplicatedUsing=OnRep_PlayMedia)
int RepIndex;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "BaseProject.h"
#include "UnrealNetwork.h"
#include "PlayerCharacter.h"
// Sets default values
APlayerCharacter::APlayerCharacter()
{
static ConstructorHelpers::FObjectFinder<UMediaPlayer> mediaP(TEXT("/Game/Movies/MediaPlayer"));
if (mediaP.Succeeded())
MediaPlayer = mediaP.Object;
}
// Called when the game starts or when spawned
void APlayerCharacter::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void APlayerCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("RepIndex Tick : %d"), RepIndex));
}
// Called to bind functionality to input
void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
InputComponent->BindAction("PlayMedia", IE_Pressed, this, &APlayerCharacter::PlayMedia);
InputComponent->BindAction("NextMedia", IE_Pressed, this, &APlayerCharacter::NextMedia);
}
void APlayerCharacter::PlayMedia()
{
RepIndex = 0;
ChangeIndex(RepIndex);
if (Role < ROLE_Authority)
{
ServerPlayMedia();
OnRep_PlayMedia();
}
}
void APlayerCharacter::OnRep_PlayMedia()
{
ChangeIndex(RepIndex);
}
void APlayerCharacter::ServerPlayMedia_Implementation()
{
PlayMedia();
}
bool APlayerCharacter::ServerPlayMedia_Validate()
{
return true;
}
void APlayerCharacter::NextMedia()
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Purple, TEXT("On Client"));
RepIndex++;
ChangeIndex(RepIndex);
if (Role < ROLE_Authority)
{
ServerNextMedia();
OnRep_NextMedia();
}
}
void APlayerCharacter::OnRep_NextMedia()
{
ChangeIndex(RepIndex);
}
void APlayerCharacter::ServerNextMedia_Implementation()
{
NextMedia();
}
bool APlayerCharacter::ServerNextMedia_Validate()
{
return true;
}
void APlayerCharacter::ChangeIndex(int idx)
{
RepIndex = idx;
if (RepIndex >= RepPathList.Num())
RepIndex = 0;
if (RepPathList.IsValidIndex(RepIndex))
{
if (MediaPlayer->OpenFile(RepPathList[RepIndex]))
{
MediaPlayer->Play();
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::White, FString::Printf(TEXT(" RepIndex : %d"), RepIndex));
}
}
}
void APlayerCharacter::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(APlayerCharacter, MediaPlayer);
DOREPLIFETIME(APlayerCharacter, RepIndex);
}