Controlling a text object through python. Hooking it to an internal variable Okay so I'm using a text object eventually odd like to make it into a health counter. My code is sloppy though and needs refining.

Here is what I got.

import bge
from bge import logic

cont = logic.getCurrentController()
own = cont.owner
scene = logic.getCurrentScene()
T = scene.objects['Text']
z = cont.sensors['z']

if z.positive:
    T ['x'] += 1

if T ['x']  == 0:
    T.text = 'failure'

if T ['x']  == 1:
    T.text = 'success'      

if T ['x']  == 3:
    T.text = T['x']


The last line didn't work, the money says that it returns without exception set. This is just a test to find something that sort of works. But ideally I'd like the texts to simply display a variable as it changed. Z is a keyboard sensor and x is a property linked to the text. Can someone tell me how I can just get the text to display the integer property x?

Also being able to reference the lines in a text file for text would be great but if that's to much to ask then please just help me with the first one.
When trying to execute your code, I get the error:

KX_GameObject, key "x" does not exist


Replace all your T['x'] with another objects property such as:

T.localPosition.x


If this isn't the property you want, you can search the objects fields with:

print(dir(T))


You have to parse/cast the int to a string with the python str() method.
The last line compiles fine as:

if T.localPosition.x  == 3:
    T.text = str(T.localPosition.x)