Hover Text Tutorial


Give player information about the object that is being hovered over.


Game Font
Child By Name
Json Coder
Oop in Bge

Set Up

Create a directory witch contains hoverText.blend, hoverText.py and jsonCoder.py. The jsonCoder.py is obtained in the Json Coder tutorial. The hoverText.py contains the code described here.

In hoverText.blend, make a cube, a cone and a sphere. All three should have the following game properties.
hover boolean True
str int some number
agl int some number
int int some number
Hover tells the detector that the object should display a hover text. The rest are just the information to be displayed.

Create an empty with a mouse over any sensor called MouseOverAny. Turn Pulse on and set Property to hover. Connect this to a module controller that runs hoverText.over.
MouseOverAny -> hoverText.over

Select any object and have that object add a new overlay screen called Hud.
Always -> and -> Scene

In the Hud scene create a plane called HoverText. Create a text object called HoverText Text and parent it to HoverText. Change HoverText Text's font to a Monospaced one. HoverText should have the following property.
collum int some number
Collum defines the maximum number of characters a line can have. HoverText should also have the following nodes.
Always -> hoverText.sub
Message -> hoverText.message
Message should have the subject hoverTarget.


import bge
import jsonCoder as json
# This gets run by the object that detects which object is being hovered over.
def over(cont):
  # Get the hit object.
  target = cont.owner.sensors['MouseOverAny'].hitObject
  # Send the hit object as a message.
  # Sending an object through messaging requires a custom json encoder.
  # http://upbge.wikidot.com/json-coder-tutorial
  bge.logic.sendMessage('hoverTarget', json.dumps(target))
class HoverText(bge.types.KX_GameObject):
  def __init__(self, DNU):
    self.text = self.child('Text')
    # Clear out the text at start.
    self.text.text = ''
    self.collum = self['collum']
  def message(self, subject, body):
    # If theres no hover target leave everything blank.
    if not body:  self.text.text = '';  return
    # Make sure the name isn't too long.
    # This truncates the name at self.collum characters.
    # Monospaced fonts are very important in games!
    name = str(body.name)[:self.collum]
    # "My stats are so huge they break the frame?!!??!?!"
    # Be carefull not to override str.
    sTr = str(body['str'])
    agl = str(body['agl'])
    int = str(body['int'])
    # This is how blender recognizes that there should be a new line.
    l = '\n'
    # This is the same as, msg = name +l+l+ 'Str : ' + sTr +l+l+ 'Agl : ' + agl +l+l+ 'Int : ' + int
    # Using parentheses allows us to spread the command across multiple lines.  I find it easer to read but its up to you.
    msg = (
    name + l+
    'Str : ' + sTr + l+
    'Agl : ' + agl + l+
    'Int : ' + int
    # Set the message.
    self.text.text = msg
# http://upbge.wikidot.com/oop-in-bge-tutorial
# The rest of this is boiler plate.
# You copy it verbatim.
# It might be convenient to place some of this into a util script and import it.  Your call.
  # 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 = HoverText
SEN_MESSAGE = 'Message'
def sub(cont):
  self = cont.owner
  if isinstance(self, BASE_CLASS):  return self
  else:  return BASE_CLASS(self)
def message(cont):
  sen = cont.sensors[SEN_MESSAGE]
  for i in range(sen.frameMessageCount):
    subject = sen.subjects[i]
    body = sen.bodies[i]
    body = JSON_CODER.loads(body)
    cont.owner.message(subject, body)


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

Further Reading

Thumbnail Tutorial

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