0
$\begingroup$

So I am controlling a test object's text property from an external text file with this code

def text():
    import bge
    from bge import logic
    path = logic.expandPath('//')

    cont= logic.getCurrentController()
    scene= logic.getCurrentScene()
    own= cont.owner
    T= int(own['t']) 

    try:
        file = open(path+'text_file.txt', 'r')
        info = file.readlines()
        own.text = info[T]
    except:
        print('error')  

it works fine except for if I want line breaks so it doesn't stretch off the box i have it in. for example i have the veriable 't' set to 0 and the first line of the txt doc says "example text for stuff" but i want a line break between "text" and "for". If i was setting the normal text vaule and wanted a break there i'd have the code

own.text='example text\nfor stuff'

but if i type that in the txt file it just displays the line break command (\n). how do i make a line break command from inside the txt file be recognized by python? or is it even possible.

$\endgroup$
2
$\begingroup$

enter image description here

When the text file is read, no interpretation is made of what is written, is just text. For that reason yo sould write in the text file as: own.text = 'write it in \na new line' and then in your script use exec.

from bge import logic

def text():
    path = logic.expandPath('//')
    cont= logic.getCurrentController()
    scene= logic.getCurrentScene()
    own= cont.owner

    try:
        file = open(path+'text_file.txt', 'r')
        info = file.readlines()
        exec(info[0])

    except Exception as e:
        print('error', e)

Disclaimer

CAUTION

This is as dangerous as someone writing "wipeout this pc" and boom... your info is gone!.

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