September 2, 2021

Visual representation of Ai concepts in Unreal Engine 5

When developing even the simplest AI that implements sensors and real-world movement, simulating the algorithm inside a virtual 3d world can cut unnecessary costs and speed-up production phases. 

Now, why build a 3d engine when there already are incredibly powerful options out there available for free? 

Unreal Engine 5 is one of the most anticipated pieces of software in a while, it should bring big performance optimizations on the table in comparison to the previous version, and both Nanite and Lumen, the two main features, seems really promising in terms of what they will allow developers to do. 

Innovation aside, there is a big issue that should be addressed as soon as possible by Epic Games, the documentation for Unreal Engine 4 is imprecise and lacking, some of the functions and blueprint nodes are even descripted in the documentation only by their name! 

Unreal Engine 5 is currently available on early access, you can imagine that the documentation is simply… not there yet. In the official section of the Epic website https://docs.unrealengine.com/5.0/en-US/ we can see that as of today, there are only 12 pages of docs, describing the main changes. 

We would really like to see more from Epic, UE5 should be a big turn also in terms of documentation. 

Anyway, today we are going to give you a hint on how to adapt one of the first official UE4 tutorial to the new engine version, embedding an overly simplified C++ AI into a basic Pawn (an actor with the ability to move and in the environment and be controlled by a GameController). 

To start we need two things: 

  • Unreal Engine 5 Early Access 
  • Visual Studio 2019 

Configuring Visual Studio 2019 for unreal engine use is a bit complex, we suggest you to follow the official guide: https://docs.unrealengine.com/4.27/en-US/ProductionPipelines/DevelopmentSetup/VisualStudioSetup/ 

Let’s start with a blank project. 

Inside the editor, this is what should be visible: 

The option to create a new C++ class has been moved, now it’s under 

Tools -> New C++ Class 

We need to create and Pawn, let’s select that option. 

If UE5 prompts you to open the log because “an error occurred”, you will probably find inside it a request for a specific .NET version, in our case it was .NET 3.1, this requirement changes on the base of the UE and VS versions. We were able to meet the requirement installing the following package: https://dotnet.microsoft.com/download/dotnet/3.1 

After Visual Studio takes its time to load, this is the basic script: 

Adding 

public:
	// Sets default values for this pawn's properties 
	AMyPawn(); 
	UPROPERTY(VisibleAnywhere) 
	UStaticMeshComponent* VisualMesh; 

To the MyPawn.h (the header) under the “public” label, will add a StaticMesh componet to the actor, allowing us to edit his visual appareance. 

Setting the constructer AMyPawn:AMyPawn in MyPawn.cpp: 

AMyPawn::AMyPawn() 

{ 
	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it. 
	PrimaryActorTick.bCanEverTick = true; 

	VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh")); 
	VisualMesh->SetupAttachment(RootComponent); 
	static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube")); 
  
	if (CubeVisualAsset.Succeeded()) 
	{ 
		VisualMesh->SetStaticMesh(CubeVisualAsset.Object); 
		VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f)); 
	} 
} 

Will set a cube as the default static mesh of the class. 

Building the project, reloading the Unreal Engine project and placing the new actor in the world from the content manager leads to this situation: 

Our objective is to visually test a simple AI concept that we have previously designed, for this article lets imagine we have thought of testing a concept of aggregation. 

Something like this: 

-> Get all similar agents 

-> Get closest agent 

-> Get closer 

With UE5 we can test this concept. 

All we need is to edit the AMyPawn:BeginPlay() function. 

Getting all the similar agents: 

void AMyPawn::BeginPlay() 

{ 
	Super::BeginPlay(); 
	TArray<AMyPawn*> Actors; 
	FindAllActors(GetWorld(), Actors); 

	for (int i = 0; i < Actors.Num(); i++) { 
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TCHAR_TO_ANSI(*Actors[i]->GetName())); 
	} 

} 

Now if we build the project and start the game, we get the log of all the MyPawn instances present in the game, keep in mind that every instance repeats this operation so if we place 3 instances, we get 9 logs: 

Onto the “Get closest agent task”, modifying the current loop we can save the closest actor:

void AMyPawn::BeginPlay()
{
	Super::BeginPlay(); 
	TArray<AMyPawn*> Actors; 
	FindAllActors(GetWorld(), Actors); 
	float minDist = -1; 
	AMyPawn* closestActor; 

	for (int i = 0; i < Actors.Num(); i++) { 

		// GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TCHAR_TO_ANSI(*Actors[i]->GetName())); 

		float newDist = GetDistanceTo(Actors[i]);
		if ((newDist < minDist || minDist == -1) && Actors[i] != this){
			minDist = newDist;
			closestActor = Actors[i];
		}
	}
} 

This step was simple, this is basic c++. Now the interesting part, let’s move the current instance closer to the closest same-class actor.

if (closestActor != this) { 

		SetActorLocation(FVector((closestActor->GetActorLocation().X + GetActorLocation().X) / 2, 

			(closestActor->GetActorLocation().Y + GetActorLocation().Y) / 2, (closestActor->GetActorLocation().Z + GetActorLocation().Z) / 2)); 

		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TCHAR_TO_ANSI(*closestActor->GetName()));
	}

	char minDistChar[10];
	sprintf(minDistChar, "%f", minDist);
	GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, minDistChar); 

After a quick build, we are done! 

We can simulate the result with Alt + S, or by clicking Play -> Simulate. 

We suggest as an exercise that you try adding a MovementComponent and use AddInput to sobstitute this brutal “Snap” that SetActorLocation provide. 

Leonardo Bonadimani 

Filosoft 

Related Posts

AI Coding Agents Are Becoming Part of Everyday Development

AI coding agents are quickly becoming a normal part of software development. According to JetBrains’ 2026 Developer Ecosystem Survey, around 90% of professional developers used AI coding agents at work at least once a week between May and July 2026. The survey included more than 15,000 professional developers worldwide. Unlike traditional code assistants, AI agents […]

August 25, 2026

Artificial Intelligence and Health: From Diagnosis to Drug Discovery

https://www.sifweb.org/sif-magazine/articolo/intelligenza-artificiale-e-salute-dalla-diagnosi-alla-scoperta-di-nuovi-farmaci-2025-03-27 Artificial Intelligence (AI) is a technology that allows computers to process huge amounts of data, recognize patterns, and make highly accurate predictions. Through advanced models such as machine learning and deep learning, AI systems can learn from data and continuously improve their performance. In medicine, this innovation is transforming the way diseases are diagnosed, […]

May 22, 2026

AI as cognitive support for developers

A recent study by GitHub Next explores how artificial intelligence is reshaping modern software development workflows. The interviews revealed a clear distinction between repetitive, standardized tasks — such as boilerplate code generation, simple refactoring, or test generation — and high cognitive-load activities, including complex debugging, architectural design, contextual system analysis, and the evaluation of technical […]

May 13, 2026

Top ten stubborn gems in the upgrade from ruby 2 to 3

Updating gems is the fulcrum of the upgrade process of a legacy Rails codebase. Other than rails itself, a few gems stand out for their stubbornness in limiting the normal functionality of the application. Establishing the difficulty of maintaining the gem features it’s an heuristic process that involves looking up the gem on https://rubygems.org/ and […]

September 19, 2025

Bill Gates nel suo blog sull’AI

Developing AI and AGI has been the great dream of the computing industry. For decades, the question was when computers would be better than humans at something other than making calculations. Now, with the arrival of machine learning and large amounts of computing power, sophisticated AIs are a reality and they will get better very […]

March 22, 2023