1
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

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)
$\endgroup$
  • $\begingroup$ Thanks! I was missing that last str() command. Works perfectly now that it recognizes the integer as a string $\endgroup$ – c0mlink Oct 18 '16 at 19:47
  • $\begingroup$ Nice to hear. If this answer solved your problem, please mark it as accepted. $\endgroup$ – Leander Oct 18 '16 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.