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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Why is replication not working for float/bool?

Hello!

I am very new to UE4 and am trying to set up my first basic Networking game. I am doing something very simple, i am trying to replicate a bool or a float from my AActor. I have the following code:

MazeCreator.h

 #pragma once
 
 #include "CoreMinimal.h"
 #include "GameFramework/Actor.h"
 ...
 #include "MazeCreator.generated.h"
 
 // if this fails change to development editor
 UCLASS()
 class MAZE3PS_API AMazeCreator : public AActor
 {
     GENERATED_BODY()
     
 public:
 
     // Sets default values for this actor's properties
   AMazeCreator(const FObjectInitializer& ObjectInitializer);
   ~AMazeCreator();
 
   virtual void GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const override;
 
   bool IsSupportedForNetworking() const override;
 
 protected:
     // Called when the game starts or when spawned
     virtual void BeginPlay() override;
 
 private:
   
   UPROPERTY(ReplicatedUsing=OnRep_hasMaze)
     float hasMaze = 0;
 
   void ServerSetHasMaze();
   
   UFUNCTION()
   void OnRep_hasMaze();
 };


MazeCreator.cpp

 #include "MazeCreator.h"
 ...
 #include "EngineUtils.h"
 #include "UnrealNetwork.h"
 
 // Sets default values
 AMazeCreator::AMazeCreator(const FObjectInitializer& ObjectInitializer)
   : Super(ObjectInitializer)
 {
   bReplicates = true;
   bAlwaysRelevant = true;
 }
 
 // Called when the game starts or when spawned
 void AMazeCreator::BeginPlay() {
   Super::BeginPlay();
 
   if (HasAuthority()) {
     this->ServerSetHasMaze();
   }
 }
 
 void AMazeCreator::ServerSetHasMaze() {
   if (HasAuthority()) {
     this->hasMaze = 1;
     OnRep_hasMaze();
   }
 }
 
 // gets called when hasMaze gets replicated
 void AMazeCreator::OnRep_hasMaze() {
   if (HasAuthority()) {
     UE_LOG(LogTemp, Error, TEXT("hasMaze turned on server"));
   } else {
     UE_LOG(LogTemp, Error, TEXT("hasMaze turned on client"));
   }
 }
 
 void AMazeCreator::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const {
   Super::GetLifetimeReplicatedProps(OutLifetimeProps);
 
   DOREPLIFETIME(AMazeCreator, hasMaze)
 
 }
 
 bool AMazeCreator::IsSupportedForNetworking() const {
   return true;
 }


The problem is that I am actually using the bool/float to render a maze on the screen. The server renders the maze but the client doesn't. If i look at the Editor Output logs, when running in Single Process mode, I can see that the server logs "hasMaze turned on server" but the client never prints it. I also know that the client logs are being printed there as my client player falls past the rendered maze on the server and because of that starts printing warnings about ignoring the server correction to the player movement.

So my question is, what am i doing wrong here and why is hasMaze not getting replicated in my MazeCreator class?

Let me know if you need anything else from me,

Thanks for the Help!

Tyler

Product Version: UE 4.17
Tags:
more ▼

asked Feb 23 '18 at 02:31 AM in C++ Programming

avatar image

tyler.gauch
46 2 6 5

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

1 answer: sort voted first

This was fixed by adding:

 if (HasAuthority()) {
     this->SetReplicates(true);
 }
 
 instead of 
 
 this->bReplicates = true;

in the constructor and making the variable a Public bool/float instead of private

more ▼

answered Feb 23 '18 at 03:24 AM

avatar image

tyler.gauch
46 2 6 5

(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