So i have some long “Text” variables that i store in a DataTable.
im getting some specific Row from the DataTable and inject the Text in a “Text Render Component”
I know i could use Shift+Enter to do a return to line while i write the Text in the DataTable but that’s not really practical due to the presentation of DataTables.
My goal is to :
Get the selected Text
Count an amount of characters from the start (ie : 12 or 15)
I found a way to do this in blueprints! Let me explain my method to you:
First of all, create the following variables in your BP:
Input String (String) - This is the Text from your Data Table transformed into a String
Current Index (Int) - This is the initial position from where we wil start to count characters
Char Limit (Int) - This is the max characters per row
Cut Position (Int) - This is de position in the String where we will be cutting each row
Formatted String (String) - This is the resulting String with the new format
You need to get the text from your data table using the node “Get Data Table Row” (right click on “Out Row” and split it to get your text variable) and cast it to String, then store it in your “Input String” variable
Now get the length of your “Input String” variable and make a While Loop when “Current Index” is less than that length. You can set the text of your text render in the “Completed” pin by casting the “Formatted String” variable back to “Text” type here. (I’m goin to explain how to process that variable in the next steps)
Now, we need to cut our “Input String” in pieces. We will use the “Mid” node to do that. This node will count X characters (our “Char Limit” variable) from a start point (our “Current Index” variable) and return all the characters counted as a String. Then we will find the las space in that string using the Find Substring node. (note that you need to type an actual space in the “Substring” text box)
The next step is to check if we found a space character in the current string fragment and set our variables accordingly. If we find a space, we need to set our “Cut Position” variable adding the returned value from the Find Substring node to our “Current Index” variable. If we didn’t find a space we need to add our “Current Index” to our “Char Limit” instead “Cut Position”
The reason we want to find the last space of a row is to add the jump to a new line there instead of cutting a word in half.
Ok, now we have our string fragment and we know if we found a space, we need to put our currect fragment and the jump to a new line in our “Formatted String” variable. To do it we will use the following nodes immediately after we set our “Cut Position” variable (keep in mind that you need to insser a new line in the B text box of your Append node by using “shift+enter”). And the last thing to do is to set the “Current Index” variable by adding “Cut Position” + 1
Hello Juan !
First i want to say sorry for the late reply, i stumbled on other issues with my code and i had to change the way my DataTable was working.
My goal is quite complicated, as i want to create a virtual console system that allows the player to input commands in a fake console prompt and examine theses inputs to find a console response output as well as executing specific Events when some inputs are validated.
I also figured that i could simply enter pre-formatted texts directly in my Datatable by checking the option : Multi-Line Text
However, this could be annoying in the long run, because the datatable interface is already cumbersome and it could be hard to manage hundreds of Rows in that fashion, so the text Auto-format seems like an elegant solution.
It would also be better in case i wanted to translate my game, that way i wont need to re-format the text for every language.
TBH i dont think i will translate my game but that’s something to keep in mind if that’s your case.
So I tried your code and it works like a charm !
Here’s a full view of the code in case anyone is interested :
In this case for the demo, instead of pulling the string from my DataTable i simply set the variable : Input_String with the following text as a single line :
““ THIS IS A CONSOLE COMMAND RESPONSE TEST, IT CONTAIN A DEMO TEXT TO DEMONSTRATE BRG_JUAN_CRUZ_MK TEXT AUTO FORMATTING PROTOTYPE. IT SHOULD CRETE BREAKS TO THE TEXT ALLOWING A SINGLE LINE STRING TO BE DISPLAYED IN MULTIPLE LINES BY A BLUEPRINT AUTOMATION. ””
As a Bonus, I coupled your method with a TypeWriter effect that will display the Auto-formatted Text character by character with a Timer By event and a delay of 0.02.
I just created another empty string variable storing the text character by character.
At the end of execution, i check if my Typewriter String is Equal to the Formatted String, if true, it stops the timer by event with it’s Timer-Handle to stop the loop.
Here is the code i added in place of the ending “Set Text” :