[Solved] How do I set a limit to characters in a text object

Get help using Construct 2

Post » Mon Mar 06, 2017 2:43 pm

I have a text object to which I can add numbers. I don't want the player to be able to add an infitine amount of numbers obviously. I want the text to stop at 9 characters. How?
Last edited by Turaco on Tue Mar 07, 2017 5:44 am, edited 1 time in total.
B
10
S
4
Posts: 34
Reputation: 874

Post » Mon Mar 06, 2017 5:11 pm

Use the length len() expression in the system object to get the length of the text and if its less then 9 allow more characters to be added.

compare 2 values
len(textobject) < 9 add number
B
43
S
23
G
20
Posts: 735
Reputation: 12,027

Post » Tue Mar 07, 2017 5:44 am

Ethan wrote:Use the length len() expression in the system object to get the length of the text and if its less then 9 allow more characters to be added.

compare 2 values
len(textobject) < 9 add number



Thanks, worked perfectly :)
B
10
S
4
Posts: 34
Reputation: 874

Post » Tue Mar 07, 2017 7:59 am

Ethan wrote:compare 2 values
len(textobject) < 9 add number

The only flaw of that logic is that the user can use Ctrl+V to enter infinite amount of numbers.
Better to use (Usefull not only for numbers):
*TextBox: On Changed
->TextBox: Set text to left(self.Text,9)

If you want only positive numbers you can use:
*TextBox: On Changed
->TextBox: Set text to str(clamp(float(Self.Text), 0, 999999999))

If you want positive whole numbers:
*TextBox: On Changed
->TextBox: Set text to str(clamp(int(Self.Text), 0, 999999999))
B
61
S
31
G
6
Posts: 125
Reputation: 7,895


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 51 guests