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"

How do you replicate a TArray of UObjects

I created an Actor Component with an array of objects derived from UObject. I've setup the UObject to be replicatable on it's own when within the ActorComponent but when I put it into a TArray the game crashes.

My Setup is as follows:

 class UMyActorComponent
 {
     UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Replicated)
     TArray<UMyObject> myObjects
 }
 
 void UMyActorComponent::GetLifetimeReplicatedProps(...)
 {
     DOREPLIFETIME(UMyActorComponent, myObjects)
 }
 
 bool UMyActorComponent::ReplicateSubobjects(ActorChannel, Bunch, RepFlags)
 {
     bool wroteSomething = Super::(ActorChannel, Bunch, RepFlags);
         
     for(int32 i = 0; i < myObjects.Num(); ++i)
     {
         if(myObjects[i]->IsReplicationNeeded()) // custom flag
         {
             wroteSomething |= ActorChannel->ReplicateSubobject(myObjects[i], *Bunch, *RepFlags)
         }
     }
 }

while writing this I just had a thought... I had been looking at the documentation on replicating Dynamic Arrays... Do I absolutely positively HAVE to have each element in the array if I am not using the ReplicationKey function...??... that may be it...

Product Version: Not Selected
Tags:
more ▼

asked Feb 18 '15 at 06:37 PM in C++ Programming

avatar image

ArcainOne
449 32 25 55

avatar image ArcainOne Feb 19 '15 at 09:28 PM

Am I really the only person whose ever tried this????

avatar image BPANDREW Feb 19 '15 at 09:36 PM

I'm having a hard time thinking of why you would need to do this - you can just add actors with replication set to true to your scene, then you can access them using GetAllActors - aka your map/world IS your array of actors

Can you give a higher level example of what you are trying to accomplish?

avatar image ArcainOne Feb 23 '15 at 11:07 PM

The major reason is to replicate generic data to the server about the player. The server is master of the data but the player can see his or her own stats. Specifically there is a rather complicated trait and status effect system of objects that don't need to be actors, but do need to be replicated. I found that UActorComponent allows replication from the parent actor so I started there.

avatar image BPANDREW Feb 23 '15 at 11:33 PM

I replicate tarrays, but typically I use enums or structs inside of them - I do sometimes put references to objects in those structs/enums/arrays but I never have to do the "ReplicateSubobjects" part and it works fine - i.e if those objects in your scene are set to replciate, it takes care of it on its own

avatar image ArcainOne Feb 24 '15 at 07:56 PM

I think that is because those are "natively" supported because they are simple types. I know UStructs replicate without issue... In retrospect because struct and classes are basically the same thing now in C++ i probably could have just used them... but stubbornness.

I have since solved it though, and will be posting the answer (with code) soon as it was a solid week of banging head against wall, scraping everything and starting small while searching any information I could find about replication, subobjects, and TArrays. I swear I read the same documentation 10-30 times looking for subtle clues and hints...

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

2 answers: sort voted first

For the answer to this question I wrote out an explaination of Replicating a TArray of Subobjects in the forums

https://forums.unrealengine.com/showthread.php?59275-Replicating-TArrays-crashes-game&p=231425#post231425

more ▼

answered Feb 27 '15 at 11:06 PM

avatar image

ArcainOne
449 32 25 55

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

Can you elaborate on how the game is crashing? Have you stepped through with the debugger? It's probably a null/invalid pointer in myObjects[i] that's crashing when you access it. Either that or the IsReplicationNeeded() function is crashing (can't see if this is possible without seeing the code).

more ▼

answered Feb 19 '15 at 09:35 PM

avatar image

Lojo
90 2 2 9

avatar image ArcainOne Feb 23 '15 at 11:20 PM

(I wrote a comment earlier... but it's not showing up so... pardon me if this is a duplicate response.)

Unfortunately all I can say is that the error occurred at the moment of replication. I setup an Interface on the client and server to display bit of replicated information. The moment I called a command to change the client's data the game crashed. I sort of gave up and moved on to something else... then came back in all my stubbornness and actually got it working.

However what was causing the crash I am not sure about as I have completely destroyed the previous code that was leading to the crash. But I will be making a post on how to replicate a TArray of UObjects to help others who may need this later.

My guess is that the error occurred because I was inadvertently creating the array on BOTH the client and the server thus leading to similar but different objects. That or maybe a TArray must be duplicated in it's entity... but I don't think that was the problem as my new experiences with replicating UObjects in a TArray don't seem to care much about the Array itself, but of the object... This is a hypothesis I had intended to actually test. My new objects are constructed only on the server and replicated down to the client.

(comments are locked)
10|2000 characters needed characters left
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