Page 1 of 1

Listing the contents of inventories/message inboxes

Posted: Thu Apr 07, 2011 1:04 pm
by SpoilerDuck
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!

Re: Listing the contents of inventories/message inboxes

Posted: Fri Apr 08, 2011 2:59 am
by denzil
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)

Re: Listing the contents of inventories/message inboxes

Posted: Fri Apr 08, 2011 7:32 am
by SpoilerDuck
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!

Re: Listing the contents of inventories/message inboxes

Posted: Fri Apr 08, 2011 7:56 am
by denzil
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.