Check Overlap for moved Actor

I have to move an Actor (StaticMesh) from C++ using SetActorLocation.

How can I check an overlap with all other Actors on the scene to avoid unnecessary overlap.

Something like this,

MyActor->SetActorLocation(NewLocation);
if ( MyActor->"overlap with other actors" )
{
    // no need to move my actor due to overlap issue.
    MyActor->SetActorLocation(OldLocation);
    // or highlight it
    MyActor->SetRedColor();
}

The main thing is to find an overlap with all other actors.

I tried to find an answer…
Probably like this? I did’n check yet…
Please confirm if it’s correct.

// In constructor
OnActorBeginOverlap.AddDynamic(this, &AMyActor::OnOverlapBegin);
OnActorEndOverlap.AddDynamic(this, &AMyActor::OnOverlapEnd); 
bGenerateOverlapEvents = true;

// Functions
void AMyActor::OnOverlapBegin(class AActor* ThisActor, AActor* OtherActor)
{
    bOverlap = true;
}

void AMyActor::OnOverlapEnd(class AActor* ThisActor, AActor* OtherActor)
{
    bOverlap = false;
}

bool AMyActor::GetOverlapStatus()
{
    return bOverlap;
}

And after that

MyActor->SetActorLocation(NewLocation);
if (MyActor->GetOverlapStatus())
{
     // no need to move my actor due to overlap issue.
     MyActor->SetActorLocation(OldLocation);
     // or highlight it
     MyActor->SetRedColor();
}

If you want to check for blocking hits, use this and put whatever you want to do inside. Make sure your mesh actually has a collision defined and that collision presets are set to block and GetStaticMeshComponent()->SetNotifyRigidBodyCollision(true) is in your constructor or BeginPlay:

virtual void NotifyHit (class UPrimitiveComponent * MyComp, AActor * Other, class UPrimitiveComponent * OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult & Hit)
{
    UE_LOG(LogTemp, Warning, TEXT("Hit something!");
}

for overlaps, define this function and make sure bGenerateOverlapEvents is set to true in constructor or BeginPlay:
void OnOverlapsBegin(UPrimitiveComponent *Comp, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{
    UE_LOG(LogTemp, Warning, TEXT("Overlapped something!");
}
Also, you have to bind that overlap delegate (the OnOverlapsBegin function I wrote above). So you would have to do this in BeginPlay or PostInitializeComponents:
virtual void PostInitializeComponents()
{
    GetStaticMeshComponent()->OnComponentBeginOverlap.AddDynamic(this, &AMyStaticMeshActor::OnOverlapsBegin);
}

Finally, you can also use this function for overlaps which is much simpler if you're not comfortable with using delegates:
virtual void NotifyActorBeginOverlap(AActor *OtherActor)
{
    UE_LOG(LogTemp, Warning, TEXT("Overlapped something!");
}

Thank you very much. There is very detailed and useful answer.
I’ll check this solution.

My StaticMesh does not have collision. Before mounting it looks like phantom and moving in the client side only.
In fact I just need to move my StaticMesh to totally empty places (no any actors in that volume).
And (ideally) before changing location, I want to know is it empty or not.

This is an implementation of constraction from blocks.

Event when this actor bumps into a
blocking object, or blocks another
actor that bumps into it. This could
happen due to things like Character
movement, using Set Location with
‘sweep’ enabled, or physics
simulation.

Set Location with ‘sweep’?

Whether we sweep to the destination
location, triggering overlaps along
the way and stopping short of the
target if blocked by something. Only
the root component is swept and
checked for blocking collision, child
components move without sweeping. If
collision is off, this has no effect.

My collision is off. If it’s on, moved actor bumps all actors around.

Is it possible to use this only?

SetActorLocation(NewLocation, true)

In that case new location will not be assigned?
It means I can switch on the collision in advance without any risk to bump other actors.
I’ll check today. If it works, it’s the best solution for me.

SetActorLocation(NewLocation, true) works.
I even can use it…

However, my actor is stuck from any obstacle. It’s not good.

I don’t understand how NotifyHit function works.
It works in case of bump with main character only.
And doesn’t work with another client character.
I put this function to the class of moved block.

Could you give an example?

Thanks in advance.

There is a function called TeleportTo. Useful for just instantaneously moving to positions. Sweep means that the object will collide with things as it moves to the target location. If you want it to just teleport, make sure sweep is false

void AWarrior::NotifyHit(class UPrimitiveComponent* MyComp, AActor* Other, class UPrimitiveComponent *OtherComp, bool  bSelfMoved, FVector HitLocation, FVector HitNormal, FVector  NormalImpulse, const FHitResult &Hit)
{
	Super::NotifyHit(MyComp, Other, OtherComp, bSelfMoved, HitLocation, HitNormal, NormalImpulse, Hit); // Important
	AInventoryActor *InventoryItem = Cast(Other);
	if (InventoryItem)
	{
		TakeItem(InventoryItem);
	}
}

This is some random example code I found in one of my projects. So here, when the character hits something, it will check if its an inventory and if it is, will pick it up.
For character collision, you could just do an OnComponentBeginOverlap on the character’s capsule component. That will work fine but NotifyHit should work also. Try putting this SetActorEnableCollision(true) in character constructor or change collision presets to block. SimulatePhysics also. Just mess around with the collision settings and it will work, I don’t remember all the exact settings you have to change.

virtual bool TeleportTo ( const FVector & DestLocation, const FRotator & DestRotation, bool bIsATest, bool bNoCheck )

bNoCheck - is true if we should skip
checking for encroachment in the world
or other actors

I should use bNoCheck=false.

Probably this is exactly that I need. Thank you!
I’ve never used this function before.

I’ll test it today.