Text Mapping: Word Wrapping and file loading?

So I’d like to create a library of text…literally. I’d like my game to have a vast repository of lore and history available to read on demand, rather than bombarding my player with it in the main storyline, so I’d like to have lots of books. For the time being, they all use the same page texture and animation, which is fine, but writing the text on the pages has led me into a couple of problems.

*Is there a way to automatically word wrap the text, or will I need to write a script that automatically puts in
at the end of every line from my source text?

*Is there an easy way to call text by file to read it line by line? In Ruby, if I remember correctly, it was ridiculously easy, whereas in Java, it took a little more doing. Not sure how Python handles things.

What I have in mind would be like…

def loadText(filepath):
return doMagicLoadingTrickHere(filepath)

which would return a string or an array of lines or…it’s been a long time since my CS classes, I don’t know what data structure would be the most efficient way to do this. My guess is being able to dynamically load in/free lines of pre-formatted text would be the ideal, but based on that thread in the other forum, I’m not sure if Blender handles this yet (I may be misinterpreting things again).

If anyone has some insight, as always, I would greatly appreciate the help.

Ok, I was experimenting with something similar.
Its really easy in python,
read() reads the whole file and puts "
" at the end of every line.
But unless you want a lot of text files you still need some code to split it up into pages,
this thread might be useful: Dialog/Speech System
I made page test attached below similar to andrew-101’s but it loads a text file.

Simple file loading code

textFile = open("theText.txt", "r")

text = textFile.read()

print text


There’s also readlines() that puts every line of text into a list.

I just found out that Python has a textwrap module, that might be cool to play around with.
python doc link

import textwrap

textFile = open("theText.txt", "r")

text = textFile.read()
text = textwrap.fill(text,20)

print text


Here’s a couple of examples I made
theText.blend is really a txt file but you don’t have to rename it.

Hope that’s helpful, There’s a lot more but I’m not going to copy a chapter out of my python book. :slight_smile:

Edit: you have to have to first save theText.blend and then open the other files by clicking on them not through blender.


theText.blend (273 Bytes)Read_TextFile_test_008.blend (154 KB)Read_TextFile_withPages_012.blend (158 KB)

Ah, thanks so much! That should help a ton with handling the volumes of text I plan to put in.