Blender 2.5 Python Scripting in the Game Engine Entering Text at Run Time Part 2

preview_player
Показать описание
In part 2 of this video tutorial I show how to process the spacebar and backspace key presses. The spacebar is straight forward but the backspace delete requires string manipulation. The easiest way to do this is to use Python's built in string functions and methods.

The tutorial shows how to put all key press processing code into one Python script.

Key points -
You can set up a keyboard sensor to add a space character, the original 'all keys' code needs modifying so that it does not affect the string.
Logic bricks can be used to sense a backspace but a property actuator is not capable of deleting a character from a string.
The Python len() function returns the length of a string and can be used identify the last character.
The method rstrip() will right strip the character.
All keyboard processing code can be put into one Python script using a series of if statements.
Рекомендации по теме
Комментарии
Автор

@1ard I think I have seen the error message before and I think it is for leaving out quote eg. own['prpText'], prpText must be in quotes, single or double quotes -'prpText' or "prpText". I believe the fancy name is a string literal and it is important for any length including a single character and an empty string. Hope helps, Ian

ianscottBlenderTutorials
Автор

@Defuseninja001 I just went into game layout, in the text window I went to templates and select basic game. I changed from Blender render to Blender game and clicked the start button, I did not get an error on the import bge line or any other. I guess you must have something in front of the line, might be just a space?

ianscottBlenderTutorials
Автор

How do you make it so when you hit a certain key all the letters you just typed disappear?

Epicgamer
Автор

hey do you know how one can toggle the write-mode to false when you press the Enter-key(like normal textfields)? I have a plane behind the text and when the mouse is over and pressed, it changes its property "toggle" to true so that one can activate the writing with that. But when I press the Enter-key to toggle the planes property "toggle" to false so that you arent able to write into that string, nothing changes. Even the console is quiet.

The modified code i have put on the cam(Keyboard with AllKeys and TrueLevelT):
import bge

cont =
own = cont.owner
sensor = cont.sensors["KEYS"]

keyboard = bge.logic.keyboard.events
scene = bge.logic.getCurrentScene()

plane = scene.objects['plane']
textobject = scene.objects['Text']

if plane['toggle'] == True:
    for key, status in sensor.events:
# key[0] == bge.events.keycode, key[1] = status
        if status ==
            string = bge.events.EventToString(key)
        # all letters
            if string[1] == "K":
                plane['text'] += string[0]

            if string == "SPACEKEY":
                plane['text'] += " "
            # check when enter is pressed to quit write-mode
            if  bge.events.ENTERKEY:
                plane['text'] == False

            if string == "BACKSPACEKEY":
                lenString = len(plane['text'])
                oldString = plane['text']

                lastChar = oldString[lenString-1]
                plane['text'] = oldString.rstrip(lastChar)
 
        # all digits from num pad
            if string[0] == "P":
                plane['text'] += string[3]
       # place the string into a textobject
            textobject.text = plane['text']

Hope you understand the problem sorry i am greek :§

McMidas
Автор

um sorry to bother u but when i did the space bar i must have screwed up cuz before i did tht it typed just fine but now no keys will work and when i hit the space bar it says"[+[[[unexpected charecter''']]+[extra charecters"

ard
Автор

@ianscott888 well that fixed the error though i couldnt see the space so instead i used "_" instead of this " " and i could see the spacebar was working properly but when i hit any other key it doesn typ anythign :o iam 99.9% sure i typed my code exactly like yours but its hard to read it in the vid even in hd XD and for some reason it wont let me go full screne but anyway have any ideah what i could be doing wroung =]

ard
visit shbcf.ru