So I have been trying to work around this issue I have come across while creating a word processing blueprint. I am trying to create a way to center the text within the blueprint. My thought process is to get the position of the last placed character, divide that number by 2 (to get half the distance from the origin) and then subtract that number from the world position of the blueprint.
Below is my current set up, though the results are less than desirable. Any ideas?
What do you use to show your text in your blueprint ? If you use a text render, you can center the text by selecting center in the alignements settings (in detail / text) and your text will be centered on typing update.
I am not sure to have right understand but if you use one mesh for each letter, then you would save each mesh in an array “text”. Each time a letter is added, you add it to your array, then get its length, divide by 2. Function or custom event : for each loop from your array :
Set location = size of letter * (index - return value from divide)
If remainder from divide = 0 : peer : add half a size to each set location.
I suppose that each letter mesh has its origin centered, if not, you’ll have to deal with this offset.
I couldn’t get the array to work since each letter is a different size, But I got the BP below to work near perfectly.
One problem: The ‘Realign’ bool is telling the BP if it needs to center on X or Y. The issue is that if I have one that needs X and another that needs Y in the same level it will only ever run the true(Y) bool.
No, Sorry. I guess I didn’t explain well enough what I am trying to do.
The player at the beginning of the game will enter some text. Then throughout the levels this text will be displayed in several areas. But placed almost like signs, it will be static after it is placed. I just need to be able to center it based on the length of the string. Which is working above, but only for the ‘True’ branch. Even if I have one that is set to false.
The above script is in the construction graph with everything else I have shown here. I tried moving everything in the ‘Center Text’ comment box to the event graph and running it on begin play but that didn’t work at all.
I had to go away but here is the bp : I used the basic cube shape with a custom event, you’ll have to do with your own typing system but : You create an array of staticmesh component “text” and nothing more in bp :
I just had to change the multiply and add values to negative. The words were displaying backwards, with some letters stacked completely on top of each other.
Now I just gotta figure out how to add spaces and kerning back in, since multiple words can be used. The issue being that the letters are not a uniform size.
Happy that helped ^^
To have it working with different sizes of letter, the idea i would follow would be to use a second array.
Each new letter is added in mesh array and in size array (from a third array with all sizes registered) then instead of counting number of meshes, you add all sizes in array, divide by 2 and set a var “locate” with negative of it. On loop from mesh array : set letter location with “locate”, set locate = locate + current letter size. And if you add space to your registered array, it’ll be done too.
Here is my idea, i had to add a few things : there are 3 arrays : “text” the one static mesh added on input, “textsize” the one size of letter added on input and “textref” the one with size references set before start. Each time you add a letter, you need to get the right text size from “textref” and add it to “textsize”. I used 2 meshes one 100 and one 50 (set 0 : 100 and 1 : 50 in “textref” array), and a offset of half the size of current letter was needed. Once again, you’ll have to adapt to your system.
Edit : i suppose that check text length is useless but in case of i didn’t remove it. I did separate nodes for add meshes and set letter, that could be set in a single function to be cleaner and easier with more entries. Even, letter could be set from an array too (by find index refering to entry)
Check it out! I made some minor changes to your build. I took out the center section since I am generating all meshes from the same event. Ran the index in from the initial ForLoop. And I added the top section to get / store the size of the meshes.
Here is the resulting BP.
It works perfectly, Except it is backwards. hahaha.
All of the spacing is perfect though!
When I turn the final Locate negative though. The sentence is flipped, and the letters are the right direction… But now the spacing is all messed up again.
I am going to continue to mess with it. I’ll let ya know what I come up with!
I may try just editing the Kerning Adjustments for each letter to save time.