[SOLVED]Simple Chat problem!

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
Sterkiherz
Regular
Posts: 87
Joined: Wed Oct 03, 2018 6:16 pm
Projects: Comic: Miraclewish; Visual Novel: Murder Mystery Ghost Story (WIP)
Organization: Sterki Herz
Tumblr: sterkiherz
itch: sterkiherz
Discord: sterkiherz#7005
Contact:

[SOLVED]Simple Chat problem!

#1 Post by Sterkiherz »

Hello! How are you doing?

I was doing some testing today and I've decided I wanted to add some kind of chat system to my game.
- I've edited this post to show my solution to this! -

I've managed to fix this problem and I guess I'll post my full code here just in case anyone else needed it~

Code: Select all

#---CHAT SYSTEM
init -1 python:
    class Message(object):
        def __init__(self, pic, name, message):
            self.pic = pic
            self.name = name
            self.message = message
# Mom chatlog:
    class MChatlog(object):
        def __init__(self):
            self.history = []

        def addmessage(self, pic, name, message):
            self.history.append(Message(pic, name, message))

        def delchat(self):
            self.history = []

screen chatdisplay:
    tag menu
    use no_quick_menu
    zorder 5
    add "gui/nvl.png"

    viewport:
        mousewheel True
        side_area (430, 120, 400, 400)
        yinitial 0.0
        scrollbars "vertical"
        vbox:
            spacing 10
            for i in Mchat.history:
                if i.name == "You": #check speakers name
                    frame:
                        xalign 1.0
                        #position the player's chat bubble
                        xoffset 75
                        background "#dedede"
                        maximum (300, 500)#(300,200)
                        vbox:
                            hbox:
                                add i.pic xalign 1.0
                                text i.name font "fonts/Satisfy-Regular.ttf" size 25 color "#dedede" outlines [(1, "#595959")] xalign 1.0
                            text i.message font "fonts/ABeeZee-Regular.ttf" size 18 color "#ffffff" xalign 1.0
                else:
                    frame:
                        xalign 0.0
                        background "#595959"
                        maximum (300, 500)
                        vbox:
                            hbox:
                                add i.pic
                                text i.name font "fonts/Satisfy-Regular.ttf" size 25 color "#dedede" outlines [(1, "#595959")]
                            text i.message font "fonts/ABeeZee-Regular.ttf" size 18 color "#ffffff"

init:
    default Mchat = MChatlog()

#define avatars
image mom = "messenger/av/Mom_s.png"
#image mrj = "messenger/av/Mom_s.png" --> need image
image you = "messenger/av/You_s.png"
image alice = "messenger/av/Alice_s.png"
image tahiel = "messenger/av/Tahiel_s.png"
image wren = "messenger/av/Wren_s.png"
image kevan = "messenger/av/Kevan_s.png"
image nieves = "messenger/av/Nieves_s.png"

#----------------------------------------

label chat_test:
    n "okay, let me add some messages..."
    $ Mchat.addmessage('you', "You", "Mom, I'll be home later today.")
    #$metxt=True
    play sound "audio/message.ogg"
    show screen chatdisplay
    $renpy.pause()

    $ Mchat.addmessage('you', "You", "I'm on my way to a Friends Place.")
    #$metxt=True
    play sound "audio/message.ogg"
    $renpy.pause()

    $ Mchat.addmessage('mom', "Mom", "That's alright honey. Just don't go home too late.")
    #$metxt=False
    play sound "audio/message.ogg"
    $renpy.pause()

    $ Mchat.addmessage('mom', "Mom", "Okay?")
    #$metxt=False
    play sound "audio/message.ogg"
    $renpy.pause()

    $ Mchat.addmessage('you', "You", "Yeah, yeah. I know.")
    #$metxt=True
    play sound "audio/message.ogg"
    $renpy.pause()

    $ Mchat.addmessage('mom', "Mom", "Alright, I'll be waiting..")
    #$metxt=False
    play sound "audio/message.ogg"
    $renpy.pause()

    $ Mchat.addmessage('mom', "Mom", "Love you {image=kiss}")
    #$metxt=False
    play sound "audio/message.ogg"
    $renpy.pause()

    $ Mchat.addmessage('you', "You", "{image=kiss} {image=kiss}")
    #$metxt=True
    play sound "audio/message.ogg"
    $renpy.pause()
    hide screen chatdisplay

    n "There. It works now!"
I'm not an expert at this and there's probably better ways to make this work but this is the way I've managed to fix it so hey, here it is and I hope someone finds it useful.

So what did I change?
* The user could not click to advance text - Apparently I had my screen defined with the 'Modal=True' statement and 'renpy.pause()' was not having any effect. Deleting this statement seemed to have fixed this issue.
* I've added a way to check if speakers name is set to 'You' so that the chat bubble is positioned and aligned to the right.
---
Thank you for reading and have a nice day~ n.n
Image
๐“Ÿ๐“ธ๐“ป๐“ฝ๐“ฏ๐“ธ๐“ต๐“ฒ๐“ธ โœฆ ๐“๐“ป๐“ฝ ๐“‘๐“ต๐“ธ๐“ฐ โœฆ ๐““๐“ฒ๐“ผ๐“ฌ๐“ธ๐“ป๐“ญ (sterkiherz#7005) โœฆ sterki.herz@gmail.com
~~Feel free to contact me for any questions~~
๏ธตโ€ฟ๏ธตโ€ฟเญจโ™กเญงโ€ฟ๏ธตโ€ฟ๏ธต
๐ป๐“Š๐‘”๐“ˆ & ๐’ฆ๐’พ๐“ˆ๐“ˆ๐‘’๐“ˆ, ๐’ฎ๐“‰๐‘’๐“‡๐“€๐’พ ๐ป๐‘’๐“‡๐“

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]