I have a struct:
USTRUCT(BlueprintType)
struct Fmy_struct
{
GENERATED_BODY()
protected:
float test_float = 100.f;
bool test_bool = true;
}
I have a replicated array in an actor component:
UPROPERTY(Replicated_Lifetime)
TArray<Fmy_struct> my_struct_array;
I have included the my_struct_array in “get lifetime replicated props” method of the actor component.
Now if I add a new struct to this struct array in the server, will the struct array replicate to clients, and new struct element (with the new values) reflect in the client?
Also, if I update any struct element’s value in the struct array, will that value change be replicated to the client?
Make ‘test_float’ and’ test_bool’ public (struct members are public by default in C++), and also decorate them with the UPROPERTY() macro. Unreal’s replication system will then pick up the data to replicate. The client’s copy of the array will reflect changes made by the server. You can also add a RepNotify function to handle the replicated changes as an event in case you want to trigger other behavior.
Your USTRUCT member properties should not be protected or private if you want to use the Unreal struct for their intended purpose (data). It’s fine to make private methods and values that encapsulate or hide functionality in C++, but without public UPROPERTY members, they will be almost useless in Blueprint.
1 Like