\

[Original tutorial is based on UE 4.18, MINE is based on UE 4.25]

English original Address

In the following section, we create a new Actor subclass called ConsoleLog (we don’t need to do anything in the header file).

ConsoleLog.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ConsoleLog.generated.h"

UCLASS(a)class UNREALCPP_API AConsoleLog : public AActor
{
	GENERATED_BODY(a)public:	
	// Sets default values for this actor's properties
	AConsoleLog(a);protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay(a) override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	
};
Copy the code

We will then print the log message in the.cpp file. For this example, we will print the message in the BeginPlay method. So, when the game starts, the message will be printed.

Here are three ways to print a message.

  • Print to console
  • Print to screen
  • The printed data is in vector format

They are shown below

UE_LOG(LogTemp, Warning, TEXT("I just started running"));
Copy the code

GEngine->AddOnScreenDebugMessage(- 1.5.f, FColor::Red, TEXT("Screen Message"));
Copy the code

GEngine->AddOnScreenDebugMessage(- 1.5.f, FColor::Orange, FString::Printf(TEXT("My Location is: %s"), *GetActorLocation().ToString()));
Copy the code

Here is the complete.cpp file (using macro definitions simplifies the steps of printing debugging information)

// define a print message function to print to screen
#define print(text) if(GEngine) GEngine - > AddOnScreenDebugMessage (1, 1.5, FColor: : Green, text)
#define printFString(text, fstring) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Magenta, FString::Printf(TEXT(text), fstring))
#include "ConsoleLog.h"

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

}

// Called when the game starts or when spawned
void AConsoleLog::BeginPlay(a)
{
	Super::BeginPlay(a);// Standard way to log to console.
	UE_LOG(LogTemp, Warning, TEXT("I just started running"));

	// Log to Screen
	GEngine->AddOnScreenDebugMessage(- 1.5.f, FColor::Red, TEXT("Screen Message"));

	FVector MyVector = FVector(200.100.900);

	// log vector
	GEngine->AddOnScreenDebugMessage(- 1.5.f, FColor::Orange, FString::Printf(TEXT("My Location is: %s"), *GetActorLocation().ToString()));

	// Use the shortcut defined above
	print("Hello Unreal");	
	printFString("My Variable Vector is: %s", *MyVector.ToString());
	
}

// Called every frame
void AConsoleLog::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}
Copy the code

The result is as follows

First printed on the bottom, last printed on the top

\