How do you replicate an Actor pointer?

If you look in shootergame, there’s an FTakeHitInfo struct that basically deals with replicating damage information across the network.

But part of that struct is:


	/** Who hit us */
	UPROPERTY()
	TWeakObjectPtr<class AShooterCharacter> PawnInstigator;

	/** Who actually caused the damage */
	UPROPERTY()
	TWeakObjectPtr<class AActor> DamageCauser;


Which is strange. Because in the replication callback:



void AShooterCharacter::OnRep_LastTakeHitInfo()
{
	if (LastTakeHitInfo.bKilled)
	{
		OnDeath(LastTakeHitInfo.ActualDamage, LastTakeHitInfo.GetDamageEvent(), LastTakeHitInfo.PawnInstigator.Get(), LastTakeHitInfo.DamageCauser.Get());
	}
	else
	{
		PlayHit(LastTakeHitInfo.ActualDamage, LastTakeHitInfo.GetDamageEvent(), LastTakeHitInfo.PawnInstigator.Get(), LastTakeHitInfo.DamageCauser.Get());
	}
}

It uses the two pointers and passes them to the OnDeath and PlayHit methods as pointers.

So is there something special about TWeakObjectPtr that allows them to be replicated safely or something?

Outside of that, what is the “correct” way to identify an actor to be replicated? I have a need for a similar structure and I want my “threat” actor to be replicated in a similar manner.

Thanks in advance.

Phil.

Pointers for any Replicating Actor With Be Found Locally

Hi there!

Actually as long as your actor has bReplicates set to true, then you can pass pointers across server functions directly or as USTRUCTS and UE4 will find the local version of that same actor!

This is one of the most impressive things to me about UE4 Networking Code!

Please note if you put the Actor pointer (or any variable) in a USTRUCT it must be UPROPERTY() to be considered for replication.



USTRUCT()
struct FMyNetStruct
{
	GENERATED_USTRUCT_BODY()
 
	UPROPERTY()
	AYourReppedActorClass* ActorPtr** = nullptr;**
	
	UPROPERTY()
	FVector HitLocation = FVector(0);
	
	FMyNetStruct() {}
};


Also remember with USTRUCTS you must always initialize your variables yourself!

You MUST set the ptr to nullptr or you can easily crash your game.

:slight_smile:

Rama

@Rama Could you explain how to properly get a struct created and hooked up to a project? I’ve noticed that struct actor replication does not work with blueprints and so I decided to try and get it done with C++. I have no idea how to get this thing going… There are no options through the editor to create a C++ struct class. I tried to open the project in VS and create a header file and define according to your tutorial on the wiki, but I just can’t get the thing hooked up to the project.

in VS you can create a new YourNewStruct.h and if needed a YourNewStruct.cpp (check its location to be in source, by default VS wants to put it into Intermediate, at least here), where you can implement your struct FYourNewStruct. then you should include the YourNewStruct.h in e.g. an existing AActor .h that uses FYourNewStruct (before the *.generated.h), in the YourNewStruct.h file you should add #pragma once and #include “YourNewStruct.generated.h”. it work here fine.

Thanks for the help @sivan. I wasn’t able to do it only with your comments, but it helped me put on the right track.

This helped me as well: Creating Header-Only Files? - C++ Gameplay Programming - Unreal Engine Forums.