interpolating in strings

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
Wap
Newbie
Posts: 12
Joined: Wed Jul 13, 2016 6:11 pm
Contact:

interpolating in strings

#1 Post by Wap »

Howdy. Is there a simple way to make something like this work:

Code: Select all

    $ pronounA = "he"
    $ pronounB = "his"

    $ saying = renpy.random.choice(["[pronounA] takes [pronounB] business elsewhere","unable to find the deal [pronounA] sought, [pronounA] leaves the store","[pronounB] interest wanes and [pronounA] leaves"])

    "And then the thing happens thusly: [saying]"

    $ pronounA = "she"
    $ pronounB = "her"

    "[saying]"
Right now this displays the pronouns like "bla bla [pronounA] yadda yadda". I've been dealing with this the long way with concatonation but it just occurred to me that I could be wasting time. Is there a point where I could insert a

Code: Select all

$ saying = renpy.someFunctionOrOther(saying)
Between the random chooser and the quote being read by the narrator and have this magically work? Please let me know.

User avatar
papiersam
Veteran
Posts: 231
Joined: Fri Aug 12, 2016 2:24 pm
Completed: Gem Hunt Beta, 1/Probably, Animunch
Projects: The Panda Who Dreamed
Contact:

Re: interpolating in strings

#2 Post by papiersam »

Funny, I didn't think that would behave that way when I saw it. Anyways;

Code: Select all

$ saying = renpy.someFunctionOrOther(saying)
There are python statements for that, easy. Something along the lines of using

Code: Select all

init python:
    import re
    
label start:
    
    $ saying = renpy.random.choice(["he takes his business elsewhere","unable to find the deal he sought, he leaves the store","his interest wanes and he leaves"])

    "And then the thing happens thusly: [saying]"


    python:
        #re.sub(r'\bhe\b', 'she', saying) <-- doesn't seem to be working...
        #re.sub(r'\bhis\b', 'her', saying)
        saying = saying.replace("he", "she")  #<-- this will replace 'the' with 'tshe'
        saying = saying.replace("his", "her")
            
    "[saying]"
But I haven't gotten it to work precisely correct (I guess the re library isn't included in usable Renpy/Python 2.7, but it's odd that it didn't bring up an error). Easiest way is to build your own python function (as I don't think Renpy has a specific built-in function like that) and have it return the right string, something like:

Code: Select all


init python:

    def she_insert(str):
        arr = str.split
        for i in range (0, len(arr)):
            if arr[i] == "his":
                arr[i] == "her"
            if arr[i] == "he":
                arr[i] == "she"
        
        return " ".join(arr)

Warning: code untested.

All in all, I hope that helped you out a bit, but I'm not quite certain I answered your question.

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: interpolating in strings

#3 Post by trooper6 »

Basically, [interpolation] is Ren'py code. Lines starting with $ are Python and not Ren'py, so you have to use Python Interpolation. Here is a page on it: https://pyformat.info
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
papiersam
Veteran
Posts: 231
Joined: Fri Aug 12, 2016 2:24 pm
Completed: Gem Hunt Beta, 1/Probably, Animunch
Projects: The Panda Who Dreamed
Contact:

Re: interpolating in strings

#4 Post by papiersam »

Lines starting with $ are Python and not Ren'py
Is it...is it not the other way around? Have I been wrong this whole time?

Code: Select all

 %(var)s 
is pythonic, and

Code: Select all

 [var] 
is renpyic (for this example, at least), right?


(Also, I tried

Code: Select all

$ saying = renpy.random.choice(["%(pronounA)s takes %(pronounB)s business elsewhere","..."])
but it had the same effect, printing excatly "%(pronounA)s takes %(pronounB)s business elsewhere").

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: interpolating in strings

#5 Post by trooper6 »

r_sami wrote:
Lines starting with $ are Python and not Ren'py
Is it...is it not the other way around? Have I been wrong this whole time?
Nope. $ is Python, without is Renpy.
r_sami wrote:

Code: Select all

 %(var)s 
is pythonic, and

Code: Select all

 [var] 
is renpyic (for this example, at least), right?
Yep, but I prefer the newer .format code over the older % code for string interpolation whenever I have the choice.

So in a general sense there are lots of ways to deal with switching pronouns. If I were doing this just with Renpy, and not dealing with the random phrase element, let's say dealing with the pronouns of the main character? I'd do something like this:

Code: Select all

default he = "he"
default him = "him"
default his = "his"
default He = "He"
default Him = "Him"
default His = "His"

# The game starts here.
label start:
    "Game starts"
    menu:
        "Is the main character male, female, or gender neutral?"
        "Male":
            pass
        "Female":
            $ he = "she"
            $ him = "her"
            $ his = "hers"
            $ He = "She"
            $ Him = "Her"
            $ His = "Hers"
        "Neutral":
            $ he = "ze"
            $ him = "hir"
            $ his = "hir"
            $ He = "Ze"
            $ Him = "Hir"
            $ His = "Hir"
    "Thank you."
    "When the story starts, Alex is just waking up. [He] rubs [his] eyes and looks around [him]self. What will [he] do today?"
    return
    
I think sort sort of thing is clever. But that isn't what the OP asked for. The OP wants a random phrase chosen of three and wants pronoun interpolation.

You can do it like this:

Code: Select all

default pronouns = {"male":["he", "him", "his"], "female": ["she", "her", "hers"], "neutral": ["ze", "hir", "hirs"]} #your dictionary of pronouns
default gender = "female" #what your default gender is

# The game starts here.
label start:
    "Game starts"

    $ saying = renpy.random.choice(["{p[0]} takes {p[1]} business elsewhere".format(p=pronouns[gender]),"unable to find the deal {p[0]} sought, {p[0]} leaves the store".format(p=pronouns[gender]),"{p[1]} interest wanes and {p[0]} leaves".format(p=pronouns[gender])]) #This is using the .format code for interpolation

    "With Female"
    "[saying]"
    "This works"
    
    "With male"
    $gender = "male"
    "[saying]"
    "This won't work, it will still use female pronouns!"

    return
So the code I posted works, but it will always use the first set of pronouns you give it even if you change the gender later, because you only set the saying phrase once. So I recommend creating a function that returns a newly formatted random phrase every time you call it like so:

Code: Select all

default pronouns = {"male":["he", "him", "his"], "female": ["she", "her", "hers"], "neutral": ["ze", "hir", "hirs"]}
default gender = "female"

init python:
    def leave(g):
        global gender
        gender = g
        return renpy.random.choice(["{p[0]} takes {p[1]} business elsewhere".format(p=pronouns[gender]),"unable to find the deal {p[0]} sought, {p[0]} leaves the store".format(p=pronouns[gender]),"{p[1]} interest wanes and {p[0]} leaves".format(p=pronouns[gender])])

# The game starts here.
label start:
    "Game starts"

    "With Female"
    $saying = leave("female")
    "[saying]"
    
    "With male"
    $saying = leave("male")
    "[saying]"

    "With plural"
    $saying = leave("plural")
    "[saying]"
    return
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

Wap
Newbie
Posts: 12
Joined: Wed Jul 13, 2016 6:11 pm
Contact:

Re: interpolating in strings

#6 Post by Wap »

Thanks guys. Not quite the simple solution I expected, but I learned some things and you gave me an idea of something unrelated. So, merci beaucoups.

Post Reply

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot]