Dialogue and Diction Filter

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
Kinsman
Regular
Posts: 130
Joined: Sun Jul 26, 2009 7:07 pm
Location: Fredericton, NB, Canada
Contact:

Dialogue and Diction Filter

#1 Post by Kinsman »

If you're writing a game that has events that can be experienced by different characters, writing each character's take on the situation and organizing the logic for all of them can be a pain. This bit of code is designed to make that easier.

Code: Select all

"You've arrived at the restaurant. It's very busy tonight, but you see a free spot where you and your date can sit down."

date.carol "Wow, my brother's restaurant has done a lot better than I expected! Look how busy it is!"
date.alien "So, this is the place you humans refer to as a 'restaurant'. How interesting."
date "It's really busy here, isn't it? It must be a good place."

In this example, the variable 'date' was set to any of a number of different characters.

Rather than say each line in sequence, Ren'Py will approach these lines as a set. The first few lines are specific, to either a character, or a type of character. If the speaker matches the conditions, then that line will be spoken, and not the others.

The last line in the set should always be a generic one, the line that 'anyone else' would say. Whether the line is said depends on if none of the other conditions were true for the speaker.

The last generic line also lets Ren'Py know that the set is over, and it should go back to reading lines normally until it meets the next set:

Code: Select all


"You've arrived at the restaurant. It's very busy tonight, but you see a free spot where you and your date can sit down."

date.carol "Wow, my brother's restaurant has done a lot better than I expected! Look how busy it is!"
date.alien "So, this is the place you humans refer to as a 'restaurant'. How interesting. And how crowded."
date "It's really busy here, isn't it? It must be a good place."

player "Hopefully we won't have to wait too long to order. What do you want to get?"

date.alien "I don't have the slightest idea of what any of these foods even are."
date "Hmm.. it's so hard to decide."

player "Well, I heard the fish was good."

Basic Use

The first thing you should do to use this system is to create a set of FilteredCharacter objects. FilteredCharacter objects are just like regular Character objects, except that

* You'll need to make them FilteredCharacter's so that the dialogue system recognizes them.
* Along with the usual keyword arguments, you can add extra keywords that give basic information about the character, such as gender, age, or personality.

Code: Select all


init python:
    
    carol = FilteredCharacter("Carol",color="#ff0",gender="f",age=25,alien=False)
    nooko = FilteredCharacter("Nooko",color="#0f0",gender="f",age=225,alien=True)
    gorbo = FilteredCharacter("Gorbo",color="0a0",gender="f",age=171,alien=True)
    alice = FilteredCharacter("Alice",color="#aaf",gender="f",age=21,alien=False)

    player = FilteredCharacter("Player",color="#fff",gender="m",age=22,alien=False)

After you've set this up, you can do two things right away:

* You can refer to the variable names of the characters, such as 'carol' or 'nooko'. Ren'Py will realize that this is a line meant for that character only, and will only say it if the current speaker is exactly that character.

* If any of your fields set up in FilteredCharacter are booleans, you can use the name of the field as a test to see if it is True for the speaker. For instance, Nooko and Gorbo will speak lines that you've marked as 'alien'.

Custom Filters

If you need more flexibility, you can set up additional filters using the 'diction' object:

Code: Select all


diction.add_filter("human","speaker.alien == False")
diction.add_filter("girl","speaker.gender == 'f'")
diction.add_filter("boy","speaker.gender == 'm'")
diction.add_filter("drunk","speaker.beers >= 3")

'diction' holds the rules you create, and hands them off to all the FilteredCharacter's in your game.

The function 'add_filter' takes two arguments; the name of the filter (which you can then use for marking your dialogue), and the condition that the filter has to meet.

The condition is written in the same way that ConditionSwitch conditions are written, i.e. a basic Python statement. Note the use of the word 'speaker', which is a special variable that holds whoever is planning to speak.

If Tests

You can also use the filters, either the automatic ones or the ones you've created, in if-tests in your script.

Code: Select all


date.drunk "Ish.. ish's been greeeeat. I loooove you.."
date "Well, this has been a fine time. It was really nice getting to know you."

if date.carol:
    jump meet_carol_brother

if date.alien:
    jump sudden_monster_attack


Notes

* The code uses 'diction' and 'speaker' as reserved global variables, so don't use those names for your own purposes.
* Don't set a named variable in FilteredCharacter and then use that same name to title a filter - the filters are created as variables themselves, and you would just overwrite that name on the character. object.
Attachments
dialoguefilter.rpy
(5.23 KiB) Downloaded 172 times
Flash To Ren'Py Exporter
See the Cookbook thread

User avatar
sapiboonggames
Veteran
Posts: 299
Joined: Thu Jan 05, 2012 8:53 am
Completed: I Love You, Brother [BxB]
Contact:

Re: Dialogue and Diction Filter

#2 Post by sapiboonggames »

This is neat, thanks for sharing :)
Visit my website: http://www.sapiboong.com

User avatar
sapiboonggames
Veteran
Posts: 299
Joined: Thu Jan 05, 2012 8:53 am
Completed: I Love You, Brother [BxB]
Contact:

Re: Dialogue and Diction Filter

#3 Post by sapiboonggames »

Sorry if I seem noob, but how to use this? :oops:
Tried to copy your codes above, but it says that name "date" is not defined.
How to define the "date" variable?
Visit my website: http://www.sapiboong.com

Kinsman
Regular
Posts: 130
Joined: Sun Jul 26, 2009 7:07 pm
Location: Fredericton, NB, Canada
Contact:

Re: Dialogue and Diction Filter

#4 Post by Kinsman »

Oh, don't worry about the "date" variable. That was just an example for the script.

A bit of code like this might have initially set it:

Code: Select all

menu:
    "Who are you dating tonight?"
    "Alice":
        $date = alice
    "Carol":
        $date = carol
    "Nooko":
        $date = nooko
Flash To Ren'Py Exporter
See the Cookbook thread

User avatar
sapiboonggames
Veteran
Posts: 299
Joined: Thu Jan 05, 2012 8:53 am
Completed: I Love You, Brother [BxB]
Contact:

Re: Dialogue and Diction Filter

#5 Post by sapiboonggames »

Sorry but I'm back here with yet another question. I really want to use this as it is so neat and makes everything easier :mrgreen:

I use this code:

Code: Select all

init python:
    clarence = FilteredCharacter("Clarence",color="#0f0", gender="m",rp=clarenceRP)
    diction.add_filter("inlove","speaker.rp >= 50")

Code: Select all

if date.inlove:
        date "Hey, we aren't done here yet, are we?"
        menu:
            "Go to hotel":
                date "That's what I've been waiting for."
                call nightdate
            "Go to bar":
                date "Sounds nice."
And get "invalid syntax" error.

I think the cause is (rp=clarenceRP) part. ClarenceRP is a variable that store Clarence's relationship point. I think I'm writing the code wrong.
Visit my website: http://www.sapiboong.com

Kinsman
Regular
Posts: 130
Joined: Sun Jul 26, 2009 7:07 pm
Location: Fredericton, NB, Canada
Contact:

Re: Dialogue and Diction Filter

#6 Post by Kinsman »

Set the field like this:

Code: Select all

    clarence = FilteredCharacter("Clarence",color="#0f0", gender="m",rp=0)
and then add or subtract points to "clarence.rp".

If you do that, you also won't need a clarenceRP variable.
Flash To Ren'Py Exporter
See the Cookbook thread

User avatar
sapiboonggames
Veteran
Posts: 299
Joined: Thu Jan 05, 2012 8:53 am
Completed: I Love You, Brother [BxB]
Contact:

Re: Dialogue and Diction Filter

#7 Post by sapiboonggames »

Kinsman wrote:Set the field like this:

Code: Select all

    clarence = FilteredCharacter("Clarence",color="#0f0", gender="m",rp=0)
and then add or subtract points to "clarence.rp".

If you do that, you also won't need a clarenceRP variable.
Thanks, that works!
Visit my website: http://www.sapiboong.com

User avatar
soggybottoms
Regular
Posts: 35
Joined: Fri May 09, 2014 12:52 am
Contact:

Re: Dialogue and Diction Filter

#8 Post by soggybottoms »

Hi hi, I'm brand new here and super new to coding in general. I'm working on a way too ambitious child raising simulator and this script you've created looks like it could save me so much time!! But I'm not sure where to begin as far as implementing it. I'm really hoping you may not mind being patient with a total noob and walking me through it. n_n

First, I added the entire code from the downloadable file into an init section I designated under the "label start" in my main script file (which I'm planning to use only for init purposes before hopping to a different rpy file for the actual playable game start, no idea if that's even a good idea).

Anyway. So the player is "parent" and they want to adopt a kid. One way to do so will involve meeting with multiple "birth moms" in interview fashion to pick whose baby you will end up adopting, so right away your script would be useful! I'm just not sure where to start. Do I go to the default "# Declare characters used by this game." section and, instead of adding like a normal character, put something like
"init python:
bm1 = FilteredCharacter("Lucy",age=23,temperament=relaxed)
bm2 = FilteredCharacter("Annie",age=19,temperament=difficult)"?

That's just for example. I have no idea how I'm going to code in what "temperaments" are, but my goal is to have the birthmom's temp reflect what innate temp the baby will have. But that's another topic entirely. n_n;


Then once the characters are set, is that it? And I can refer to them in the way you've posted your examples above?

I know this is a lot! Thank you so much for reading.

Post Reply

Who is online

Users browsing this forum: No registered users