Listing the contents of inventories/message inboxes

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
User avatar
SpoilerDuck
Newbie
Posts: 17
Joined: Thu Apr 07, 2011 10:27 am
Contact:

Listing the contents of inventories/message inboxes

#1 Post by SpoilerDuck » Thu Apr 07, 2011 1:04 pm

Hello, all! I've been trying to create a message system using The Inventory and Money System in the cookbook as my base. I'm aware there is an actual message system also in the cookbook, but as it isn't documented I can't understand what does what and I am loath to copy+paste code into my project without knowing exactly how it works (I'm trying to teach myself here!).

So far I have this as the contents of scripts I've named messages.rpy:

Code: Select all

init python:
    #Message classes
    class Message:
        def __init__(self, subject, sender, body):
            self.subject = subject
            self.sender = sender
            self.body = body
            
    class Inbox:
        def __init__(self):
            self.messages = []
            
        def send(self, message):
            self.messages.append(message)
            return True
            
        def has_message(self, message):
            if message in self.messages:
                return True
            else:
                return False
            
              
           
    #Variable declaration
    inbox = Inbox()
    messages = Inbox
    
    #The messages themselves
    welcome1 = Message("Welcome to the message system!", "Eileen", "Welcome to the message system! This is where you can send and receive messages from others.")
    welcome2 = Message("Using the message system", "Eileen", "Using the message system is a snap! Simply select the message you want to read in your inbox with a click and it opens! But you knew that already, didn't you?")
In script.rpy, at certain points in the story the messages 'welcome1' and 'welcome2' are inserted into inbox.messages. Using the inbox.has_message() function I'm able to confirm if a particular message has been inserted successfully.

What I'd like to know is how I'd go about listing the subjects all the messages in inbox.messages, and placing each entry within its own ui.textbutton which I can use to call up a new frame containing the message in full. Basically, making an actual inbox.

I'm brand new to python, so attempts to hack together a function for reading from inbox.message and displaying the list aren't going so well. I figured I should stop guessing and ask for a bit of help!

User avatar
denzil
Veteran
Posts: 293
Joined: Wed Apr 20, 2005 4:01 pm
Contact:

Re: Listing the contents of inventories/message inboxes

#2 Post by denzil » Fri Apr 08, 2011 2:59 am

Just add this code into class Inbox: and then you can just use inbox.show_messages() to show the textbuttons. You probably will want to wrap the textbuttons into ui.vbox() or something. Also you will need to modify the clicked action.

Code: Select all

class Inbox:
    def show_messages(self):
        for message in self.messages:
            ui.textbutton(message.subject, clicked=show_message_details)
Practice makes purrrfect.
Finished projects: Broken sky .:. colorless day .:. and few more...

User avatar
SpoilerDuck
Newbie
Posts: 17
Joined: Thu Apr 07, 2011 10:27 am
Contact:

Re: Listing the contents of inventories/message inboxes

#3 Post by SpoilerDuck » Fri Apr 08, 2011 7:32 am

Thank you so much! So what exactly is going on with that bit of code? I understand everything except (for) "message" - does Python allow you to use a variable like that to signify an index without having to first declare/define it?

Also used the knowledge gained from your code to make a display_message() function as well. Thanks to you I'm learning!

User avatar
denzil
Veteran
Posts: 293
Joined: Wed Apr 20, 2005 4:01 pm
Contact:

Re: Listing the contents of inventories/message inboxes

#4 Post by denzil » Fri Apr 08, 2011 7:56 am

SpoilerDuck wrote:Thank you so much! So what exactly is going on with that bit of code? I understand everything except (for) "message" - does Python allow you to use a variable like that to signify an index without having to first declare/define it?
Yes, Python creates the variable first time you assign something to it, so usually there's no need to declare it before.

If you want better explanation you could read Python Tutorial which is a good and quick way to learn Python basics. Or if you already know some programming then Dive Into Python is a good reading.
Practice makes purrrfect.
Finished projects: Broken sky .:. colorless day .:. and few more...

Post Reply

Who is online

Users browsing this forum: gas