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"

replication of properties in a component

My goal is to replicate hp current value in the following setup:

a UActorComponent named UResourceComponent that contains, among other fields, the following:

 UPROPERTY(EditAnywhere,Replicated, BlueprintReadWrite, Category = "Details")
 float CurrentValue=100.f;

and by using a tutorial, the following code for replication:

in the header:

 virtual void GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const override;
 
 virtual bool IsSupportedForNetworking() const override
 {
     return true;
 }

and the cpp:

 void UResourceComponent::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
 {
     Super::GetLifetimeReplicatedProps(OutLifetimeProps);
 
     DOREPLIFETIME(UResourceComponent, CurrentValue);
 }

next up, is another UActorComponent named UVitalsComponent this component holds 3 UResourceComponent as resource bars. for now i am trying to replicate only the hp bar

the header:

 UPROPERTY(EditAnywhere,Replicated, BlueprintReadWrite, Category = "Initialization", meta = (EditCondition = bUseResourceHP))
     UResourceComponent* BarHP;    
 
 virtual bool ReplicateSubobjects(class UActorChannel *Channel, class FOutBunch *Bunch, FReplicationFlags *RepFlags) override;
 
 
 virtual void GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const override;

 virtual bool IsSupportedForNetworking() const override
 {
     return true;
 }

and again, following a tutorial, the cpp: setting BarHP->SetIsReplicated(true); in the constructor

 bool UVitalsComponent::ReplicateSubobjects(class UActorChannel *Channel, class FOutBunch *Bunch, FReplicationFlags *RepFlags)
 {
     bool WroteSomething = Super::ReplicateSubobjects(Channel, Bunch, RepFlags);
 
     if (BarHP != nullptr)
     {
         WroteSomething |= Channel->ReplicateSubobject(BarHP, *Bunch, *RepFlags);
     }
 
     return WroteSomething;
 }
 void UVitalsComponent::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
 {
     Super::GetLifetimeReplicatedProps(OutLifetimeProps);
 
     DOREPLIFETIME(UVitalsComponent, BarHP);
 }


lastly, a character class named Acpp_BaseGameCharacter. in blueprint i create a class derived from VitalsComponent and add it to the character. after failed tests i decided to search and save the component in the BeginPlay method.

Header:

 virtual bool ReplicateSubobjects(class UActorChannel *Channel, class FOutBunch *Bunch, FReplicationFlags *RepFlags) override;
 
 virtual void GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const override;
  
  UPROPERTY(VisibleAnywhere,Replicated)
 class UVitalsComponent* vitalsComponent;


and the cpp:

 void Acpp_BaseGameCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
 {
     Super::GetLifetimeReplicatedProps(OutLifetimeProps);
 
     DOREPLIFETIME(Acpp_BaseGameCharacter, vitalsComponent);
 }
 
 bool Acpp_BaseGameCharacter::ReplicateSubobjects(class UActorChannel *Channel, class FOutBunch *Bunch, FReplicationFlags *RepFlags)
 {
     bool WroteSomething = Super::ReplicateSubobjects(Channel, Bunch, RepFlags);
 
     if (vitalsComponent != nullptr)
     {
         WroteSomething |= Channel->ReplicateSubobject(vitalsComponent, *Bunch, *RepFlags);
     }
 
     return WroteSomething;
 }
 
 
 void Acpp_BaseGameCharacter::BeginPlay()
 {
     Super::BeginPlay();
     vitalsComponent = FindComponentByClass<UVitalsComponent>();
     vitalsComponent->SetIsReplicated(true);
 }


i expect that whenever i change the value of current hp on the server, the clients will get updated. it doesnt work and i feel really stupid and sure that i missed something.

Product Version: UE 4.18
Tags:
more ▼

asked Mar 06 '18 at 07:48 PM in C++ Programming

avatar image

Kidakaka
17 1 1 5

avatar image Atheist91 Mar 07 '18 at 12:00 PM

Hello.

I'd start with moving following line to the constructor of your Acpp_BaseGameCharacter. I'm guessing it might be way to late (world initialization wise) to set that in BeginPlay.

 vitalsComponent->SetIsReplicated(true);

Edit:

Also, make sure your component contructor contains this:

 bReplicates = true;

If that doesn't fix it, I'd remove your ReplicateSubobjects implementation. To be honest i never had to use that to properly replicate component properties. Maybe it's messing up default replication somehow.

avatar image Kidakaka Mar 07 '18 at 05:46 PM

thank you for the reply vitalsComponent is created in the blueprint, hence im not sure if it will be found at the construct time. moreover, in the blueprint i also set the replication flag to true. i added ReplicateSubobjects method only after i tried to make it work without it.

avatar image Atheist91 Mar 07 '18 at 05:49 PM

Ah, I see. Then yeah, you won't find it in contructor. :/

avatar image Atheist91 Mar 07 '18 at 06:22 PM

You could also try to do it in AActor::PostInitializeComponents. It's called somewhere during blueprint compilation/creation/opening. Add some log to make sure so you'll know when it executes.

avatar image Atheist91 Mar 07 '18 at 05:54 PM

I've edited my answer (added part about bReplicates property). You might be missing this.

avatar image Kidakaka Mar 07 '18 at 06:25 PM

thank you i will try the AActor::PostInitializeComponents. as for breplicates - yeah, same same :P

(comments are locked)
10|2000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
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