Text Input Tutorial

Abstract

Receive text input and send it as a message. Usefull for things like names.

Prerequisites

Game Font
Oop in Bge
Json Loads
Child by Name

Setup

Create a plane for the background. Call it TextInput. Give it a material. Enable Shadeless, Object Color, and Transparency. Set its Object Color, giving it partial alpha. Give the background the shown nodes and properties. Create a Text object. Name it TextInput Text. Parent it to the background object. Give it the same material. Set its Object Color but give it full alpha.

text-input-tutorial-img0.png
text-input-tutorial-img1.png

Code

import bge
# https://docs.python.org/3/library/json.html
import json
 
class TextInput(bge.types.KX_GameObject):
 
  def __init__(self, DNU):
    self.text = self.child('Text')
    self.text.localScale *= self['scaleFix']
    # Apparently self.color returns a vector but will not accept one.
    self.baseColor = self.color.x, self.color.y, self.color.z, self.color.w
    # This is the color the bar will be when typing.
    # json converts the string to a list.
    self.activeColor = json.loads(self['activeColor'])
    self.collum = self['collum']
 
  def input(self):
    mOver = self.sensors['MouseOver'].positive
    mLeft = self.sensors['MouseLeft'].positive
    # Returns True is mOver and mLeft are positive, False otherwise.
    mClick = mOver and mLeft
    kEnter = self.sensors['KeyEnter'].positive
    kAny = self.sensors['KeyAny'].positive
 
    if mClick or kEnter:
      # If logging is on, turn it off.
      if self.logOn:  self.logOn = False
      # If logging is off turn it on.
      else:  self.logOn = True
      # If logging is toggled, end the script immediately.
      return
 
    if self.logOn:
      if kAny:
        # If the string fits in the box, then set the text to it.
        if len(self['_logText']) < self.collum:
          # The underscore is meant to indicate that this property is not meant to be modified by the user.
          self.text.text = self['_logText']
        else:
          # Get the last eight characters, then reverse them.
          self.text.text = self['_logText'][:-self.collum-1:-1][::-1]
 
  @property
  def logOn(self):  return self['_logTogle']
  @logOn.setter
  def logOn(self, isOn):
    if isOn:
      # Change the color of the bar.
      self.color = self.activeColor
      # Clear the text.
      self['_logText'] = ''
      self.text.text = ''
      # Turn on logging.
      self['_logTogle'] = True
    else:
      # Reset the bars color.
      self.color = self.baseColor
      # Json assumes characters will be contained in quotes,
      # We also need to remove the newline added when enter was pressed.
      msg = '"'+self['_logText'][:-1]+'"'
      # Send the submitted text as a message.
      bge.logic.sendMessage(self['msgSubject'], msg)
      # Turn off logging.
      self['_logTogle'] = False
 
  # http://upbge.wikidot.com/child-by-name-tutorial
  def child(obj, child):
    name = obj.name.split('.')
    try:    suffix = '.'+name[1]
    except IndexError: suffix = ''
    return obj.childrenRecursive[name[0] +' '+child+ suffix]
 
# http://upbge.wikidot.com/oop-in-bge-tutorial
BASE_CLASS = TextInput
 
def sub(cont):
  self = cont.owner
  if isinstance(self, BASE_CLASS):  return self
  else:  return BASE_CLASS(self)
 
def input(cont):  cont.owner.input()

Questions

Log in and edit this post to ask or answer questions.

Further Reading

Text Console
Json
Wrap

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License