# How do I replicate a subobject?

**URL:** <https://forums.unrealengine.com/t/how-do-i-replicate-a-subobject/310050>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [March 8, 2015, 11:58pm UTC](https://forums.unrealengine.com/t/how-do-i-replicate-a-subobject/310050 "2015-03-08T23:58:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wrekk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wrekk](https://forums.unrealengine.com/u/Wrekk)\
**Post date:** [March 8, 2015, 11:58pm UTC](https://forums.unrealengine.com/t/how-do-i-replicate-a-subobject/310050/1 "2015-03-08T23:58:18Z")

</div>

Hello.

I’ve been trying to replicate a subobject, but with no luck. I have an item class that looks like this:

```
UCLASS(abstract)
class UItemBase : public UObject
{
	GENERATED_BODY()

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

	UPROPERTY(VisibleAnywhere)
	FString itemName;

	UPROPERTY()
	UTexture* itemIcon;

	UPROPERTY()
	int32 itemLevel;

	UPROPERTY()
	EItemRarity itemRarity;

	UPROPERTY()
	int32 itemID;

	UPROPERTY()
	FVector2D itemSize;

	UPROPERTY()
	EItemSubclass itemSubclass;
};

```

How do I make it replicable? When I use UPROPERTY(Replicated) it gives me some unresolved externals errors.

If there is another way to get a subobject from the server without the client ever having any information about that object, please tell me. 🙂

---

<div class="post-metadata">

**Author:** ![SaxonRah](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SaxonRah](https://forums.unrealengine.com/u/SaxonRah)\
**Post date:** [March 9, 2015, 12:45am UTC](https://forums.unrealengine.com/t/how-do-i-replicate-a-subobject/310050/2 "2015-03-09T00:45:49Z")

</div>

Do UObjects replicate ? I didn’t think they did, I thought AActor was the most basic class that had replication.

Try changing it to AActor

---

<div class="post-metadata">

**Author:** ![Wrekk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wrekk](https://forums.unrealengine.com/u/Wrekk)\
**Post date:** [March 9, 2015, 1:05am UTC](https://forums.unrealengine.com/t/how-do-i-replicate-a-subobject/310050/3 "2015-03-09T01:05:11Z")

</div>

I don’t want it as a spawnable object, I want UItemBase as items in for example an inventory. 🙂 I really want to know how I get the item from the server, without the client ever having any information about the item before asking for it (for cheating purposes). 🙂

---

<div class="post-metadata">

**Author:** ![mcucuzza](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mcucuzza](https://forums.unrealengine.com/u/mcucuzza)\
**Post date:** [March 9, 2015, 1:27am UTC](https://forums.unrealengine.com/t/how-do-i-replicate-a-subobject/310050/4 "2015-03-09T01:27:49Z")

</div>

Sounds like you need to implement GetLifetimeReplicatedProps

```
void UItemBase::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const {
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);

	DOREPLIFETIME(UItemBase, itemName);
}

```

You’ll need a DOREPLIFETIME for each property you mark as replicated.

There will likely be some other steps to get the object as a whole to replicate, since it’s not an AActor. You’ll need to override ReplicateSubobjects on whatever object owns a reference to the item.

This would look something like:

```
bool AMyItemOwningClass::ReplicateSubobjects(UActorChannel* channel, FOutBunch* bunch, FReplicationFlags* repFlags) {
	check(channel && bunch && repFlags);

	bool wroteSomething = Super::ReplicateSubobjects(channel, bunch, repFlags);

	wroteSomething |= channel->ReplicateSubobject(MyAwesomeItem, *bunch, *repFlags);
	return wroteSomething;
}

```

Where MyAwesomeItem is the UItemBase object that you want to replicate.

---

<div class="post-metadata">

**Author:** ![Wrekk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wrekk](https://forums.unrealengine.com/u/Wrekk)\
**Post date:** [March 9, 2015, 1:36am UTC](https://forums.unrealengine.com/t/how-do-i-replicate-a-subobject/310050/5 "2015-03-09T01:36:08Z")

</div>

Ah, thanks! I’ll try it out when I get home. 🙂

EDIT: Is there really no other way of doing this? It seems like way too much of a hassle to do this for every object. ☹

---

<div class="post-metadata">

**Author:** ![Wrekk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wrekk](https://forums.unrealengine.com/u/Wrekk)\
**Post date:** [March 9, 2015, 3:04pm UTC](https://forums.unrealengine.com/t/how-do-i-replicate-a-subobject/310050/6 "2015-03-09T15:04:27Z")

</div>

How would you get items from the server? I really can’t seem to figure it out. I thought I had it planned out, but I didn’t.

I got the item to replicate (like this: [Screenshot - c4d32107f5b931d7302ebed0050266eb - Gyazo](http://gyazo.com/c4d32107f5b931d7302ebed0050266eb) ), but I have no idea how to actually use the information I get, as the functions can’t return a value.

I would usually (without replication) just have a function called GetItem which returns a UItemBase. Could you give me any advice? 🙂

---

<div class="post-metadata">

**Author:** ![mcucuzza](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mcucuzza](https://forums.unrealengine.com/u/mcucuzza)\
**Post date:** [March 9, 2015, 6:26pm UTC](https://forums.unrealengine.com/t/how-do-i-replicate-a-subobject/310050/7 "2015-03-09T18:26:53Z")

</div>

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

---

<div class="post-metadata">

**Author:** ![Wrekk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wrekk](https://forums.unrealengine.com/u/Wrekk)\
**Post date:** [March 9, 2015, 7:21pm UTC](https://forums.unrealengine.com/t/how-do-i-replicate-a-subobject/310050/8 "2015-03-09T19:21:25Z")

</div>

I have looked at it, and it doesn’t really help much unfortunately. ☹

---

<div class="post-metadata">

**Author:** ![SaxonRah](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SaxonRah](https://forums.unrealengine.com/u/SaxonRah)\
**Post date:** [March 11, 2015, 12:30am UTC](https://forums.unrealengine.com/t/how-do-i-replicate-a-subobject/310050/9 "2015-03-11T00:30:02Z")

</div>

Quoted from gmpreussner’s answer on [this UObject Replication question](https://answers.unrealengine.com/questions/51358/uobject-replication.html).

> The first class in the UObject class hierarchy that implements replication is AActor. All game relevant objects that you wish to be shared between server and clients have to be Actors.

> UObject is the most basic class in the Engine’s object system. You can think of it as ‘object’ in C# and Java. A game can have tens of thousands of UObject instances at any given time, many of which are not even relevant to the game. Imagine what would happen if they were all replicated! Hence it is the purpose of AActor to implement game relevant classes that can be replicated.

> You said you like UObjects because they are easier to create. Are you perhaps using them simply to transfer data? In that case you may want to consider UStruct properties on your Actors.

> If you are using the UObjects to replicate or share behavior, then you may want to consider function replication on your Actor instead.

> If you are looking for a way to share data or behavior between different actor classes, or if you want to compartmentalize it to better structure your code, you may also consider [Replicated Actor Components.](https://docs.unrealengine.com/latest/INT/Gameplay/Networking/Replication/Components/index.html)

* * *

UE4’s replication system by default will not allow cheating if used properly. Since the things you do not want the client to know about, are ran only on the server.

To better understand ue4’s replication i would recommend following the [blueprint replication tutorial on youtube.](https://www.youtube.com/playlist?list=PLZlv_N0_O1gYwhBTjNLSFPRiBpwe5sTwc)

---

<div class="post-metadata">

**Author:** ![SaxonRah](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SaxonRah](https://forums.unrealengine.com/u/SaxonRah)\
**Post date:** [March 11, 2015, 1:48am UTC](https://forums.unrealengine.com/t/how-do-i-replicate-a-subobject/310050/10 "2015-03-11T01:48:24Z")

</div>

My inventory system’s base item is an AActor and I tried to use cheat engine and like software to attempt to cheat with duping and modify values and stuff and it was fine i could not get anything to work client side.

---

<div class="post-metadata">

**Author:** ![Wrekk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Wrekk](https://forums.unrealengine.com/u/Wrekk)\
**Post date:** [March 11, 2015, 3:41pm UTC](https://forums.unrealengine.com/t/how-do-i-replicate-a-subobject/310050/11 "2015-03-11T15:41:27Z")

</div>

Hm, I guess I could try to make it an actor instead. 🙂

EDIT: I tried that, but then when I play the game, it triggers a breakpoint at `void CastLogError(const TCHAR* FromType, const TCHAR* ToType) { UE_LOG(LogCasts, Fatal, TEXT("Cast of %s to %s failed"), FromType, ToType); }`. I used Visual Assist X to rename, so it’s not because I forgot something (I made my UItemBase class inherit from AActor, and then renamed it to AItemBase).

---

<div class="post-metadata">

**Author:** ![SaxonRah](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SaxonRah](https://forums.unrealengine.com/u/SaxonRah)\
**Post date:** [March 14, 2015, 11:39pm UTC](https://forums.unrealengine.com/t/how-do-i-replicate-a-subobject/310050/12 "2015-03-14T23:39:03Z")

</div>

Do you still have the UObject overriden function ? I’m not sure that works in AActor. Try removing that. If it doesn’t work. What i would do is this,  
Adding a new c++ aactor class from the UE4 Editor Menu, and retype only your needed implementations of the baseitem back into the new aactor.
