Ren'Py Help with setting or testing minimum input variable length [SOLVED]

moose_puck

Active Member
Sep 6, 2021
740
1,678
I'm trying to include a simple script to allow the player to set the age of a character and I want to make it so they can only set it for 18 or higher.

I've used the code below and it works fine, up to a point. Any number between 10 and 17 will default to 18. A single 1 will also be returned as 18. Where it doesn't work is when a player inputs a single digit age between 2 and 9. Those slip through unchanged.

I've looked around and most of the similar examples I have seen are from Ren'py versions more then 4 years old.

Any suggestions that can fix this? Either make it properly parse ages "2-9" or force the player to input a minimum of 2 characters? I seem to recall Java had a min and maxlength function, but all i can find in Ren'py is the basic length one.

Python:
    $ mca = renpy.input("How old are you again?", default=18, length=2, allow="0123456789")

    $ mca = mca.strip()

    if mca == "":
        $ mca="18"

    if mca < "18":
        $ mca = "18"
 
Last edited:

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,979
16,236
I'm trying to include a simple script to allow the player to set the age of a character and I want to make it so they can only set it for 18 or higher.
Code:
    if mca < "18":
        $ mca = "18"
A string can not be inferior, nor superior, to another string. What is tested here is "have the string stored in 'mca' less character than 2".

You need to firstly turn the string into a number. Then you'll be able to manipulate it and force a value of 18 if needed:
Code:
    $ mca = renpy.input [...]
    $ mca = str( max( 18, int( mca ) ) )
It will keep the maximal value between "18" and the value entered by the player, then turn it back into a string for easier manipulation in the game.
 
  • Like
Reactions: moose_puck

moose_puck

Active Member
Sep 6, 2021
740
1,678
A string can not be inferior, nor superior, to another string. What is tested here is "have the string stored in 'mca' less character than 2".

You need to firstly turn the string into a number. Then you'll be able to manipulate it and force a value of 18 if needed:
Code:
    $ mca = renpy.input [...]
    $ mca = str( max( 18, int( mca ) ) )
It will keep the maximal value between "18" and the value entered by the player, then turn it back into a string for easier manipulation in the game.

That worked like a charm. So much more concise too. You're the boss!