Page 1 of 1

Using variables in dialog that need captialisation [solved]

Posted: Thu Apr 18, 2019 2:11 pm
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. :(

Re: Using variables in dialog that need captialisation

Posted: Thu Apr 18, 2019 3:51 pm
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...

Re: Using variables in dialog that need captialisation

Posted: Thu Apr 18, 2019 8:19 pm
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()

Re: Using variables in dialog that need captialisation

Posted: Fri Apr 19, 2019 9:21 am
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.

Re: Using variables in dialog that need captialisation

Posted: Fri Apr 19, 2019 12:36 pm
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

Re: Using variables in dialog that need captialisation

Posted: Fri Apr 19, 2019 6:59 pm
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...

Re: Using variables in dialog that need captialisation

Posted: Sun Apr 21, 2019 1:17 pm
by strayerror
Oooh, totally forgot about tags, that could work, thanks for the suggestions!