x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Update Clients variable

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);
 }

Product Version: UE 4.15
Tags:
more ▼

asked Jul 09 '17 at 09:47 PM in C++ Programming

avatar image

KrazyApple
23 1 6 8

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Hmm actually you increment index on client, after that you change on server. In this form replication is useless, i dont know why you want server validated int for media playing, but i have some idea.

So first, if you are sure you want change that int on server, do not change on client. If NextMedia called check you have authority or not, if not only just call ServerNextMedia (and onrep actually dont need to be called)

After this server will replicate the new value and will run your onrep function.

Second: i do not see player is marked replicated? Maybe in default characterd are marked, but do SetIsReplicated(true) for sure.

Third: if you try to achieve some kind of syncronized media playing, where more player can switch between media and all players need to be see that selected media, well thrn you doing wrong. Because int will be replicated to every player, but code will not run because in multiplayer local player have rights, other players are only simulated actors.. so if one of that simulated actor int will be replicated, he CANT change local actors (player) media!!! (Recommendation: read Cedric's network compedium ;) http://cedric-neukirchen.net/Downloads/Compendium/UE4_Network_Compendium_by_Cedric_eXi_Neukirchen.pdf)

So if you try to achieve this kind of achieve this kind of mechanism, you need to work with NetMultiCast RPC Which can be started from server and will run in every client..

In short: player change media -> server rpc to change media -> server sends netmulticast rpc to all clients to play switch new media

more ▼

answered Jul 10 '17 at 07:09 AM

avatar image

AmphDev
1.5k 15 9 33

avatar image KrazyApple Jul 10 '17 at 09:02 AM

Thanks you for the help

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question