Download

Concating strings and other things

Hi guys got small problem but can’t figure out how to concate properly inside unreal.

Basically I have a FName variable like this
FName name = *requiredComponent->GetName();

Which works fine howether I’ve been getting crashes whenc creating multiple things and after debugging I found out the reason for the crashes is because multiple objects can’t have the same name

There fore I need a way to add for example _01 , _02 etc after each name the same as what unreal does

So for example say I made 2 box collision components
First one would be name = boxCollisonComponent_01
Second would be name =boxCollisionComponent_02
Etc

But I can’t figure out how to do it.

I tried making an int that increases by 1 each time and convert that to a string and then append that to the FName variable but that didn’t work.

Thanks

I’m a bit confused. The “requiredComponent” was generated by you on code?, or is something coming from a blueprint?.

If it was generated by you manually, you could either name them differently or if generated by code add the logic to randomize the name there (or add _01, _02 and so on). If this is something that was generated by Unreal then shouldn’t they already have different names?.

What’s the type of requiredComponent?, just to have more info on the issue could you show a little more code?.

Alternatively, you could use FString for all the tools it provides, and if this is something that will travel through network you can later convert it to FName (minding size). Also note FName is case insensitive, so might not be the best choice for unique identification.

Have a look at this
https://wiki.unrealengine.com/String_Conversions:_FString_to_FName,_FString_to_Int32,_Float_to_FString

Thakyou but I have already looked at that and it is very helpfull for learning straight forward conversions but what I need is a bit more complicated

As i need to convert
FName to a string
Pass an int variable into a string
And then add the int variable/string onto the end of the other string .
Then convert it all back into a FName as FName is the only variabls type that the function im using will accept as the parameter.

Also im not really struggling with the conversions but more rather the concating (adding together of the strings etc ) there was nothing in that wiki about concating strings etc and the way it works in unreal seems to differ slightly from standard c++.

Everything I try doesn’t give me any compiler errors or anything but I just cannot seem to get the desired result

Thanks

What about



FName name = *requiredComponent->GetName();
FString base = name.ToString();
int32 index = someIndex;
base.append("_");
base.append(FString::FromInt(index));

FName newName = FName(*base);



wow very interesting and cool way of doing it. didn’t realise there was already ‘append functions available’ to help i was trying to do it all myself.
worked perfectly.

thanks for you’re help

No problem. Also you can just say fstring1 += fstring2;
It’s even easier than calling append();

I think that’s what I was trying in the beginning and was getting weird results which is why I said I think unreal does things differently howether looking at my old code I think the problem may have been I was declaring the int as int not int32 .
Anyway all sorted now . Thanks again

Hmm maybe you tried to += with the integer? Always convert other types to FString before you call +=. Not really sure, if that not should work too, but just to make sure. There are conversion functions for allmost all F variables, FRotator, FVector, FName, (.ToString()), float is FString::SanitizeFloat(floatname), int32 is FString::FromInt()

Why not use an array or a dynamic array for the object creation…