1
$\begingroup$

I want to change a text dynamically using the game engine, that is, the bge module. I want to do it through a text box and I found a question about it. The problem is that in a part of it says to add a text object. I do not know how to do it, because I can not find the option "text object" in the "add" button of the "Logic Editor" bar. The code I´m using is this:

# import game engine modules
from bge import render, logic
# import stand alone modules
import bgl, blf

# create a new font object, use external ttf file
font_path = logic.expandPath('C://Documents and Settings//Guillermo//Mis documentos//Google Drive//Gproyectos//Proyectos con Blender//Zeyada.ttf')
# store the font indice - to use later
font_id = blf.load(font_path)
chain = 'this is the message'

def write():
    """write on screen"""
    width = render.getWindowWidth()
    height = render.getWindowHeight()
    # OpenGL setup
    bgl.glMatrixMode(bgl.GL_PROJECTION)
    bgl.glLoadIdentity()
    bgl.gluOrtho2D(0, width, 0, height)
    bgl.glMatrixMode(bgl.GL_MODELVIEW)
    bgl.glLoadIdentity()

    # BLF drawing routine
    blf.position(font_id, (width * 0.2), (height * 0.3), 0)
    blf.size(font_id, 50, 72)
    bgl.glColor4f(1.0, 0.0, 0.0, 1.0)
    blf.draw(font_id,chain)

def update(cont):
    own = cont.owner
    chain = own['msg']

# set the font drawing routine to run every frame
scene = logic.getCurrentScene()
scene.post_draw = [write]

I call to this piece of code, drawtext.py. In the scripting window this is the code I have written:

import drawtext
drawtext.chain = 'Hello Mind Maps!'

But I suspect that drawtext.chain must be linked to a text box from which you can enter text in real time.

$\endgroup$
0
$\begingroup$

you can edit written text via string game property but you need to assign it to your drawing property in this method, here is the code.

# import game engine modules
from bge import render, logic
# import stand alone modules
import bgl, blf

# create a new font object, use external ttf file
font_path = logic.expandPath('//C://Documents and Settings//Guillermo//Mis 
documentos//Google Drive//Gproyectos//Proyectos con Blender//Zeyada.ttf')
# store the font indice - to use later
font_id = blf.load(font_path)
# we get the game property and assign it before drawing so we can change it always 
cont = logic.getCurrentController()
chain = cont.owner['msg']

def write():
    """write on screen"""
    width = render.getWindowWidth()
    height = render.getWindowHeight()
    # OpenGL setup
    bgl.glMatrixMode(bgl.GL_PROJECTION)
    bgl.glLoadIdentity()
    bgl.gluOrtho2D(0, width, 0, height)
    bgl.glMatrixMode(bgl.GL_MODELVIEW)
    bgl.glLoadIdentity()

    # BLF drawing routine
    blf.position(font_id, (width * 0.2), (height * 0.3), 0)
    blf.size(font_id, 50, 72)
    bgl.glColor4f(1.0, 0.0, 0.0, 1.0)
    blf.draw(font_id,chain)

# set the font drawing routine to run every frame
scene = logic.getCurrentScene()
scene.post_draw = [write]

all you have to do is changing the owner property "msg" it will be drawn, you can also put a keyboard sensor and set the "msg" string as keystrokes log property so it will receive the keystrokes and display, them you don't need to connect it to the python controller but make sure that the code is always running, the code should be with the object that has the property "msg".

$\endgroup$

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.