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"

Can an arbitrary struct be passed via an RPC?

My project has custom networking requirements, with a need to send data directly between server and clients. I've been able to send data to a client RPC using an FString as a parameter, but I cannot send a struct - the project will not compile if I try. Is sending a struct possible? And if so, are there any examples for how this is done?

If it helps, here is the code I'm using that fails to compile:

     // the struct
     USTRUCT()
     struct FClientRpcData
     {
         GENERATED_USTRUCT_BODY()
     
         public:
             uint8 _Data[256];
             uint32 _Hash;
     };
 
    // inside my class declaration (a custom PlayerController)
    UFUNCTION( Client, Reliable )
    void ClientRpcFunction( const FClientRpcData& Data );

     // inside the class implementation
    void AMyPlayerController::ClientRpcFunction_Implementation( const FClientRpcData& Data )
    {
     ....
    }

I get this error when I try to compile:

 error C2511: 'void AMyPlayerController::ClientRpcFunction_Implementation(const FClientRpcData &)' : overloaded member function not found in 'AMyPlayerController'






Product Version: Not Selected
Tags:
more ▼

asked Nov 26 '14 at 06:48 PM in C++ Programming

avatar image

mdomowicz
13 2 3 5

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

2 answers: sort voted first

Hi,

Did you try to send the struct itself ?

 void ClientRpcFunction( FClientRpcData Data );
more ▼

answered Dec 07 '14 at 09:37 PM

avatar image

uced
850 75 89 360

avatar image mdomowicz Dec 08 '14 at 09:58 PM

Hmm, that works! I thought I had tried that actually. I don't know why it wouldn't have worked the first time. Maybe I had something else wrong. Anyway, thanks!

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

About the first question, you should mark your member variables _Data and _Hash with a UPROPERTY() definition. This would allow UHT to generate serialization, retrospection and replication code for this type.

According to the compilation problem and this: https://docs.unrealengine.com/latest/INT/Gameplay/Networking/Replication/RPCs/index.html your code looks good.

Marcin

more ▼

answered Nov 27 '14 at 10:06 AM

avatar image

mpo
573 23 15 46

avatar image mdomowicz Dec 01 '14 at 10:38 PM

Good call on the UPROPERTY() definitions. I've added them. I'm still getting the same compile error though. Perhaps sending structs of data is just not possible in UE4?

avatar image WCode Dec 02 '14 at 09:06 AM

There are two things you can try if this is not working (can`t see any errors).

1 Did you forget to add Replicated to the Struct member in your controller?

2 Add validation to the method. like this.

  // inside my class declaration (a custom PlayerController)
 UFUNCTION( Client, Reliable, WithValidation )
 void ClientRpcFunction( const FClientRpcData& Data );

Just to mention it you then also have to add a second method to you cpp file.

 bool AMyPlayerController::ClientRpcFunction_Validate(const FClientRpcData &)

Also here is a example on a replicated struct.

 /** Contains inventory array and integer "condition". */
 USTRUCT(Blueprintable)
 struct FInventoryContainerStruct
 {
     GENERATED_USTRUCT_BODY()
 
 public:
     // Array of items containing the players inventory/ loot.
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player Class, FInventoryContainerStruct")
         TArray<FItemInventoryStruct> InventoryArray;
 
     // Changed everytime array is edited, to make sure array get replicated to client.
     UPROPERTY()
         int32 Condition;
 
     /**
      * Update the Inventory Container to guarantee replication to owner. 
      * @param: Pass in a posetive or negative integer. */
     void UpdateArrayCondition(int32 ToAdd)
     {
         Condition += ToAdd;
     }
 };

 // INventory for this player managed on server and replicated to client.
 UPROPERTY(ReplicatedUsing = OnRep_InventoryArray, EditAnywhere, BlueprintReadWrite, Category = "Player Class, Inventory")
     FInventoryContainerStruct InventoryContainer;

 // Adds a item to the inventory if space.
 UFUNCTION(BlueprintCallable, Category = "Player Class, Inventory")
     void AddItemToInventory(const FItemInventoryStruct ItemToAdd);

 UFUNCTION(Server, Reliable, WithValidation)
     void SERVER_AddItemToInventory(const FItemInventoryStruct ItemToAdd);

Hope it helps

WCode

avatar image mdomowicz Dec 08 '14 at 10:32 PM

Thanks for the response. I only just got a chance to look at this now, and luckily for me, uced's response below was exactly what I needed. I really appreciate your help though. Thanks again!

(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