center realtime text


I have looked around and can’t find a solution.
Is there a way to center the text, no matter how long the text will be?

I got a ray shooting in y direction, i want to show/center the text when i hover over an object with a property in it.
i got everything working except the centering of the text, i tried .format() and textWrapper but non of them have the right result. (if i make a text block of 32 characters, and the text is 32 is perfect but when the text changes to lets say 4 characters it is not centered anymore)

i don’t know if this helps ,i do something similar to what you describe in this test.

You have the same problem, you notice it less due to i have it at the crosshair.

So how do we get it centered?

only when the text have a uneven number of characters

switch with this and you get almost pixel perfect centered text

import bge

cont = bge.logic.getCurrentController()

dict = bge.logic.globalDict

ray = cont.sensors['ray']

if ray.positive:
    dict["HUD-Target"].worldPosition.x = 0

    words = len(ray.hitObject["objname"])
    characterwidth = 0.0924
    ofset = (words/2)*characterwidth

    dict["HUD-Target"].worldPosition.x -= ofset
    dict["HUD-Target"]["Text"] = ray.hitObject["objname"]
    dict["HUD-Target"]["Text"] = ""

i have re-uploaded the blend

How about to justify text? The option exists in the font tab but does nothing in-game.

it would surely be easier if those buttons did something.

switch with this and you get almost pixel perfect centered text

this works for me, thanks for the solution!

no problem :cool:

remember it only works on mono space fonts

that’s not a problem, i’m using in game text object/font, as long as that works i’m happy :slight_smile: