Limit text input

I’ve got several text inputs via the keyboard, some are Logic bricks and some are python. I’ve got them setup as click boxes. So you click the box, input becomes active, you hit enter or click off the box and it becomes inactive. I’ve pretty much figured out how to do everything I need a text object to do but

HOW IN THE WORLD DO YOU LIMIT THE INPUT CHARACTERS?

I’ve literally spent a month googling etc and I can’t find anything that will just let me limit the key input to say 5 characters so it doesn’t just type on off the end of the screen. I don’t need word wrap, that seems to come up a lot in my searches. I just need to limit the number of characters that someone can input.

Any help would be awesome.

thanks

The best way is to act on button press.

When a button gets pressed you trigger a check for input size. If the input is within the range, you do not need to do any further … it is fine.

When hitting the limit there are two options:

A) (pro-active) prevent any input that increases the text any further.
B) (post processing) you remove any character that exceeds the text limit.

In python:


textbox.text = texbox.text[:10]

Will mean that anything beyond ten characters is discarded.

1 Like

Thanks for the replies, I really do appreciate it. I’m still pretty new at this stuff, but I think Ive learned a lot in the past couple months. Either of you know how I could set up logic bricks to accomplish this?

Monster, I’m not sure I follow… I mean I understand what you are saying, but I don’t understand the how.

sdfgeoff, how would I incorporate that, I’m not that good with python, I’m just really good at following tutorials and tweaking it to fit my needs… how would I incorporate that in to this script?

import bge
from bge import logic, events

def main():

cont = bge.logic.getCurrentController()
own = cont.owner


keyEvents = logic.keyboard.events

hitKey = [k for k in keyEvents \
                if keyEvents[k] == logic.KX_INPUT_JUST_ACTIVATED]
                
input = own ["userinput"]

if hitKey != []:
    print(hitKey)   
    characterValue = bge.events.EventToCharacter(hitKey[0], True)
    print(characterValue)   
    own ["userinput"] += characterValue
      
    
own.text = own ["userinput"]         

main()

You can disable the text input when reaching the limit:


textchecker.py:


def activateOnMax(controller):
    owner = controller.owner
    text = owner.text
    max = owner["max"]
    if len(text) >= max:
        for actuator in controller.actuators:
            controller.activate(actuator)

module: textchecker.activateOnMax

What it does:

  • when the text exceeds the limit the logging gets disabled

Advantage: the text will never exceed
Disadvantage: after reaching the limit, editing is not possible.

Other method:

  • adjust later + copy value


textchecker.py:


def adjustInput(controller):
    owner = controller.owner
    input = owner["input"]
    max = owner["max"]
    if len(input) > max:
        owner["input"] = input[:max]

Module: textchecker.adjustInput

What it does:

A) at the scene load it copies property “input” to property “Text” (otherwise you would see “Text” until you press a button)
B) at key press the python module checks the content of “input” if longer than “max” it will be truncated
C) at key press it copies property “input” to property “Text”

Why copy?
If you set “Text” directly you would see characters exceeding the limit, resulting in a flickering.

Benefit:

  • the string can still be edited even when reaching max.
  • Text never exceeds even when input exceeds for a short time.
1 Like

Works like a friggin Charm Monster, I had to tweak one of my scripts to clear the input to nothing after hitting enter so Multiple things could be inputted to the same text field… But, easy enough… So now I’ve got a text box with a max of 3 characters that you have to click on to activate and it clears after hitting enter then you can enter a new string, exactly what I needed. Your help on this forum to other people has helped me the most…