How to work with blf module for text in game

Copy/paste this code from docs:

# import stand alone modules
import blf
import bpy

font_info = {
    "font_id": 0,
    "handler": None,
}


def init():
    """init function - runs once"""
    import os
    # Create a new font object, use external ttf file.
    font_path = bpy.path.abspath('//Zeyada.ttf')
    # Store the font indice - to use later.
    if os.path.exists(font_path):
        font_info["font_id"] = blf.load(font_path)
    else:
        # Default font.
        font_info["font_id"] = 0

    # set the font drawing routine to run every frame
    font_info["handler"] = bpy.types.SpaceView3D.draw_handler_add(
        draw_callback_px, (None, None), 'WINDOW', 'POST_PIXEL')


def draw_callback_px(self, context):
    """Draw on the viewports"""
    # BLF drawing routine
    font_id = font_info["font_id"]
    blf.position(font_id, 2, 80, 0)
    blf.size(font_id, 50, 72)
    blf.draw(font_id, "Hello World")


if __name__ == '__main__':
    init()

But the text is displayed only on viewport, not ingame camera and whenever I change the text definitions, a new text is created instead of replace the previous one. Do this actually work on UPBGE?

import bge
import blf

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

resX = bge.render.getWindowWidth()
resY = bge.render.getWindowHeight()

def BLFText():
    FONT_ID=0
    blf.color(FONT_ID, 0.3, 1.0, 0.3, 1)
    TEXT = "Text"
    blf.size(FONT_ID, int(resX/20), 72)
    blf_dimensions = blf.dimensions(FONT_ID,TEXT)
    blf.position(FONT_ID, (resX/2-blf_dimensions[0]/2),(resY/2-blf_dimensions[1]/2), 0)
    blf.draw(FONT_ID, TEXT) 
    
if 'init' not in own:
    own['init']=True
    scene.post_draw.append(BLFText)

Thanks, now the text is displaying. But i still got the same low quality/distortion problem as using textObject when i decrease the font size.

See the cut in letters…do you know how to solve this?

Just a few minutes ago I updated the code because I had forgotten the ‘init’=True switch and the so the vector text got added every frame :slight_smile: going blurrrrrry

happens when using standalone ? ensure those have same ratio or even same

image