Page 1 of 1

Counting the length of a character name

Posted: Wed Apr 07, 2021 1:19 pm
by solarProtag
Hi there!

I've been trying to figure this out for a bit, but I keep coming out empty, but I want to count how long a character's name is so that I can use that to display appropriately-sized nameboxes without having to define a namebox image for each speaking character.

I've tried using len(who) but that only works for lists so it doesn't even run, and I've tried to convert the name string into a list but the engine hated that.

Thank you in advance!

Re: Counting the length of a character name

Posted: Wed Apr 07, 2021 2:14 pm
by IrinaLazareva
solarProtag wrote: Wed Apr 07, 2021 1:19 pmI've tried using len(who) but that only works for lists so it doesn't even run
Nope. This method works for string type of data.

Code: Select all

label start:
    'test'
    python:
        a = 'hello word'
        b = len(a)
    '[a] [b]'
    return
And structure in the screen say():

Code: Select all

    if len(who)<=....bla bla 
must be work.
I guess the problem is something else..

Re: Counting the length of a character name

Posted: Wed Apr 07, 2021 3:30 pm
by solarProtag
Yeah, for some reason no matter what I do the len() function refuses to count the "who" variable. It doesn't work for "what" either.

Re: Counting the length of a character name

Posted: Wed Apr 07, 2021 5:23 pm
by Alex
solarProtag wrote: Wed Apr 07, 2021 1:19 pm ... I want to count how long a character's name is so that I can use that to display appropriately-sized nameboxes without having to define a namebox image for each speaking character. ...
Try to use Frame() as a background of the namebox instead of an image, so it will stretch/shrink automatically.

https://www.renpy.org/doc/html/displayables.html#Frame

Re: Counting the length of a character name

Posted: Wed Apr 07, 2021 6:00 pm
by solarProtag
Alex wrote: Wed Apr 07, 2021 5:23 pm
solarProtag wrote: Wed Apr 07, 2021 1:19 pm ... I want to count how long a character's name is so that I can use that to display appropriately-sized nameboxes without having to define a namebox image for each speaking character. ...
Try to use Frame() as a background of the namebox instead of an image, so it will stretch/shrink automatically.

https://www.renpy.org/doc/html/displayables.html#Frame
The thing is, that stretching is exactly what I don't want, as my goal is to have one namebox for names shorter than a certain number of letters, and one for names longer.
I've tolerated the stretching in my past games since I didn't care, but this time I'd like to see if I can make that happen.

Re: Counting the length of a character name

Posted: Wed Apr 07, 2021 6:51 pm
by Alex
solarProtag wrote: Wed Apr 07, 2021 1:19 pm ...I've tried using len(who) but that only works for lists so it doesn't even run, and I've tried to convert the name string into a list but the engine hated that. ...
This code worked for me...

Code: Select all

screen say(who, what):
    style_prefix "say"

    window:
        id "window"

        if who is not None:
            $ x = len(who)

            window:
                id "namebox"
                style "namebox"
                text who + str(x) id "who"

        text what id "what"

Code: Select all

define e = Character("Eileen")
define x = Character("???")

label start:
    e "..."
    x "..."

Re: Counting the length of a character name

Posted: Wed Apr 07, 2021 7:05 pm
by solarProtag
That is so weird then!
Anytime I've tried something like:

Code: Select all

if who is not None:
    if len(who) <=5:
	$ nameshort = True
    else:
	$ nameshort = False

     window:
	id "namebox"
	style "namebox"
        text who id "who"
it won't update my variable. (and yes it's defined before this)