populate a text object using python

hi,
just learning the basics…
how do I populate a text object in the game engine using Python? I’d like to know how to populate an existing actor (text object). Is it possible to create text objects on the fly?
thanx in advance
finty

First, are you talking about the special Game Engine Text, or the normal Blender Text Object? I will explain to you how to update a Game Engine Text object. If you don’t know how to use those, I highly recommend following ST150’s very helpful tutorial.

It’s very easy to populate a game engine text object using Python, since the Text value is a property of the text object. If you are running the Python script from the text object itself, use this code:

cont = GameLogic.getCurrentController()
own = cont.getOwner()

own.Text = "Type your desired string here"

You could also update the text with another property. Let’s say you gave the text object another string property called “win” (by using the “Add Property” button in the Logic Buttons [F4]) and give a value of “You Win!”. Then you could use this code to inform the player that they had won:

cont = GameLogic.getCurrentController()
 own = cont.getOwner()
 
own.Text = own.win

If you wanted to update the text object from another object’s logic section, you could either select both objects in the 3D view and connect the logic bricks between the two objects, or you could access the text object via Python like this:

textObj = GameLogic.getObjectList()["OB<i>yourTextOjbect'sName</i>"]

textObj.Text = <i>desired value</i>

To generate text objects on the fly, place one text object on a layer that is not visible during gameplay, then use the “Edit Object >> Add Object” actuator to add it when desired.

Also, just as a note, the “Text” property of the game engine text object can be either a string or an int, so it be used to keep score as well. If you’d like to learn more about text in the game engine, please read some of the tutorials on my site in the “Realtime Text” section. You may also enjoy “Making an On-screen Timer”. It’s one of my favorites.

thanx blendenzo. This is exactly what I was looking for.
I assume it’s not possible to create game engine text objects on the fly. I found Social’s Intro tutorial to Python for the GE. He mentioned that logic bricks need to exist.
thanx again

If by “on the fly” you mean with pure Python, then you cannot do it. There is no way to add an object that does not already exist in a game with Python. Like I said, the best way to create game text objects on the fly is by using the same method one would use to add bullets ( that is, the “Edit Object >> Add Object” actuator).

cheers blendenzo,
you’ve got some good tutorials on your site. The third option you suggested took me ages to get working because I was leaving out ‘OB’ as part of the object’s label (its kinda weird having to do that). I changed the line to:
textObj = GameLogic.getCurrentScene().getObjectList()[“OByourTextOjbect’sName”]
thanks again. you’ve got me started now