So im working on a RPG-Ish text box, where the text is displayed trough a typewritter effect ( every second a character / letter from the string is added to the displayed message on the screen ). Since my messages go off-screen im trying to set up word wrapping for it, i have something rudimentary working alredy but the result is inconsistent
I Have a function called UpdateTextWrap where i "figure out " the character index at which i should add a new line, Then once the current message on the screen reaches that index it adds the new line and calls the function again so it updates the index
MessageCharacters is an string array that has one elemenent per character in the string that contained the dialogue (using GetCharacterArrayFromString)
Here is the code im using
void UGalileaDialogueW::UpdateTextWrap()
{
int32 wordPosition = 0;
int32 length = 0;
for (int32 iw = LoopFrom; iw < MessageCharacters.Num(); iw++)
{
wordPosition++;
// if a character, start counting
if (MessageCharacters[iw] != " ")
{
length++;
}
//if we hit an space then stop counting
else
{
length = 0;
}
// if the length plus our position goes beyond the line end, then wrap where the word began
if (length + wordPosition > lineEnd)
{
//grab the position from the index, then remove the length from it so we go back the amount of
//characters the word had
WrapAt = iw - length;
LoopFrom = iw;
break;
}
}
}
Here is the snippet from where i update the dialogue each tick and call the wrap text function when the index matches
void UGalileaDialogueW::UpdateDialogue()
{
// if our current index matches the wrap one then do so
if (CurMessageIdx == WrapAt)
{
space = 0;
line++;
WrapAt = 0;
// call the function again
UpdateTextWrap();
}
// if our current space goes beyond the line then add a new one
if (space > lineEnd)
{
space = 0;
line++;
}
The function works correctly when called “once”, It updates the wrap at text index perfectly ( which is shown as the 48 in the screenshot, thats the character position at which it will wrap ). However once i call the function a second time, the index is NOT updated it remains at the same value
Each text character is its own widget. With some changes so I can alter each character position and rotation without being limited to the text box. Hence why I can’t / I’m not using a text box widget for this
For each character a Widget? You gonna run into performance trouble very very Quickly!
And here I was prepping some screenshots =/ well Posting them anyway in case you change your mind or if you want to addapt to my method.
You could Parse your Words into Array of strings with a empty space delimiter. You would basicly need to add the Lengths of the Words (+ empty spaces dont forget them) until they reach your MaxCharacterPerLineIndex typewrite them until done add new line repeat until your Word array is empty. Sounds about right ^^
Yeah its certainly taking a toll in perfomance :P, but so far this is the only way i have found that allowed me to modify the position of text characters individually, In any case could you explain a bit more im a bit confused on how the Parse Into Array string function can help me sort the issue?, I can count the spaces & length of my words until it reaches the max characters per line with no issue but then im stuck since i need to figure the index " before " the word that went over- the index so i can add the new line there
Also thanks for everyone else suggestions and help!
Haha glad it worked out. And yes some adjustments needed had to rush it. Wanted to check it out in around 30minutes and test on my End. I leave a Answer Post so you can mark it as resolved =)
Halfway Answer in the Comments. Good enough to finish it up with a couple adjustments to the Code. If someone needs Help on the same topic feel free to leave a comment.
If you want 100% justified text, just add extra spaces via code on the lines with less “wide” letters like M or W. Or, use a font with equal spacing for all characters.