How to replicate the variable declared in under the struct?

I have this in the USTRUCT under AItemBase:

AItemBase.h

USTRUCT(BlueprintType)
struct FMyTestStruct
{
	GENERATED_USTRUCT_BODY()

UPROPERTY(Replicated, EditDefaultsOnly, BlueprintReadOnly, Category = Fashion Items Related)
class USkeletalMesh* SkeletalMesh;

};

I am trying to replicate it but It says AItemBase has no member SkeletalMesh

AItemBase.cpp

void AItemBase::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);
	DOREPLIFETIME(AItemBase, SkeletalMesh);
}

How Can I Replicate the variable SkeletalMesh ?

2 Likes

Hello,

You’d need to replicate the struct variable, not the member of the struct. Replicated USTRUCT properties replicate their members if they’re a UPROPERTY

Here is a nice resource on structs if you’re not familiar with it: wiki.unrealengine.com

3 Likes

Thank You for help, so I need to replicate FST_ItemFashion ?

//Fashion Item Struct
USTRUCT(BlueprintType)
struct FST_ItemFashion : public FTableRowBase

Not the struct directly, but a class member variable of the struct type.

Example:

USTRUCT(BlueprintType)
struct FST_ItemFashion : public FTableRowBase
{
     UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = Fashion Items Related)
    class USkeletalMesh* SkeletalMesh;

}

...(class code)...

protected:

UPROPERTY(Replicated)
FST_ItemFashion ItemFashion;
4 Likes

and then how to access the rows? currently I am accessing them like:

(const FST_ItemFashion* OutRow{ DT_ItemFashion->FindRow<FST_ItemFashion>((ID), "") })

I will need to do instead?

(const ItemFashion* OutRow{ DT_ItemFashion->FindRow<FST_ItemFashion>((ID), "") })

I think you’re confusion Structs and Data Tables.

Structs don’t have rows to access like data tables. A struct is just a condensed data structure and data tables have rows of data of the same data structure.

If you want to access a member of a struct you would just do this:

ItemFashion.SkeletalMesh = nullptr;

You can use your custom struct for a data table though. I’m not sure what your use case is here so I’m going to point you to this resource which I’ve found helpful for using data tables in C++: Unreal Engine C++ Fundamentals – DataTables – Jolly Monster Studio

3 Likes

I have large data tables derived from structs, I create struct in C++ and created data table from it in under editor BP I know very well to use datatables and structs.

I have problem with replicating variables located under the struct.
SkeletalMesh variable used for attaching the jacket item to the sub skeletal mesh of the main mesh.

when I start the game in editor with 2 players , they don’t see each otehr clothing items, in my case they don’t see each others jacket attached to the sub skeletal mesh.

You can see in this post how I am working with them.

forums.unrealengine.com

Sorry - no meaning to offend.

I think the issue may be with how your players are equipping the jacket not necessarily the struct replication. Is the skeletal mesh component of the character replicated? If you equip the skeletal mesh on the server only, the mesh will replicate to the client.

Sir, all right, I am just sharing information with you and don’t even think about to be offended :smiley:

Yes the main bodies can see each other and all the movement/actions are replicated. Only the attached items are not showing between them

Great you never know with internet people :slight_smile:

It’s hard to tell without seeing how the items are attached but I think that is where your issue is.

Try adding some checks to make sure only the Server is doing the attaching – something like if(HasAuthority()=false){return;)

This will ensure it’s only the sever characters setting the mesh.

Also, logging what is getting attached would be helpful for this as well.

EX:

if(SkeletalMesh)
{
    UE_LOG(LogTemp,Warning,TEXT("SM name = %s",*SkeletalMesh->GetName()))
}
else
    UE_LOG(LogTemp,Warning,TEXT("SM is null))
}
2 Likes

yes, solved… it was actually the client was setting the patterns for skeletal mesh, now at least I found the bug, thank You Sir for help… I will try to fix it

I will try to do something like this.

//.H

USTRUCT(BlueprintType)
struct FMyTestStruct
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY()
	int32 TestValueClient;

	UPROPERTY()
	int32 TestValueServer;

	FMyTestStruct()
	{}
};

void TestStructReplication();

UFUNCTION(Server, Reliable, WithValidation)
void ServerUpdateStruct();
virtual void ServerUpdateStruct_Implementation();
virtual bool ServerUpdateStruct_Validate();

UPROPERTY(Replicated)
FMyTestStruct TestStruct;

//.CPP

void TestStructReplication()
{
	TestStruct.TestValueClient = 1;
	ServerUpdateStruct();
}

void ServerUpdateStruct_Implementation()
{
	TestStruct.TestValueServer = 2;
}

bool ServerUpdateStruct_Validate()
{
	return true;
}

Results

Expected if Server replicates changes only

ClientValue = 1

ServerValue = 2

Expected If Server replicates entire struct

ClientValue = 0

ServerValue = 2

Actual Result

ClientValue = 1

ServerValue = 2

The server only replicates changes that have been executed on the server and does not replicate changes the client has made locally.