Basic Message System

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
User avatar
saguaro
Miko-Class Veteran
Posts: 560
Joined: Sun Feb 12, 2012 9:17 am
Completed: Locked-In, Sunrise, The Censor
Organization: Lucky Special Games
itch: saguarofoo
Location: USA
Contact:

Basic Message System

#1 Post by saguaro »

This is a basic in-game message system that uses screens and a little Python. It tracks the read status of messages and includes "mark all read", delete, and restore functions. It also has reply and draft features that use Ren'Py's built-in menus.

Updates:
05/20/14 v 1.2
- Added a simple draft feature that allows player to send messages to names on a contact list (same setup as replies)
- Incorporated message delay based on xavimat's contributions
- Refactored code and put updated choice screen in messages.rpy
screenshot0005.png
screenshot0006.png
Include the following after your start label:

Code: Select all

label start:
    $ mail = []
    $ mail_queue = [] # for message delay
    $ contacts = []  # for draft feature
The syntax for adding messages is add_message(subject line, sender name, message body). Include these anywhere in the script after the mail list is defined.

Code: Select all

    $ add_message("Welcome to Ren'Py!", "Eileen", "This is a test message.")
    $ add_message("Delayed message test", "Eileen", "This is a delayed message.", delay=True)
Delayed messages can be delivered like so:

Code: Select all

    $ deliver_next() 
    $ deliver_all()
Check to see if messages have been read based on the message title:

Code: Select all

    if check("TPS Reports"):
        e "Yep!"
    else:
        e "Nope!" 
If a message is reply-able, the syntax will be add_message(subject line, sender name, message body, reply_label).

Code: Select all

    $ add_message("I'm so mad!", "Fred", "You were mean to me.  You better apologize!", "fred_reply")
You will need to create the reply label. The template is:

Code: Select all

label fred_reply(current_message):
    menu:
        "I won't apologize, Fred. You smell like old socks and I hate you!":
            # whatever happens
            $ current_message.can_reply = False
        "You're right. I'm sorry, Fred.  Let's be friends.":
            # whatever happens
            $ current_message.can_reply = False
        "Don't reply yet.":
            pass
    return
The player can "draft messages" to people on a contact list. To add people to the list:

Code: Select all

    $ fredo = Contact("Fred", "fred_draft")

    # to change the draft message, do something like 
    $ fredo.draft = "fred_draft2"
    # if you don't want Fred on the list anymore (sorry Fred)
    $ fredo.delete() 
The template for drafts is:

Code: Select all

label fred_draft(contact, message_title="Listen, Fred..."):
    menu:
        "You're my very best friend! Let's get tacos!":
            $ contact.draft_label = None # must include this line for each option 
            $ add_message("Taco Time", "Fred", "Yay tacos!!!")   
        "I don't think we should be friends anymore.":
            $ contact.draft_label = None
            $ add_message(":(", "Fred", "WHYYYYYYYYYYYY")            
        "Discard draft.":
            pass
    return
Additional Features:
* Adding a sound effect when messages are received - by xavimat
* Adding time delay to replies - by xavimat

messages.rpy contains everything you need, including the modified choice screen. script.rpy has a demo. Drop all three scripts into a new project to test it out.
Attachments
messages.rpy
(8.3 KiB) Downloaded 1715 times
script.rpy
demo script
(4.3 KiB) Downloaded 1473 times
Last edited by saguaro on Tue May 20, 2014 6:43 pm, edited 4 times in total.

User avatar
saguaro
Miko-Class Veteran
Posts: 560
Joined: Sun Feb 12, 2012 9:17 am
Completed: Locked-In, Sunrise, The Censor
Organization: Lucky Special Games
itch: saguarofoo
Location: USA
Contact:

Re: Basic Message System

#2 Post by saguaro »

Update: Moved everything to the first post to avoid confusion.
Last edited by saguaro on Sat Apr 06, 2013 7:30 am, edited 1 time in total.

User avatar
Kitten the Cat
Regular
Posts: 60
Joined: Sun Jul 31, 2011 12:29 pm
Projects: The Onigami House [NaNo12], Beyond the Veil [NaNo14]
Organization: Riceball Games
Location: Sydney, Australia
Contact:

Re: Basic Message System

#3 Post by Kitten the Cat »

This is awesome, I have a workplace setting VN in mind and interacting using emails is perfect. Thank you for sharing!
Currently working on Beyond the Veil, our NaNoRenO '14 entry.
Image

User avatar
fioricca
Veteran
Posts: 333
Joined: Fri Feb 19, 2010 3:17 am
Completed: Rising Angels Reborn (2013), Rising Angels Fates: Allegiance (2017)
IRC Nick: souten
Contact:

Re: Basic Message System

#4 Post by fioricca »

Ahhh this is simply adorable! I love e-mail/messaging features. Thanks for sharing the code!

User avatar
Nuxill
Veteran
Posts: 464
Joined: Sat Sep 25, 2010 4:50 pm
Projects: No Friend
Tumblr: nuxill
itch: nuxill
Contact:

Re: Basic Message System

#5 Post by Nuxill »

Ah this is much cleaner looking and easier to understand than the old message system. :) Thanks!

User avatar
wyverngem
Miko-Class Veteran
Posts: 615
Joined: Mon Oct 03, 2011 7:27 pm
Completed: Simple as Snow, Lady Luck's Due,
Projects: Aether Skies, Of the Waterfall
Tumblr: casting-dreams
itch: castingdreams
Location: USA
Contact:

Re: Basic Message System

#6 Post by wyverngem »

Nice, but there is one bug. You can reply fine, but when you go back into your phone messages the Reply is still highlighted like you can click it. I tried to change it so that it's either reply or delete and it's always reply even after replying.

pwisaguacate
Veteran
Posts: 356
Joined: Mon Mar 11, 2013 11:03 pm
Contact:

Re: Basic Message System

#7 Post by pwisaguacate »

This is ingenious! There are VNs that include a mailbox system, but I don't remember one where the player directly replies back (and doesn't have to do so immediately)*. I imagine this being useful for setting flags. I shall check this out eventually.
*I haven't read many VNs yet. It sounds like Chaos;Head may have a reply feature built in but I can't find any screenshots.

Would anybody here like to look into the bug that wyverngem found? Attach some sort of replied = True/False thing?

User avatar
saguaro
Miko-Class Veteran
Posts: 560
Joined: Sun Feb 12, 2012 9:17 am
Completed: Locked-In, Sunrise, The Censor
Organization: Lucky Special Games
itch: saguarofoo
Location: USA
Contact:

Re: Basic Message System

#8 Post by saguaro »

The reply label should include $ current_message.can_reply = False, not current_message.reply. I changed the field name when I made another change and didn't include the new variable in the reply example.

wyverngem found an issue with the message count and some other things, so I'll wait until we've resolved them all before I update the recipe.

User avatar
saguaro
Miko-Class Veteran
Posts: 560
Joined: Sun Feb 12, 2012 9:17 am
Completed: Locked-In, Sunrise, The Censor
Organization: Lucky Special Games
itch: saguarofoo
Location: USA
Contact:

Re: Basic Message System

#9 Post by saguaro »

All right, I updated the problem above in the example script and fixed the counter issue.

The message count should only be based on visible messages, not total messages in the list. A list comprehension lets us do this. If you're already using this system, here's the updated code:

Code: Select all

screen mailbox:
    tag menu
    modal True
    default current_message = None
    $ visible_messages = [i for i in mail if i.view]
    frame:
        style_group "mailbox"
        vbox:
            label "Inbox"
            if new_messages > 0:
                text ("Messages: " + str(len(visible_messages)) + " ([new_messages] unread)")
            else:
                text ("Messages: " + str(len(visible_messages)))

User avatar
Fleeples
Newbie
Posts: 24
Joined: Thu Apr 25, 2013 2:13 pm
Projects: If!Girl
Contact:

Re: Basic Message System

#10 Post by Fleeples »

Thanks! This is going to be really useful for my VN. <3

User avatar
netravelr
Miko-Class Veteran
Posts: 504
Joined: Thu Jan 28, 2010 2:31 am
Completed: Culina: Hands in the Kitchen, Culina: The Spirit of Cooking, Saving Zoey
Projects: Love at the Laundromat
Organization: Lakeview Interactive
Deviantart: netravelr
Location: USA
Contact:

Re: Basic Message System

#11 Post by netravelr »

This is great! Very useful to some people, I'm sure. Thanks for sharing this!
Image
Technical Designer/Programmer
Game Design Portfolio - Project updates on my Twitter
Experienced in: C/C++/C#, Python, Unreal, Unity, and Flash
_________________
"Space can be very lonely. The greatest adventure is having someone share it with you."

Kingv
Veteran
Posts: 324
Joined: Sun Jan 29, 2012 11:54 pm
Projects: Pet Tails, Transformation Sequence, Cheer On!
Tumblr: EphemeralBalance
Deviantart: kingv
Location: USA
Contact:

Re: Basic Message System

#12 Post by Kingv »

I'm really loving this code! I was scratching my head trying to figure out how I'd accomplish something like this on my own when I found this post. But I have a question... How would I go about modifying the code that it would override the Ren'Py theme and I'd see custom graphics in it's place? I wanted to change it so that it would look like an actual email inbox but I'm not sure where to start or what code to change. Any help would be greatly appreciated!

User avatar
saguaro
Miko-Class Veteran
Posts: 560
Joined: Sun Feb 12, 2012 9:17 am
Completed: Locked-In, Sunrise, The Censor
Organization: Lucky Special Games
itch: saguarofoo
Location: USA
Contact:

Re: Basic Message System

#13 Post by saguaro »

It references the custom style "mailbox", which is located at the bottom of messages.rpy.

To modify the appearance of the inbox you can also make changes to the mailbox screen in messages.rpy. To modify the reply screen make your changes to the top section of the choice screen in screens.rpy.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1460
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: Basic Message System

#14 Post by xavimat »

Thanks saguaro. With your permission, I have some proposals to this:

- When a message is added, a chime can sound and the little button to access the mail can be automatically shown adding two lines in the definition of “add_message” in “message.rpy” (of course, you need to add the file “chime.wav” or similar):

Code: Select all

    def add_message(subject, sender, body, reply_label=False):
        renpy.sound.play("chime.wav")
        message = Mail(subject, sender, body, reply_label)
        renpy.show_screen("mailbox_overlay")
- Maybe we need a delay in the reply of the reply. I mean, in the example, when we reply to “Fred” his answer appears immediately. I've added two functions to add messages that will appear later:

Code: Select all

    # PUT THIS IMMEDIATELY AFTER THE “DEF ADD_MESSAGE” IN “MESSAGES.RPY”
    def add_later(subject, sender, body, reply_label=False):
        mail_later.append([subject, sender, body, reply_label])
        
    def add_now():
        for element in mail_later:
            add_message(element[0], element[1], element[2], element[3])
        global mail_later
        mail_later = []

    # ADD THIS IN THE BEGINNING OF “label start”, JUST AFTER “$ mail = []” in the SCRIPT.RPY:
    $ mail_later = []
How this works? You can add messages to be shown later the same way you did with “add_message”:

Code: Select all

# Changed “fred_reply”:
label fred_reply(current_message):
    menu:
        "I won't apologize, Fred. You smell like old socks and I hate you!":
            $ add_later("Fine then!", "Fred", "THIS MEANS WAR")  # changed 'add_message' with 'add_later'
            $ current_message.can_reply = False
        "You're right. I'm sorry, Fred.  Let's be friends.":
            $ add_later("<3", "Fred", "Okay! BFFs!")  # changed 'add_message' with 'add_later'
            $ current_message.can_reply = False
        "Don't reply yet.":
            pass
    return
The message will be stored, and, with the simple code:

Code: Select all

$ add_now()
all the stored messages will be added to the mailbox.

Hope it's useful.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
saguaro
Miko-Class Veteran
Posts: 560
Joined: Sun Feb 12, 2012 9:17 am
Completed: Locked-In, Sunrise, The Censor
Organization: Lucky Special Games
itch: saguarofoo
Location: USA
Contact:

Re: Basic Message System

#15 Post by saguaro »

xavimat, good ideas, thanks for that code. I added links to the top page pointing to these features.

Post Reply

Who is online

Users browsing this forum: No registered users