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

Product Version: Not Selected
Tags:
more ▼

asked Mar 08 '15 at 11:58 PM in C++ Programming

avatar image

Wrekk
82 24 24 31

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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.

more ▼

answered Mar 09 '15 at 01:27 AM

avatar image

mcucuzza
28 1 4 6

avatar image Wrekk Mar 09 '15 at 01:36 AM

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

avatar image Wrekk Mar 09 '15 at 03:04 PM

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

avatar image mcucuzza Mar 09 '15 at 06:26 PM
avatar image Wrekk Mar 09 '15 at 07:21 PM

I have looked at it, and it doesn't really help much unfortunately. :(

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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

more ▼

answered Mar 09 '15 at 12:45 AM

avatar image

SaxonRah
328 11 11 28

avatar image Wrekk Mar 09 '15 at 01:05 AM

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

avatar image SaxonRah Mar 11 '15 at 12:30 AM

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.

avatar image SaxonRah Mar 11 '15 at 01:48 AM

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.

avatar image Wrekk Mar 11 '15 at 03:41 PM

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

avatar image SaxonRah Mar 14 '15 at 11:39 PM

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.

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