Formatting a String with three int's

I am using UnrealEngine and Rider for the C++ side.

Morning all, Hope I can ask this here.

I am following a instructor for a course but cannot get this resolved because its not part of the course.

So here is his original code.

void FInv_LabelNumberFragment::Assimilate(UInv_CompositeBase* Composite) const
{
FInv_InventoryItemFragment::Assimilate(Composite);

if (!MatchesWidgetTag(Composite)) return;

UInv_Leaf_LabeledValue* LabelValue = Cast<UInv_Leaf_LabeledValue>(Composite);
if (!IsValid(LabelValue)) return;

LabelValue->SetText_Label(Text_Label, bCollapseLabel);

FNumberFormattingOptions Options;
Options.MinimumFractionalDigits = MinFractionalDigits;
Options.MaximumFractionalDigits = MaxFractionalDigits;

LabelValue->SetText_Value(FText::AsNumber(Value, &Options), bCollapseValue);
}

it displays a float value called Value in a item description widget. It works no problem.

Now I want to display a 2d6 +3 type of value.

NumberOfDice d TypeOfDice + ExtraDamage.

So this is my code, but cannot figure out to properly write the code.

void FInv_DamageNumberFragment::Assimilate(UInv_CompositeBase* Composite) const
{
FInv_InventoryItemFragment::Assimilate(Composite);
if (!MatchesWidgetTag(Composite)) return;

UInv_Leaf_LabeledValue* LabelValue = Cast<UInv_Leaf_LabeledValue>(Composite);
if (!IsValid(LabelValue)) return;

LabelValue->SetText_Label(Text_Label, bCollapseLabel);

FNumberFormattingOptions Options;

*//LabelValue->SetText_Value(FText::AsNumber(Value, &Options), bCollapseValue);
*
LabelValue->SetText_Value(FText::FromString( FString::Printf(TEXT(“%i d %i + %i”), GetNumberOfDice() , GetTypeOfDice(), GetExtraDamage(), &Options), bCollapseValue));

}

this is the line to fix.

LabelValue->SetText_Value(FText::FromString( FString::Printf(TEXT(“%i d %i + %i”), GetNumberOfDice() , GetTypeOfDice(), GetExtraDamage(), &Options), bCollapseValue));

any help would be great. Been at this for days.

Found this post about using FText::Format on a String Table

In your case, the code (not tested) could be something like this:

int32 Value1 = 1;
int32 Value2 = 2;
int32 Value3 = 3;

FText FormatInput = FText::FromString("{Int1}d{int2} +{int3}"); // 2d6 +3

FFormatNamedArguments Args;
Args.Add("Int1", Value1);
Args.Add("Int2", Value2);
Args.Add("Int3", Value3);
FText FormattedText = FText::Format( FormatInput, Args );

ok how would i fix the last line to set

LabelValue->SetText_Value(FText::FromString(FormatedText, &Options), bCollapseValue);

i have to pass it like to original line of

LabelValue->SetText_Value(FText::AsNumber(Value, &Options), bCollapseValue);

with Replacing Value with FormatedText?

Replace the FText input parameter with FormattedText like so:

LabelValue->SetText_Value(FormattedText, bCollapseValue);

Final code could be something like this:

FText FormatInput = FText::FromString("{DiceCount}d{DiceType} +{ExtraDamage}");

FFormatNamedArguments Args;
Args.Add("DiceCount", GetNumberOfDice());
Args.Add("DiceType", GetTypeOfDice());
Args.Add("ExtraDamage", GetExtraDamage());

FText FormattedText = FText::Format( FormatInput, Args );

LabelValue->SetText_Value(FormattedText, bCollapseValue);

Close but AsNumber needs the first argument as a int or Uint or float.

You don’t need FText::AsNumber() anymore. Replace it with FText::Format() like so:

LabelValue->SetText_Value(FText::Format(FormatInput, Args), bCollapseValue)

(post deleted by author)

thank you so very much.

Fixed?

yes

Nice, glad I could help. Please mark the post that helped fix the issue as Solution so others could find it. If none of the posts are the solution, please post the solution yourself, thanks.