What is the return of AActor::GetName()?

Hey, AActor::GetName is not returning the name of my Actors inside the editor. They have additionally appended an underscore and a number:

Editor: Sun

AActor::GetName: Sun_6

How comes this, and how can I prevent it?

I see that GetActorLabel works well, but only in debug.
How can I set which name my actor has at the beginning?

Is there any particular reason why you need an engine function that returns the string without the appended “_6”?

If not, why not just modify the string to remove the appended information that you don’t require?

I only wanted to know, why it is 6 and not 8, so what determines the number after the _

GetName is returning the actual name of the object, which for lack of a naming by you in SpawnActor or ConstructObject is the class name and a monotonically increasing number.

FActorSpawnParameters has a Name variable for setting an actor name

ConstructObject takes a name as the 3rd parameter

If you want the class name, then UYourClass::GetClass()->GetName(), but obviously that is not unique

I did a bit of the survey to figure out all kind of methods available to get name of actor or component. Here are few notes:

  1. GetName() value, when auto generated, can change run over run. In my tests development and shipping builds actually had differently generated names.
  2. If you do change name in editor than that stays the same run over run.
  3. GetFName() returns same value but as FName.
  4. GetFullGroupName() returns level by default.
  5. GetFullName(true) returns full unique name. For example, for pawns it would return MyCppPawn /Game/FirstPersonCPP/Maps/UEDPIE_0_FirstPersonExampleMap.FirstPersonExampleMap:PersistentLevel.MyCppPawn10 and for StaticMeshComponent it would return StaticMeshComponent /Script/Engine.Default__DefaultPawn:MeshComponent0.
  6. Actors have HumanReadableName() method which currently just returns GetName().
  7. Actors have GetDebugName(actor) method which currently just returns actor.GetName() if actor != nullptr.
  8. Actors have ActorLabel property and GetActorLabel() but both of which is editor only which means your code will fail to compile if you tried to package a shipping build with that call.
  9. There is UKismetSystemLibrary::GetDisplayName() which returns label in editor mode otherwise owner.GetName() + component.GetName().
  10. As the autogenerated names are not stable, current recommendation is to either explicitly name things in editor OR use tags on actor which can be checked using ActorHasTag(name) method.
6 Likes

Thank you for doing this. This is the sort of information that should be in the official documentation. Knowing that the name is not stable is very valuable.

If you hover over the object in the World Outliner, it will show the “ID Name”. This is what AActor::GetName() is returning.