1
$\begingroup$

I am working on a "display name" with a text object added from a inactive layer to the current scene.

I startet just changing the visibility, then i wanted a "scaling visibility". For that (since the base class of the text object is game object) i try texObj.meshes[0].material[0].alpha but found that there is no meshes.

Then i put a plane.color =[0,0,0] behind the text and meet the colors (text.color=[amount,amount,amount]), amount = amount + or - 0.02 between 0.0 and 1 to slowly make a "scaling visibility" of the text, But this requires the plane as background of the text which i want to not use if there is a way to do it without a background.

Also i have google "bge python text to mesh" trying to found a way to convert the text to mesh so i could add the text object then edit,remesh,show,hide, endObject but only found the bpy reference which not work in bge runtime.

And of course a dont want to convert the text to a mesh (Alt+C) since i will unable to change the text.

$\endgroup$

2 Answers 2

1
+100
$\begingroup$

Animate the alpha of GameObject.color

Tested using gamelogic module template, with a mouse sensor on text object. Clicking 10 times makes the text object "fade away".

enter image description here

def main(cont):
    own = cont.owner
    # color vector xyzw  ~ rgba
    own.color.w -= 0.1
    own.text = str(own.color)
$\endgroup$
1
  • $\begingroup$ LOL I did not think about the color alpha, now i can change the color too, thank you it work perfectly. $\endgroup$ Commented Nov 18, 2017 at 17:45
1
$\begingroup$

Assuming you already know about the limitations of text objects in the game engine: pixelated text, no materials, does not look the same in game as the real text object, depth buffer issues, etc. and still want to use a text object batFINGER's answer is pretty much what you want to do object.color = [r,g,b,a].

However I would recommend you use the Font Drawing module blf.

The example on the documentation page is almost all you need to know to start writing text using blf. Here is a very similar example.

import bge, bgl, blf

def init():
    """init function - runs once"""
    global scene
    # create a new font object from external ttf file and store the font indice - to use later
    bge.logic.font_id = blf.load(bge.logic.expandPath('//Roboto-Regular.ttf'))

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


def write():
    """write on screen"""
    width = bge.render.getWindowWidth()
    height = bge.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
    font_id = bge.logic.font_id
    blf.position(font_id, 10, (height - 45), 0)
    blf.size(font_id, 40, 72)
    bgl.glColor4f(.02,.2,.6,.8)
    blf.draw(font_id, 'Hello BGE!')

Some key things to note from that script.

  • expandPath('//Roboto-Regular.ttf') is looking in the same directory as the blend for that font file. Replace with your own.
  • The position is in the top left with 10px margin-left. The position's origin is bottom left, so height-45 brings the text to the top minus the height of the text.
  • bgl.glColor4f this is how you set the color with an alpha value (r,g,b,a) all 0 through 1 values. (I have it to a semi-transparent blue color)
  • blf.draw what actually draws the text! to make it more useful use a string type game property.
$\endgroup$
1
  • $\begingroup$ This is what I needed from the beginning?... it's a case study, thank you!. This is way better if can get it work as i want, but i done some test and was unable to change the text, found that the color need to be in the string and i couldn't "make copies", etc. the only thing that worked was shadow fade in/out , i will work on it when i have some time. For the moment object.color = [r,g,b,a] is my answer. $\endgroup$ Commented Nov 19, 2017 at 7:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .