FName + FName Error

FName LEV_Mode = "_PLAY_";
int32 LEV_Current = 10;

FName LevelJump = "LEV_" + LEV_Current; // OK

FName LevelJump = "LEV" + LEV_Mode + LEV_Current // ERROR! 

UGameplayStatics::OpenLevel(GetWorld(), LevelJump);

How to join 2 FNames without converting it all into strings?

This post should help you

The only workaround for joining FNames was to convert to a FString, do my concatenations, then convert back again.

Dumb. Me or FName?

In general, FNames are usually something you aren’t stringing together dynamically. What a re you using this dynamically built FName for?

just to get names dynamically of level to load

LEV_001, LEV_002, LEV_003 …
The built in UE static uses an FName for the level reference…so I built my concatenate function using FNames…well bad idea!

1 Like

I don’t think you want to be using or operating on FNames until you absolutely need them.

Here’s another example of needing to concatenate two FNames.

void UMyWidget::PostEditChangeProperty(FPropertyChangedEvent& e) {
	if (e.GetMemberPropertyName() + e.GetPropertyName() == GET_MEMBER_NAME_CHECKED(UMyWidget, configStruct.state)) {
		StateChanged_Event(configStruct.state);
	}
}

That said, AFAIK the following is all you can do.

FName memberName = e.GetMemberPropertyName();
FName propertyName = e.GetPropertyName();
FName combined = FName(memberName.ToString() + "." + propertyName.ToString());

I’d like to note that both of those options are NOT ok:

FName LevelJump = "LEV_" + LEV_Current;
FName LevelJump = "LEV" + LEV_Mode + LEV_Current;

In first case you doing char* + int which doesn’t have sence in given context.

In second case - you can’t concat FName with int directly and have to convert this int to string type first. Hence, something along the lines should work:

FName LevelJump = FName(”LEV_” + LEV_Mode.ToString() + FString::FromInt(LEV_Current));

Iirc FNames are supposed to be static, so you have to make your math on regular strings first.