Download

Component replication, need some help

Hey guys. Trying to get component replication to work, but the docs seem slightly out of date. They’re also quite vague. So, hopefully someone can chime in with some clarification.

Upon reading the docs here, my implementation is this:

My PlayerState has a collection of UObject derived data objects with (currently) a couple of FNames in them. They also have their ObjId and RepKey members for replication.

In my PlayerState, I implement ReplicateSubobjects like so:


bool ARobotPlayerState::ReplicateSubobjects(UActorChannel *Channel, FOutBunch *Bunch, FReplicationFlags *RepFlags)
{
	bool WroteSomething = false;

	if (Channel->KeyNeedsToReplicate(0, WeaponMountsRepKey))	// Does the array need to replicate?
	{
		TArray<FName> keys;

		WeaponMounts.GetKeys(keys); // There needs to be a better way to iterate over a maps keys.

		for (auto wmIt = keys.CreateIterator(); wmIt; wmIt++)
		{
			FName key = *wmIt;

			UWeaponState *weaponState = WeaponMounts[key];
			if (Channel->KeyNeedsToReplicate(weaponState->RepObjId, weaponState->RepKey))
			{
				WroteSomething |= Channel->ReplicateSubobject(weaponState, *Bunch, *RepFlags);
			}
		}
	}

	return WroteSomething;
}

This code compiles. But I get an access violation somewhere in ReplicateSubobject (don’t know where exactly yet, I am using the binary build, not the source build. Next thing to do will be to look at that.)

Now the things that aren’t clear/my questions:

1 - In the subobject replication, what gets replicated? UPROPERTYs? I’ve tried it with replication marked UPROPERTYs and without. Same crash.

2 - The docs say “Classes wishing to replicate non-ActorComponent subobjects should implement the above three methods.”. The three methods are InstantiateReplicatorForSubObject, ReplicateSubobjects and OnSubobjectCreatedFromReplication.

They don’t mention HOW to implement anything but ReplicateSubobjects (and that example implementation is only found in ActorChannel.h in a comment, and not in that documentation.) OnSubobjectCreatedFromReplication seems straightforward, but it’s implemented already (in AActor). So what do you do in the override? InstantiateReplicatorForSubObject might be straightforward, but it’s more likely it doesn’t exist any more since I can’t find any mention of it in the source. So what’s the actual thing now?

Please devs, clarification needed. A quick sketch of how to do this properly would be very helpful.

I have nothing to add beyond I also would like an example and/or docs on how to replicate and use RPCs on basic UObjects. (bump)

This related question also needs an answer: ReplicateSubobjects useage - UE4 AnswerHub

Edit: I guess your answerhub post has more relevant info: Getting non-Actor subobject replication to work - UE4 AnswerHub. I still would prefer a more concrete example.