How do I replicate a subobject?

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. :slight_smile:

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

I don’t want it as a spawnable object, I want UItemBase as items in for example an inventory. :slight_smile: 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). :slight_smile:

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.

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

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. :frowning:

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 ), 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? :slight_smile:

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

Quoted from gmpreussner’s answer on this UObject Replication question.

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.


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.

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.

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

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).

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.