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.
Message
Author
User avatar
Kokoro Hane
Eileen-Class Veteran
Posts: 1236
Joined: Thu Oct 27, 2011 6:51 pm
Completed: 30 Kilowatt Hours Left, The Only One Girl { First Quarter }, An Encounter ~In The Rain~, A Piece of Sweetness, Since When Did I Have a Combat Butler?!, Piece by Piece, +many more
Projects: Fateful Encounter, Operation: Magic Hero
Organization: Tofu Sheets Visual
Deviantart: kokoro-hane
itch: tofu-sheets-visual
Contact:

Re: Basic Message System

#16 Post by Kokoro Hane »

This code looks excellent! This one has a reply feature, which I really like~ I will sure have fun using it.
PROJECTS:
Operation: Magic Hero [WiP]
Piece By Piece [COMPLETE][Spooktober VN '20]
RE/COUNT RE:VERSE [COMPLETE][RPG]
Since When Did I Have a Combat Butler?! [COMPLETE][NaNoRenO2020+]
Crystal Captor: Memory Chronicle Finale [COMPLETE][RPG][#1 in So Bad It's Good jam '17]

But dear God, You're the only North Star I would follow this far
Owl City "Galaxies"

User avatar
Dragonstar89
Regular
Posts: 163
Joined: Mon Aug 12, 2013 11:28 pm
Projects: TBA
Organization: Self
IRC Nick: dstar89
Deviantart: senpai-jake
Skype: dstar_891
Location: West Virginia, USA
Contact:

Re: Basic Message System

#17 Post by Dragonstar89 »

Good work! It looks great :) I'm going to play around with the codes later on, and see if I can add any new things to fit it to my liking. Thanks for sharing this!

User avatar
Dragonstar89
Regular
Posts: 163
Joined: Mon Aug 12, 2013 11:28 pm
Projects: TBA
Organization: Self
IRC Nick: dstar89
Deviantart: senpai-jake
Skype: dstar_891
Location: West Virginia, USA
Contact:

Re: Basic Message System

#18 Post by Dragonstar89 »

This is neat, and the overlay is working, but for some reason I can't add a message. I used the codes xavimat shared so some of the messages can be received later, but looking at my code I use the $ add_message with the details filled in. When I test the script and open the game though, the mailbox shows nothing...

Code: Select all

label start:

$new_messages = 0
$mail = []
$mail_later = []
$reply_screen = False

python:
    povname = renpy.input("Please enter your name.")
    povname = povname.strip()

    if not povname:
         povname = "Drenchin"

show m normal at left
with dissolve
m "My name is [povname]!"

"Well, [povname]. Let us begin your tale of adventure, danger, darkness, and frienship. Let us begin.."
"Your {b}Tale, of Arithea.{/b}"

#The Message(Letter) System. Holds not much significance to the game, but gives helpful info.

$new_messages = 0
$mail = []
$mail_later = []
$reply_screen = False

scene forest
with dissolve

show m normal at left
with dissolve

$ add_message("Adventurerers notice!", "Royal Adv Guild", "Welcome to the Adventuring world, [povname]! How do we know you're adventuring? We have our ways. Check the Mail office every day to keep up with new letters!")

m "Let's check the Mailbox! I hope I got a letter!"


show screen mailbox_overlay
"You have {b}1{/b} letter."

return
Any ideas of what may be a miss here?
Beginning pre-production work on a project in Renpy. After being away for 5 years, it's time to get back in the game 8)

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

#19 Post by saguaro »

Per xavimat's post you will need

Code: Select all

$ add_now()
to add messages to the list. Try including that after your add_message in the script.

User avatar
Dragonstar89
Regular
Posts: 163
Joined: Mon Aug 12, 2013 11:28 pm
Projects: TBA
Organization: Self
IRC Nick: dstar89
Deviantart: senpai-jake
Skype: dstar_891
Location: West Virginia, USA
Contact:

Re: Basic Message System

#20 Post by Dragonstar89 »

Hello, thanks for replying.


I did just that, but then an error appeared when I had just $ add_now() saying I hadn't included the global subject. After that, I did $ add_now() with the message contents in the right order between ( and ) and then it said add_now wasn't defined..
Beginning pre-production work on a project in Renpy. After being away for 5 years, it's time to get back in the game 8)

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

#21 Post by saguaro »

Would you mind sharing your script? You can paste in code brackets if you want, I just need to see how you incorporated xavimat's code to the other mailbox stuff.

The add_now function doesn't take parameters, but I'm not sure why you are getting the global subject error.

User avatar
Dragonstar89
Regular
Posts: 163
Joined: Mon Aug 12, 2013 11:28 pm
Projects: TBA
Organization: Self
IRC Nick: dstar89
Deviantart: senpai-jake
Skype: dstar_891
Location: West Virginia, USA
Contact:

Re: Basic Message System

#22 Post by Dragonstar89 »

Here's my (main) script.rpy:

Code: Select all

# You can place the script of your game in this file.



# Declare characters used by this game.
define m = Character('[povname]')
define juro = Character('Juro', color="#fdfdfd")
define midirake = Character('Midirake', color="#fdfdfd")
define ruki = Character('Ruki', color="#fdfdfd")
define queen = Character('Queen', color="#fdfdfd")
define cici = Character('Cici', color="#fdfdfd")
define tetiri = Character('Tetiri', color="#fdfdfd")
define shyann = Character('Shyann', color="#fdfdfd")
define mai = Character('Mai', color="#fdfdfd")
define malissa = Character('Malissa', color="#fdfdfd")
define katt = Character('Katt', color="#fdfdfd")
define felicity = Character('Felicity', color="#fdfdfd")
define gairah = Character('Gairah', color="#fdfdfd")
define tyran = Character('Tyran', color="#fdfdfd")
define zoart = Character('Zoart', color="#ff0000")
#Define the Foe Characters
define guard = Character('Guard', color="#ff0000")



# The game starts here.
label start:

$new_messages = 0
$mail = []
$mail_later = []
$reply_screen = False

python:
    povname = renpy.input("Please enter your name.")
    povname = povname.strip()

    if not povname:
         povname = "Drenchin"

show m normal at left
with dissolve
m "My name is [povname]!"

"Well, [povname]. Let us begin your tale of adventure, danger, darkness, and frienship. Let us begin.."
"Your {b}Tale, of Arithea.{/b}"

#The Message(Letter) System. Holds not much significance to the game, but gives helpful info.

$new_messages = 0
$mail = []
$mail_later = []
$reply_screen = False

scene forest
with dissolve

show m normal at left
with dissolve

$ add_message("Adventurerers notice!", "Royal Adv Guild", "Welcome to the Adventuring world, [povname]! How do we know you're adventuring? We have our ways. Check the Mail office every day to keep up with new letters!")

m "Let's check the Mailbox! I hope I got a letter!"


show screen mailbox_overlay
"You have {b}1{/b} letter."

return
Here's the messages.rpy with xavimat's edits included:

Code: Select all

init python:
    import renpy.store as store

    class Mail(store.object):
        def __init__(self, subject, sender, body, reply_label, view=True, status=True):
            self.subject = subject
            self.sender = sender
            self.body = body
            self.reply_label = reply_label
            self.view = view
            self.status = status
            mail.insert(0, self)
            if reply_label:
                self.can_reply = True
            else:
                self.can_reply = False
            global new_messages
            new_messages = new_messages + 1

        def reply(self):
            global reply_screen
            reply_screen = True
            renpy.call_in_new_context(self.reply_label, current_message=self)
            reply_screen = False

        def delete(self):
            self.view = False

        def restore(self):
            self.view = True

        def mark_read(self):
            self.status = False

    def restore_all():
        deleted_messages = [x for x in mail if x.view == False]
        for x in deleted_messages:
            x.restore()
        renpy.restart_interaction()

    def mark_all_read():
        unread_messages = [x for x in mail if x.status]
        for x in unread_messages:
            x.mark_read()

    def add_message(subject, sender, body, reply_label=False):
            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 = []

        message = Mail(subject, sender, body, reply_label)

screen mailbox_overlay:
    hbox:
        xalign 1.0 yalign 0.0
        if new_messages > 0:
            textbutton "Mailbox ([new_messages] New)" action Show("mailbox")
        else:
            textbutton "Mailbox" action Show("mailbox")

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)))
            side "c r":
                area (0,0,800,93)
                viewport id "message_list":
                    draggable True mousewheel True
                    vbox:
                        for i in mail:
                            if i.view:
                                if i.status:
                                    textbutton ("*NEW* " + i.sender + " - " + i.subject) action [SetScreenVariable("current_message",i), i.mark_read, SetVariable("new_messages",new_messages-1)] xfill True
                                else:
                                    textbutton (i.sender + " - " + i.subject) action SetScreenVariable("current_message",i) xfill True
                vbar value YScrollValue("message_list")
            hbox:
                null height 20
            side "c r":
                area (0,0,800,380)
                viewport id "view_message":
                    draggable True mousewheel True
                    vbox:
                        if current_message:
                            text ("From: " + current_message.sender)
                            text ("Subject: " + current_message.subject)
                            text current_message.body
                vbar value YScrollValue("view_message")
            use mailbox_commands

screen mailbox_commands:
    hbox:
        if current_message and current_message.can_reply:
            textbutton "Reply" action current_message.reply
        else:
            textbutton "Reply" action None
        if current_message:
            textbutton "Delete" action [current_message.delete, SetScreenVariable("current_message", None)]
        else:
            textbutton "Delete" action None
        if new_messages > 0:
            textbutton "Mark All Read" action [mark_all_read, SetVariable("new_messages",0)]
        else:
            textbutton "Mark All Read" action None
        textbutton "Restore All" action restore_all
        textbutton "Exit" action Hide("mailbox")

init -2 python:
    style.mailbox = Style(style.default)
    style.mailbox_vbox.xalign = 0.5
    style.mailbox_vbox.xfill = True
    style.mailbox_hbox.xalign = 0.5
    style.mailbox_label_text.size = 30
    style.mailbox_label_text.xalign = 0.5
    style.mailbox_label.xfill = True
    style.mailbox_frame.xalign = 0.5
    style.mailbox_frame.yalign = 0.5
By the way; I removed the $ add_now() string, since it wouldn't allow the script to work right.
Beginning pre-production work on a project in Renpy. After being away for 5 years, it's time to get back in the game 8)

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

#23 Post by saguaro »

ok this is your problem. you've got a couple of function stacked into each other.

Code: Select all

    def add_message(subject, sender, body, reply_label=False):
            def add_later(subject, sender, body, reply_label=False):
                mail_later.append([subject, sender, body, reply_label])
you'll want to include it like so

Code: Select all

    def add_message(subject, sender, body, reply_label=False):
        message = Mail(subject, sender, body, reply_label)        
                
    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 = []  
then to have a delay you'll use add_later(blahblah).... otherwise you'll use add_message(blahblah) to add it instantly

if you queue up messages with add_later, you'll use add_now() to put them in the mailbox when you're ready

User avatar
Dragonstar89
Regular
Posts: 163
Joined: Mon Aug 12, 2013 11:28 pm
Projects: TBA
Organization: Self
IRC Nick: dstar89
Deviantart: senpai-jake
Skype: dstar_891
Location: West Virginia, USA
Contact:

Re: Basic Message System

#24 Post by Dragonstar89 »

Awesome! Thank you so much :)
Beginning pre-production work on a project in Renpy. After being away for 5 years, it's time to get back in the game 8)

User avatar
Dragonstar89
Regular
Posts: 163
Joined: Mon Aug 12, 2013 11:28 pm
Projects: TBA
Organization: Self
IRC Nick: dstar89
Deviantart: senpai-jake
Skype: dstar_891
Location: West Virginia, USA
Contact:

Re: Basic Message System

#25 Post by Dragonstar89 »

xavimat wrote: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.
I have a question re the $ add_now() code. In my script, I used $ add_message to make a default tutorial message appear instantly, then underneath that I used this:

Code: Select all

$add_later("Where are you?", "Your Father & Mother ", "Our son [povname]. Where have you gone? Has your journey to Raegnaus been safe? Please inform us on your whereabouts. We're so worried..", "letter_reply_one")
Then, in the label town where the player can go to every so often for shopping, etc. I have used:

Code: Select all

 $add_now() 
When I use that, the traceback tells me that there is no subject, message, etc. defined, and then when I put the contents of the $add_later message inside the $add_now variable, the traceback tells me that $add_now takes no arguments. So, what am I suppose to do with " $add_now() " :?:
Beginning pre-production work on a project in Renpy. After being away for 5 years, it's time to get back in the game 8)

User avatar
Kate
Regular
Posts: 197
Joined: Thu Sep 19, 2013 6:10 pm
Projects: Blackout
Organization: Moonlight Otome
Location: United States
Contact:

Re: Basic Message System

#26 Post by Kate »

I am having trouble with Ren Py placing the code to insert "your name" into the game. Can anyone help me with this specific problem? If you've played voltage, it allows the player to insert their name and use it in the game. I want to have this option, but am new and very, very confused!

User avatar
Dragonstar89
Regular
Posts: 163
Joined: Mon Aug 12, 2013 11:28 pm
Projects: TBA
Organization: Self
IRC Nick: dstar89
Deviantart: senpai-jake
Skype: dstar_891
Location: West Virginia, USA
Contact:

Re: Basic Message System

#27 Post by Dragonstar89 »

Kate wrote:I am having trouble with Ren Py placing the code to insert "your name" into the game. Can anyone help me with this specific problem? If you've played voltage, it allows the player to insert their name and use it in the game. I want to have this option, but am new and very, very confused!
Not meant for this topic, but here you go:

Code: Select all

    $ player_name = renpy.input("What is your name?")

    $ player_name = player_name.strip()
# player_name.strip will remove any 'whitespace' added in names

#  The following sets a name if the user doesn't choose one
#  choose a suitable one for them:
    if player_name == "":
        $ player_name="Billy"
  
    e "Pleased to meet you, %(player_name)s!"
You also need to set the character for the player and their name by doing the following in your script.rpy:\

Code: Select all

define main = Character("[player_name]")
Also, you could just replace %(player_name)s with simply [player_name] in any string of dialogue.
Beginning pre-production work on a project in Renpy. After being away for 5 years, it's time to get back in the game 8)

User avatar
storykween
Regular
Posts: 154
Joined: Mon Sep 30, 2013 1:17 pm
Completed: Serafina's Saga, Quantum Conscience, Echoes of the Fey, miraclr - Divine Dating Sim
Organization: Woodsy Studio
Location: St Louis
Contact:

Re: Basic Message System

#28 Post by storykween »

Thanks for posting this coding, it is very helpful!

I'd love to know if there's a way to keep track of whether the player has read a specific message? For instance, I want to achieve something like this:

Code: Select all

$ add_message("Annoyed", "Danny", "Geez, I'm having such a horrible day! First I step in dog crap and now there's no coffee!")

"Wow, Danny is in such a horrible mood!"

if message(Annoyed)= Read:
     "But I guess I don't blame him."
else:
     "He needs to practice better manners."
Obviously message(Annoyed) = Read: is not a code that would work, but can you tell me what would?

**EDIT**

I got some help with this here for the basic email system, if anyone else is curious about incorporating it into theirs: http://lemmasoft.renai.us/forums/viewto ... 42#p301242

Still haven't figured out how to use it in this email system, though. Your coding already has a Read/Unread feature, but how can I check during the game whether a specific email has been read?
Image
My website: woodsy-studio.com

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

#29 Post by saguaro »

I answered something similar earlier, the basic principal is the same.

We add a new function to messages.rpy:

Code: Select all

    def check(subject):
        for item in mail:
            if item.subject == subject:
                if item.status:
                    return False #message is marked as unread
                else:
                    return True # message is marked as read
You can check like so:

Code: Select all

    if check("Test message"):
        e "Yep!"
    else:
        e "Nope!"    
It only works if you use unique subjects for each message. Otherwise it will always check the first mail object on the list with that subject. Also, if the reader toggles the message unread it will check as unread.

User avatar
storykween
Regular
Posts: 154
Joined: Mon Sep 30, 2013 1:17 pm
Completed: Serafina's Saga, Quantum Conscience, Echoes of the Fey, miraclr - Divine Dating Sim
Organization: Woodsy Studio
Location: St Louis
Contact:

Re: Basic Message System

#30 Post by storykween »

Excellent, this should work great. Thank you!
Image
My website: woodsy-studio.com

Post Reply

Who is online

Users browsing this forum: No registered users