0
$\begingroup$

I want to display some text that change dynamically when I select another item. Displaying the text is working, but I'm wondering if there is a way to make the text multilines and/or wordwraping it so the text don't shows on only one line.

I saw some explainations on the web but these informations are over 8 years old.

It can be done by adding some "\n" in the string, but i would like to do it automatically like any textbox in most of UIs.

Can you guys help me on that issue please?

$\endgroup$
1
$\begingroup$

You can easily achieve it by using the Python standard library's textwrap module. I've written this article about text alignement in BGE some time ago, you may find it useful (it's in brazilian portuguese, you may try to use Google Translate on it). Here I will explain about left alignement, but in the article I explain about center and right alignement as well. Example file with all the modes provided below.

Left aligned text

With this simple setup:

enter image description here

The following code:

import bge
import textwrap

def main(cont):

    # Objects
    own = cont.owner

    # Sensors
    sensor = cont.sensors[0]

    # Properties
    text = 'The Blender Game Engine is a component of Blender, a free and open-source 3D production suite, used for making real-time interactive content. The game engine was written from scratch in C++ as a mostly independent component, and includes support for features such as Python scripting and OpenAL 3D sound.'

    #### INITIALIZE ####
    if sensor.positive:

        # Max number line characters before line breaks
        line_size = 35

        # Left alignement
        own.text = textwrap.fill(text, line_size)

It results in:

enter image description here

Example file

$\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.