2
$\begingroup$

I recently made a short game using python that basically ran like this

def loop():
    I = input()
    if I = "help":
         print("hello, you called for help?")
         loop()
    else:
         print("unknown")
         loop()

It ran by making functions then running using them as loops and jumping between them as levels.
I wanted to make something similar in blender and have made an input field of sorts: so when I type text goes in the bottom, but I can't find a way to make them link up in anyway...

How would I make it so it worked like a python shell? because even if I do find a way to link them up it would start at the top of the script and wouldn't have any progress

$\endgroup$

1 Answer 1

1
$\begingroup$

Sample code

Your example is designed to run in a console. It does not fit the BGE in any form.

You need to understand that a Python controller is part of the BGE's game loop. Therefore you should never ever hold the processing in your Python code. You have to exit as fast as possible. Otherwise the game loop will not continue (Remember you have only 16ms for process everything incl. render, logic and physics).

Text Input

The usual way to enter text within the BGE is to use a keyboard sensor. Typical it reacts on a single keystroke. But you can switch it into log mode to log subsequent keystrokes into a string property.

enter image description here

When the log toggle property (here: enabled) is evaluated as True the sensor converts keypresses into characters and adds them to the target property (here: Text).

Be aware:

  • The BGE does not support Keyboard mapping. Therefore the converted key-codes match EN-Keyboards only. (Which means strings entered by other keyboards are incorrect, especially at the special character keys and the Z/Y keys).
  • The target property can be any property (it does not need to be "Text")
  • The sensor will not trigger connected controllers on key press unless you enable Key or All Keys.
  • You need to determine when an input is completed. You can do that either by sensing when the target property changed (property sensor) or when a key was pressed (All Keys enabled).
  • The Keyboard sensor only adds characters. You need separate logic to clear the target property (e.g. after completing the input)

Text output

To present the entered text to the user I suggest to use a Text object. Copy the target property value from the keyboard sensor to the text attribute (or Text property) of the text object and it will be displayed.

The above example logs directly to the Text property.

I hope it helps

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .