Centered text on Text objects in the bge

the trick is to use a mono space font and have a fixed length text object and use python function format() to center the text

example:


text = "{:^32s}".format("TEST")
print(text)

outputs:
"              TEST              "


here is how it is used in the test blend below


import bge

cont = bge.logic.getCurrentController()

own = cont.owner

own["Text"] = "{:^32s}".format(own["nametag"])  
own.resolution = 3


http://15b.dk/blendfiles/centered-text.blend



You can do that too for non-monospaced fonts using blf

i tried that first ,did not get a good result.

plus this tut is aimed at beginners.

This is good, I use this for formatting. You can also left or right align text, or have a split with a left aligned part and a right aligned part to get a result like this:


Skill:              120
Speed:            15
Wisdom:           2
Age:   10000000

table = [["Skill", 120],["speed", 15],["Wisdom", 2],["Age", 10000000]]

for group in table:
    label, value = group
    text = '{:<6}{:>6}'.format(label,value)
    print(text)

This is really great :D!

i know :yes:

i use something similar her


http://15b.dk/blendfiles/spawner-test.blend

:cool: thanks