dialog - one letter at a time

Hey guys, I’m just here doing dialog for my game and I want to feature the dialog wherein it shows one letter at a time, and it is very popular with every game out there.
The things in my scene is the built-in text which has a property of the Text Game Property which is “Text”. and a game property that’s called “page”. Anyone knows how to print the Text one letter at a time?
Here’s my script. Just a simple one.

from bge import logic

cont = logic.getCurrentController()

own = cont.owner

if own[‘page’] == 0:
own[‘Text’] = “Hi”

you replace the string property with a new value. After a delay you add another letter to it.


PtargetText = "targetText"
Pdone = "done"
EMPTY = ""

def addOneLetter(cont):
  textObject = cont.owner

  targetText = textObject[PtargetText]
  currentLength = len(textObject.text)
  if currentLenth >= len(targetText):
    textObject[Pdone] = True

  textObject[Pdone] = False

  textObject.text = targetText[:currentLength+1]

def clearText(cont):
   textObject = cont.owner
   textObject.text = EMPTY


*You call slowtext.clear each time the property “targetText” changes to clear the current text
*You call slowtext.addOneLetter every ??? frames you can use a property sensor with true pulse f:??? evaluating “targetText” is not equal “”.
*You can check property “done” to be True after the text is completely visible.

How to fill “targetText” with the text of your choice is your quest ;).

I am not sure if the method I will present is the best one, but you could put the a full target text copy into a string variable, do another ‘cur_text’ variable, and make a timer. As that timer is activated and goes, you would pass one letter from the target_text variable to the ‘cur_text’ variable at the speed you want. After the target_string is empthy, the process would stop.

It would be best to get all the text from a ‘eng_lang’ file you create, so you could easily localize(‘translate’) your game to other languages.

you could easily make an always sensor that pulses the controller, and when it is high, add another letter

dialogue].blend (575 KB)

@Monster I can actually understand your script for a change. LOL. I’m kinda a noob to python so please bear with me.
The code:

targetText = textObject[PtargetText]
currentLength = len(textObject.text)
if currentLenth >= len(targetText):
textObject[Pdone] = True

targetText is the string I would use right? and if the currentLength is greater than the words not written it adds a letter which makes textObject[Pdone] true. Sorry for being a noob. :expressionless:
@agoose77 That seems to work perfectly! But where would I put my strings? Kinda like my VERY simple code where
if own[‘page’] == 1:
own[‘Text’] = “Insert dialog here”.
Where can I put this?

In my example, change the variable “words” which is created with text on the object if it doesn’t exist, otherwise it uses your text.
It will output your words to the variable “Text” on the object
Every time the words are changed, the object will delete the old message and start typing the new one. If you set the variable words to nothing ("" in the property actuator) it will clear the words.

I get it now. But how do I make my strings on the script? without using the property actuator?
Sorry. Just found out how. LOL. Sorry if such a noob. Thank you so much.