News Tutorials Games Downloads Links Contact

Featured Author:

Tutorials - Blender Game Engine

Allowing the User to Input Text

by blendenzo, with special thanks to Social and z3r0_d

Due to bugs with the Firefox Blender plugin, it is recommended that you view this tutorial using Internet Explorer with the Blender 2.25 web plugin.

Example files (zipped package): Not ready yet...

Version Info: Blend made with 2.42a. Compatible with all versions of the BGE.
  • Basic knowledge of the BGE interface
  • Ability to "plug in" a pre-written Python script
  • Ability to use bitmap text in the BGE

Basic Text Input

Allowing basic user input via the keyboard is relatively simple in the Blender Game Engine. The Keyboard Sensor provides a simple interface for recording keystrokes and storing them to a variable. Look at the image below:

Logic to record user keystrokes(img)

When the boolean property listed in "LogToggle" has a value of "True" the user's keystrokes are recorded to the "Target" property. In the example above, as long as prop is True, whatever the user types will be recorded to Text. Since Text is the display value of the bitmap text object, the user's input will be displayed directly to the screen. Social demonstrates this basic function in text_input.blend (126 kb). The plugin below shows the in-game results.

Firefox users: Due to an apparent bug with the Firefox plugin, you must navigate away from this page while the plugin is showing, then use your browser's back button to return here before the text will display properly. This applies to all plugins on this page.

Password Input

Text input can easily be used for password entry. I modified Social's .blend to test the value of Text against a pre-defined password. Download password_input.blend (130 kb) and take a look. In this example I've used three bitmap text objects. The first instructs the user to "Enter Password." The second text object receives and displays the user's input. The third will display "Password Correct" if the user enters the correct password. Take a look at the middle text object. In its logic you will find that I've added a property with the password value.

Properties of the user input object (img)

Now look at the Logic Bricks.

Logic to test the user input (img)

When the user presses the carriage return key (Enter), the value of Text is tested to see if it is the same as password. If it is, the message "Password Correct" is displayed. Notice that the expression controller "TextIsPass" is connected between two text objects. The plugin below demonstrates this setup in-game. Use the [Left Ctrl] key to confirm your password, as the plugin does not recognize the [Enter] key.

Masked Passwords

Generally the user prefers for passwords to be masked while they are inputing them. This way bystanders cannot see what is being typed. This can be achieved by sending the keyboard log to a property other than Text, then using a short Python script to output the correct number of mask characters to the Text property. Begin by adding an extra property to the text input object. We'll call it input, since it will be receiving the keyboard input.

Properties for masked password input(img)

Now go to the Blender Text Editor and make a new file named "". Here is the script (you can paste it into the Blender Text Editor using [Ctrl+Shift+V]):

cont = GameLogic.getCurrentController()
own = cont.getOwner()

mask = ""
for x in range(len(own.input)):
	mask = mask + "#"
own.Text = mask

Logic for masked password input(img)


Back to the tutorials index...

Website design by Tony "blendenzo" DiRienzo. All content Copyright Tony DiRienzo unless otherwise noted.