Download

FString.append() not working in one instance

I’m confused as to why this works in one instance in my zero fill function, and then doesn’t work in another instance… What am I missing?

Here’s the zero fill function that is working:




FString AMyClassTest::ZeroFillInt32ToFString(int32 Value, int32 ZeroFillCount)
{
    FString InputValueStr = FString(TEXT(""));
    InputValueStr.AppendInt(Value);

    FString BuildReturnValue = FString(TEXT(""));

    for (int32 i = InputValueStr.Len(); i < ZeroFillCount; i++)
    {
        BuildReturnValue.AppendChar(TCHAR('0'));
    }

    BuildReturnValue.Append(InputValueStr.GetCharArray());
    return BuildReturnValue;
}


^^ That works like a charm

Then for some reason this doesn’t work! (its in my tick function



FString ZeroFillString = ZeroFillInt32ToFString(CurrentFrame, 4);
FString FileExtStr = FString(TEXT(".png"));

ZeroFillString.Append(FileExtStr.GetCharArray());

UE_LOG(LogTemp, Log, TEXT("ZeroFillString %s]"), *ZeroFillString);

CurrentFrame++;
return;


Its just NOT appending in that case… what the hell??

Here’s what it prints out



LogTemp: ZeroFillString [0000]
LogTemp: ZeroFillString [0001]
LogTemp: ZeroFillString [0002]
LogTemp: ZeroFillString [0003]
LogTemp: ZeroFillString [0004]
LogTemp: ZeroFillString [0005]
LogTemp: ZeroFillString [0006]
LogTemp: ZeroFillString [0007]
...


Also adding another UE_LOG I see this so the string definitely exists!! I’m going insane. Ugh.



LogTemp: FileExtStr .png]


Also maybe there’s an easier way to manipulate strings with the regular std::string instead and at the end of that convert it to an FString ?? coming from other languages where you can very easily concat and manipulate strings this is immensely frustrating. I must be missing something really, really dumb.

I think you may be overcomplicating building an FString. You can do it more simply, such as:


FString MyString = "Foo";  //MyString is now Foo
MyString += "Bar";  //MyString is now FooBar

Your function could be simplified to:


FString AMyClassTest::ZeroFillInt32ToFString(int32 Value, int32 ZeroFillCount)
{
   FString InputValueStr = "";
   InputValueStr.AppendInt(Value);

   FString BuildReturnValue = "";
   for (int32 i = InputValueStr.Len(); i < ZeroFillCount; i++)
   {
      BuildReturnValue += "0";
   }

  BuildReturnValue += InputValueStr;
  return BuildReturnValue;
}

And in your tick, just do:


FString ZeroFillString = ZeroFillInt32ToFString(CurrentFrame, 4);
ZeroFillString += ".png";

UE_LOG(LogTemp, Log, TEXT("ZeroFillString %s]"), *ZeroFillString);

CurrentFrame++;
return;

The += operator will concatenate the string literal on the right to the FString on the left, and save the result in the FString on the left. No need to mess around with TEXT() and GetCharacterArray() etc. Hope this helps.

AH there we go!!! And familiar to action script , javascript, php style way (with its . concatenation difference but still)

I didn’t realize I could just use that basic += with FString. It works! Now I’m wondering if append was some scope issues or referencing some ghost value that I was losing track of. Keep it simple.

Thanks a lot! I owe you one

Not a problem. Glad to help.