Slow Typing

Hi! :smiley: :smiley:

I’m just a newbie in panda and python, but I’ve been working on some code to display a text with a slow Typing Effect like so many games out there, is not as cool like other snippets here, but maybe is useful to someone. :slight_smile:

The program reads a file (named text.txt here), and stores it’s content in a dictionary (named script here), then the content of the dictionary is displayed in the screen; The “key” item of the dictionary is (in this case) showed as the name of the character, and the “value” is the text itself:

slowTyping.py -

from panda3d.core import TextNode
from direct.showbase.ShowBase import ShowBase
from direct.stdpy.file import *
from direct.task import Task
from direct.gui.OnscreenText import OnscreenText
import direct.directbase.DirectStart

import sys


class textTyping(ShowBase):

  def __init__(self):

    # Set some variables
    self.c = 0 # Variable to keep track of wich character we have to type
    self.x = 0 # Variable to keep track of which line we are in
    self.ready = True # Check if we are typing or not
    self.DT = 0.07 # Default typing speed

    self.script = {} # Dictionary to store the CharacterName (key) and the dialog to type (value)


    # Instructions
    self.instructions = OnscreenText(text = 'Press space to dispay the dialogs with a slow typing effect',
                                     pos = (0, 0.8),
                                     scale = 0.1,
                                     align = TextNode.ACenter,
                                     wordwrap = 15)

    self.instructions2 = OnscreenText(text = 'While typing, press space again to speed up the typing :D',
                                     pos = (0, 0.6),
                                     scale = 0.08,
                                     align = TextNode.ACenter,
                                     wordwrap = 13)


    # Setup a TextNode for CharacterName and Text

    self.charName = TextNode('Character Name')
    self.charName.ALeft
    self.charName.setText("Character Name")

    self.charNameNode = aspect2d.attachNewNode(self.charName)
    self.charNameNode.setScale(0.085)
    self.charNameNode.setPos(-0.9, 0, -0.4)



    self.text = TextNode('text')
    self.text.ALeft
    self.text.setWordwrap(25.0)
    self.text.setText("Press space to load the text!")

    self.textNodePath = aspect2d.attachNewNode(self.text)
    self.textNodePath.setScale(0.07)
    self.textNodePath.setPos(-0.9, 0, -0.5)



    # Open a text file for reading
    self.textFile = open("text.txt")
    
    for line in self.textFile:
      (key, value) = line.strip().split(">")   #Remove the "\n" strings from the lines, and then, split'em in two strings separeted by ">" 
      self.script[key] = value

    # Close the file --IMPORTANT--
    self.textFile.close()





    # Accept the "escape" key to close the program
    self.accept("escape", sys.exit)


    # Accept the "space" key to write the text
    self.accept("space", self.displayText, [self.charName ,self.text, self.script])





  def displayText(self, charNameNode, textNode, script):
    if self.ready:
      self.ready = False # Check if we are already typing the text
      charNameNode.setText(script.keys()[self.x])
      textNode.setText("")
      taskMgr.add(self.slowText, "slow motion text") # The task that will type the script character by character
    else:
      self.DT = 0.01 # Fast typing XD




  def slowText(self, task):
    if task.time < self.DT: # This sentece set the time between "typings"
      return task.cont
    if self.c < len(self.script.values()[self.x]): # Check if the and of line is reached
      self.text.appendText(self.script.values()[self.x][self.c]) # Type one character
      # Go to the next character in the line
      self.c += 1
      return task.again # Keep typing
    else:
      self.c = 0
      self.ready = True
      # Go to the next line in the file
      if self.x < (len(self.script)-1):
        self.x += 1
      else:
        self.x = 0
      self.DT = 0.07
      return task.done # Stop typing and remove the task from the Task Manager



textTyping = textTyping()
run()

You will need a text.txt file in the same directory containing the text to display, for instance:

text.txt -

Ancient Panda>Panda3D is the best game engine in the world!
Panda3D User>The community of Panda3D is the best community of developers in the world!

the format of the text in the file is the following: Every line must have a name (the name of the character), and a “speech”, separeted by a “>”, this is because the code I use to store the lines of a text in a dictionary work with this format.

I’ve took this way because it’s easy to edit the files containing the dialogues without digging into the code; so, if you, for example, want to translate the text to your native language, you just have to modify these files.

Hope you find this useful :smiley: :smiley: :smiley:

NOTE: My programming skills are not actually great, at all, so maybe you found some bad indentation or the like. I’m open to corrections!!!