Print an objects name at runtime

Hi,

I am following a course and the instructor is showing how to print the name of an object. He does it this way:

void UPositionReporter::BeginPlay()
{
	Super::BeginPlay();

	FString ObjectName = GetOwner()->GetName();
	UE_LOG(LogTemp, Warning, TEXT("Position report reporting for %s"),*ObjectName);
	// ...
	
}

I got an error saying:
pointer to incomplete class type is not allowed

I tried to do it this way:

void UPositionReporter::BeginPlay()
    {
    	Super::BeginPlay();
    
    	FString ObjectName = GetName();
    	UE_LOG(LogTemp, Warning, TEXT("Position report reporting for %s"),*ObjectName);
    	// ...
    	
    }

But it would print the name of the c++ file and not the name of the Actor. Why isn’t it working?

what is a base class for UPositionReporter?

It appears that you have a Component that you would like to report on the Actor that owns it. GetOwner() returns an AActor*. If your class (which I assume is inheriting from ActorComponent) does not include Actor.h in some way, AActor pointers will be incomplete pointer types. As the compiler tells you, these are not okay to call functions on.

On another note, GetName() will normally return the name of the game object instance, for many actors, components, and object this means that it will be [class_name][instance_number] ie: MyActor_0, MyComponent_0, or MyObject_0. The number often represents a spawned instance count, so if 20 actors of the same class were in scene, I would have MyActor_0 - MyActor_19
Check out the answer to this post

To solve your " pointer to incomplete class type is not allowed " issue, make sure to include Actor.h in your class’s .cpp file, as it is required and not included in ActorComponent.h.

For me this just outputs nothing but “text”

FString a = GetOwner()->GetName();
UE_LOG(LogTemp, Warning, TEXT(“text”), *a);

You need to tell where a needs to be printed (use ‘%s’). :wink:

FString a = GetOwner()->GetName();
UE_LOG(LogTemp, Warning, TEXT("text: %s"), *a);