Manual Text Word Wrapping?


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++)
	// if a character, start counting
	if (MessageCharacters[iw] != " ")
	//if we hit an space then stop counting
		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;

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;
			WrapAt = 0;
	                // call the function again
                // if our current space goes beyond the line then add a new one
		if (space > lineEnd)
			space = 0;

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

Any help is certainly appreciated!

1 Like

Uhm a quick Question is there a reasson why you dont use the Auto Wrapping Feature? Not sure why you do it manually.

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

how about using UCanvas::WrapString?

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 ^^

Hope that helps you in some way =)

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!

Gotta run to Work. Untested Code I give it a run later did it in a Hurry.

I had to do some adjustments on my end but it works perfectly!, HUGE thanks. here is a quick celebration gif


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.

Basically, after 1 day I got it for blueprints.

Captura de tela 2023-03-26 163733

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.

Captura de tela 2023-03-26 180944

1 Like