Instanced text for the GE

Hi there,

I was making an inventory for game and I hit a wall, that wall being the wall of “how do you cater for an amount of text that is unknown”. So I got into my bulldozer and this is how I beat it.

Layer 2 (Because I like to go backwards >:D): add a plane > texture as realtime text > leave ‘Text’ property blank > name the plane to ‘Text’

Layer 1: Add an empty and call it ‘control’ or something cool like ‘brad_pitt’ > add an always sensor, python controller and an edit object actuator set to add object > Text.

Now for the juicy code that runs this bulldozer.

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

scene = GameLogic.getCurrentScene()

add = cont.getActuator("add")

GameLogic.addActiveActuator(add, 1)
objects = scene.getObjectList()
objects.reverse()
pos = own.getPosition()
own.setPosition([pos[0], pos[1], pos[2] - .5])
objects[0].Text = pos[2]

This little bit right here

objects = scene.getObjectList()
 objects.reverse()
object = objects[0]

can also be done like this:

objects = scene.getObjectList()
object = objects[len(objects) - 1]

Finally add the script to the controller and check it out, or if you lazy download my attached example.

In my example I just set the text the z position of the text object but it has other uses, such as if you wanted to display all the contents in a directory, an inventory (as I used it for) or a load script.

This example is just demonstrating how you can control objects after they have being added by a actuator. You can use this method for controlling baddies that have been spawned.

Attachments

instanced_text_example.blend (144 KB)

You forgot to pack the textures…
I did something similar months ago, but well done. You can just use actuator.getLastCreatedObject()

Ahh yes textures, I always forget that.
It shall be done.

Very good mate, well done.