Debug info string to text object


I just wanna to print debug info from “speed” string into a text object to show car speed.

import bge

cont = bge.logic.getCurrentContoller
own = cont.owner

textobject["speedo"] = speedo

This is my code but probably its wrong
Any ideas how to do it?

what, ‘speedo’ is not defined?
you have to feed speedo with getLinearVelocity before feeding textobject[“speedo”] with speedo ?

I want to display speed as numer, not as “arrow” in speed meter

maybe we have to get into some math to convert the vectors into float (i never did it for that).
sum the components and divide by three?

I have this debug info. As you can see in debug info one shows current car speed. I just wanna to show this value in Text object in HUD
Is it possible to set text value in text object using python?

if something is possible it’s through python.
i think you have to convert the int ‘speed’ variable to a string feeded to the text property.

I found on YouTube a car physics script a few minutes ago and this script/blend file already have speedo meter and script for it looking like this

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

scene = bge.logic.getCurrentScene()

C1h = scene.objects ['C1h']

own["Text"] = (C1h.getLinearVelocity(True).y)*3

This is hierarchy
And with this small script we can see actual car speed

oh… then:

the text object text property has to be called “Text” like:
textobject[“Text”] = speedo

you cannot say textobject[“speedo”] = speedo
but you dont need to convert it into a string.

why show the hierarchy? if you put the code on the text object it becomes the own.

car = scene.objects [‘car’]
speed = car [‘speed’]
own[“Text”] = speed

textobject.text = speedo

1 Like

This is a my edit of this code from link

# Display a message about the exit key using a Font object.
import bge

scene = bge.logic.getCurrentScene()
co = bge.logic.getCurrentController()
font = co.owner

CoM = scene.objects ['CoM']
speeds = CoM.getLinearVelocity(True).y*3
font.text = (speeds)

I’m trying to get velocity from parrent object called “CoM” and display it in text object as number (car speed) but I’m doing something wrong because I’ve got only “Text” value

Strings are character arrays. One can’t just set an array to equal a single numerical value.

You need to convert it first. text = str(value)
Or use fancy format. text = "Speed: %f"%value

And if you want to show less decimals, text = "Speed: %.Nf"%value where N is the number of decimals. So text = "Speed: %.2f"%value prints up to two decimal places while text = "Speed: %.3f"%value shows up to three.