Using variables in dialog that need captialisation [solved]

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
strayerror
Regular
Posts: 159
Joined: Fri Jan 04, 2019 3:44 pm
Contact:

Using variables in dialog that need captialisation [solved]

#1 Post by strayerror »

Code: Select all

eileen 'Hello [nickname]!"
player "Hi, Eileen!"
eileen "[nickname]! You're always so cheerful, what's you secret?"
In cases where nickname is a proper noun (Bob, for example) this works just fine, however sometimes nickname will not be a proper noun (stranger for a contrived example). In the second replacement I'd like to capitalise the improper noun because it's the start of a sentence, is there anyway to achieve this short of having to maintain two variables and remember to use one at the start of sentences and the other everywhere else? I just know it's going to get confusing with that approach. :(
Last edited by strayerror on Sun Apr 21, 2019 1:17 pm, edited 1 time in total.

rames44
Veteran
Posts: 233
Joined: Sun May 29, 2016 4:38 pm
Contact:

Re: Using variables in dialog that need captialisation

#2 Post by rames44 »

Well, Python strings have a capitalize() method that will convert the first character of a string to upper case.
https://www.geeksforgeeks.org/string-capitalize-python/

The trick is to use Pythonic "stuff" in say statements.

I came up with one VERY ugly way of doing it:

Code: Select all

define e = Character("Eileen")

label start:
    $ foo = "foo"
    $ e("{} gets capitalized".format(foo.capitalize()))
This takes advantage of the fact that having a character say something is equivalent to "calling" the character object with the item to be said. But, IMHO, this is SO ugly that two variables would be preferred. LOL

Of course, there may be a FAR simpler way of doing this that one of the local Ren'py/Python gurus can come up with...

User avatar
plaster
Regular
Posts: 89
Joined: Thu Jul 11, 2013 1:03 am
Tumblr: plasterbrain
Soundcloud: plasterbrain
Location: Chicago
Contact:

Re: Using variables in dialog that need captialisation

#3 Post by plaster »

Assuming you always want your sentences to start with a capital letter, you could get away with this:

Code: Select all

define config.say_menu_text_filter = capitalize_string

init python:
    def capitalize_string(what):
        return what.capitalize()

strayerror
Regular
Posts: 159
Joined: Fri Jan 04, 2019 3:44 pm
Contact:

Re: Using variables in dialog that need captialisation

#4 Post by strayerror »

Thanks both, unfortunately say_menu_text_filter is called prior to the substitutions, so that option doesn't quite work :(

I also realised that this issue is actually far more complex than first though as it really applies to the start of any sentence, even those that start in the middle of a line. Googling around it looks like sentence detection is far from "solved" and can differ significantly between languages, so I fear this isn't as solvable as I'd hoped. Might be that the best I can hope for is adding some sort of annotation [nickname!c] or something for when a capitalised version is desired, if indeed it's possible to add annotations.

rames44
Veteran
Posts: 233
Joined: Sun May 29, 2016 4:38 pm
Contact:

Re: Using variables in dialog that need captialisation

#5 Post by rames44 »

If you’re willing to add annotations, a custom text tag might work “{cap}[nickname]{/cap}”

https://www.renpy.org/doc/html/custom_text_tags.html

User avatar
plaster
Regular
Posts: 89
Joined: Thu Jul 11, 2013 1:03 am
Tumblr: plasterbrain
Soundcloud: plasterbrain
Location: Chicago
Contact:

Re: Using variables in dialog that need captialisation

#6 Post by plaster »

You're right, that is a lot more complicated than I'd assumed... Having a capitalized version of the string either thru text tags or a second variable is probably your best bet, then...

strayerror
Regular
Posts: 159
Joined: Fri Jan 04, 2019 3:44 pm
Contact:

Re: Using variables in dialog that need captialisation

#7 Post by strayerror »

Oooh, totally forgot about tags, that could work, thanks for the suggestions!

Post Reply

Who is online

Users browsing this forum: Google [Bot], Tomoyo Ichijouji