Counting the length of a character name

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
solarProtag
Regular
Posts: 37
Joined: Sun May 28, 2017 12:17 am
Completed: http://solarprotag.weebly.com/gamedev.html
Projects: Battle Live Series
Tumblr: solarasketchbook
itch: solarprotagonist
Contact:

Counting the length of a character name

#1 Post 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!

User avatar
IrinaLazareva
Veteran
Posts: 399
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy
Organization: SunShI
Location: St.Petersburg, Russia
Contact:

Re: Counting the length of a character name

#2 Post 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..

User avatar
solarProtag
Regular
Posts: 37
Joined: Sun May 28, 2017 12:17 am
Completed: http://solarprotag.weebly.com/gamedev.html
Projects: Battle Live Series
Tumblr: solarasketchbook
itch: solarprotagonist
Contact:

Re: Counting the length of a character name

#3 Post 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.

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Counting the length of a character name

#4 Post 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

User avatar
solarProtag
Regular
Posts: 37
Joined: Sun May 28, 2017 12:17 am
Completed: http://solarprotag.weebly.com/gamedev.html
Projects: Battle Live Series
Tumblr: solarasketchbook
itch: solarprotagonist
Contact:

Re: Counting the length of a character name

#5 Post 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.

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Counting the length of a character name

#6 Post 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 "..."

User avatar
solarProtag
Regular
Posts: 37
Joined: Sun May 28, 2017 12:17 am
Completed: http://solarprotag.weebly.com/gamedev.html
Projects: Battle Live Series
Tumblr: solarasketchbook
itch: solarprotagonist
Contact:

Re: Counting the length of a character name

#7 Post 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)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], MisterPinetree