# Using an assert (check) if two of the same actors have overlapping UBoxComponents on BeginPlay() in C++

**URL:** <https://forums.unrealengine.com/t/using-an-assert-check-if-two-of-the-same-actors-have-overlapping-uboxcomponents-on-beginplay-in-c/1240228>\
**Category:** Blueprint\
**Tags:** unreal-engine, CPP, question\
**Created:** [July 26, 2023, 3:04am UTC](https://forums.unrealengine.com/t/using-an-assert-check-if-two-of-the-same-actors-have-overlapping-uboxcomponents-on-beginplay-in-c/1240228 "2023-07-26T03:04:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThePDub](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/thepdub/32/719651_2.png) [@ThePDub](https://forums.unrealengine.com/u/ThePDub)\
**Post date:** [July 26, 2023, 3:04am UTC](https://forums.unrealengine.com/t/using-an-assert-check-if-two-of-the-same-actors-have-overlapping-uboxcomponents-on-beginplay-in-c/1240228/1 "2023-07-26T03:04:53Z")

</div>

Sorry for the long title!

Due to a particular setup in my game, I would like to prevent two of the same class from overlapping each other. They are stationary, and contain a box collider.

To be more clear, I created a **Location** class (Here is Location.cpp):

```auto
#include "Location.h"
#include "Components/BoxComponent.h"
#include "Components/SceneComponent.h"

ALocation::ALocation()
{
	PrimaryActorTick.bCanEverTick = true;
	SceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("Actor Root"));
	RootComponent = SceneRoot;
	BoxCollider = CreateDefaultSubobject<UBoxComponent>(TEXT("Box Collider"));
	BoxCollider->SetupAttachment(RootComponent);
}

```

And I have some derived classes, lets call them **Ruins** and **BanditCamp** (Here is BanditCamp.h. Ruins.h/.cpp is exactly the same):

```auto
#pragma once

#include "CoreMinimal.h"
#include "Location.h"
#include "BanditCamp.generated.h"

UCLASS()
class HERA_API ABanditCamp : public ALocation
{
	GENERATED_BODY()
	
};

```

I want to check if any class derived from Location is overlapping with each other. This is my best attempt so far. Is this a decent approach at detecting what I want to avoid?

Again, we are back in **Location.cpp**

```auto
void ALocation::BeginPlay()
{
	Super::BeginPlay();
	for (const FOverlapInfo& overlaps : BoxCollider->GetOverlapInfos())
	{
		checkf(overlaps.OverlapInfo.GetComponent()->GetOwner()->GetClass() == ALocation::StaticClass(), TEXT("One Location is overlapping another box component. Most likely, this is another Location and will cause errors in the UI!"));
	}
}

```

---

<div class="post-metadata">

**Author:** ![ThePDub](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/thepdub/32/719651_2.png) [@ThePDub](https://forums.unrealengine.com/u/ThePDub)\
**Post date:** [July 26, 2023, 3:08am UTC](https://forums.unrealengine.com/t/using-an-assert-check-if-two-of-the-same-actors-have-overlapping-uboxcomponents-on-beginplay-in-c/1240228/2 "2023-07-26T03:08:13Z")

</div>

It appears to work, but I need to know if this is correctly going “up the chain” to the base class.

---

<div class="post-metadata">

**Author:** ![ThePDub](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/thepdub/32/719651_2.png) [@ThePDub](https://forums.unrealengine.com/u/ThePDub)\
**Post date:** [July 26, 2023, 3:24am UTC](https://forums.unrealengine.com/t/using-an-assert-check-if-two-of-the-same-actors-have-overlapping-uboxcomponents-on-beginplay-in-c/1240228/3 "2023-07-26T03:24:01Z")

</div>

I think I am even more confused. This appears to work as desired, but shouldn’t the checkf only execute if it is false?

---

<div class="post-metadata">

**Author:** ![UnrealEverything](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@UnrealEverything](https://forums.unrealengine.com/u/UnrealEverything)\
**Post date:** [July 26, 2023, 5:56am UTC](https://forums.unrealengine.com/t/using-an-assert-check-if-two-of-the-same-actors-have-overlapping-uboxcomponents-on-beginplay-in-c/1240228/4 "2023-07-26T05:56:36Z")

</div>

Yes `check` and `ensure` should only trigger if their condition is false. I use asserts a lot as well since they allow you to check for any unexpected mistakes in code or when you setup specific objects. Plus you get automatic breakpoints when they are violated and you’re running with a debugger attached.

Since you are only checking for ` == ALocation::StaticClass()` this means all overlaps must be other instances of `ALocation`. If your instance overlaps anything other than that the check should trigger. I think you have the condition reversed, you want the assert to fail (condition being false) whenever the overlap is another `ALocation`. So it should be `checkf(!overlaps.OverlapInfo.GetComponent()->GetOwner()->IsA(ALocation::StaticClass()), ...);`. This allows anything to overlap the `ALocation` unless it is another `ALocation` or derived from `ALocation`. You can also use `overlaps.OverlapInfo.GetComponent()->GetOwner()->GetClass()->IsChildOf()` instead of `IsA`.

Since in your case the message reads like a non-critical issue I’d also like to recommend the “map check for errors”. You could implement that in your `ALocation` class. You should be able to attach the actor instance(s) to the error message which allows you to directly link to the actor in the level.

> **[AActor::CheckForErrors](https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/GameFramework/AActor/CheckForErrors/)**
>
> Function that gets called from within Map\_Check to allow this actor to check itself for any potential errors and register them with map check dialog.

---

<div class="post-metadata">

**Author:** ![ThePDub](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/thepdub/32/719651_2.png) [@ThePDub](https://forums.unrealengine.com/u/ThePDub)\
**Post date:** [July 26, 2023, 9:49am UTC](https://forums.unrealengine.com/t/using-an-assert-check-if-two-of-the-same-actors-have-overlapping-uboxcomponents-on-beginplay-in-c/1240228/5 "2023-07-26T09:49:46Z")

</div>

I sincerely appreciate the explanation! Thank you so much 😃

I think something was off with live coding because after shutting down the engine and restarting it (I build from source) I discovered I had the condition reversed. Weird….anywho, thanks again!!

---

<div class="post-metadata">

**Author:** ![ThePDub](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/thepdub/32/719651_2.png) [@ThePDub](https://forums.unrealengine.com/u/ThePDub)\
**Post date:** [July 26, 2023, 10:18am UTC](https://forums.unrealengine.com/t/using-an-assert-check-if-two-of-the-same-actors-have-overlapping-uboxcomponents-on-beginplay-in-c/1240228/6 "2023-07-26T10:18:40Z")

</div>

Oh one another question if you don’t mind, please: if I wanted to create a Map Check for this, do you have an example? I am seeing other folks specify to only use it with a WITH\_EDITOR macro to not package it in a finished build, but those posts are several years old and I don’t know if that is necessary anymore.

---

<div class="post-metadata">

**Author:** ![UnrealEverything](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@UnrealEverything](https://forums.unrealengine.com/u/UnrealEverything)\
**Post date:** [July 26, 2023, 2:57pm UTC](https://forums.unrealengine.com/t/using-an-assert-check-if-two-of-the-same-actors-have-overlapping-uboxcomponents-on-beginplay-in-c/1240228/7 "2023-07-26T14:57:44Z")

</div>

Yes the declarations of `virtual void CheckForErrors();` in `AActor`, `UActorComponent` and `FLevelInstanceActorImpl` all are in `WITH_EDITOR` blocks. So if you implement it in a derived class you have to do the same if you do `void CheckForErrors() override;`.

This is an example from engine source:

```auto
void AStaticMeshActor::CheckForErrors()
{
	Super::CheckForErrors();

	FMessageLog MapCheck("MapCheck");

	if( !StaticMeshComponent )
	{
		FFormatNamedArguments Arguments;
		Arguments.Add(TEXT("ActorName"), FText::FromString(GetName()));
		MapCheck.Warning()
			->AddToken(FUObjectToken::Create(this))
			->AddToken(FTextToken::Create(FText::Format(LOCTEXT( "MapCheck_Message_StaticMeshComponent", "Static mesh actor {ActorName} has NULL StaticMeshComponent property - please delete" ), Arguments)))
			->AddToken(FMapErrorToken::Create(FMapErrors::StaticMeshComponent));
	}

	...
}

```

`AStaticMeshActor` even has a check very similar to yours. It checks whether two of the same static meshes are placed almost exactly on top of each other. In case you duplicate/paste one without moving it somewhere else.

---

<div class="post-metadata">

**Author:** ![ThePDub](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/thepdub/32/719651_2.png) [@ThePDub](https://forums.unrealengine.com/u/ThePDub)\
**Post date:** [July 27, 2023, 12:09am UTC](https://forums.unrealengine.com/t/using-an-assert-check-if-two-of-the-same-actors-have-overlapping-uboxcomponents-on-beginplay-in-c/1240228/8 "2023-07-27T00:09:34Z")

</div>

This all is way more clear to me now. Many thanks again!

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [August 26, 2023, 12:09am UTC](https://forums.unrealengine.com/t/using-an-assert-check-if-two-of-the-same-actors-have-overlapping-uboxcomponents-on-beginplay-in-c/1240228/9 "2023-08-26T00:09:43Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
