How can I reduce the amount of code for this screen?

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
zeroTheHero
Regular
Posts: 64
Joined: Mon May 07, 2018 10:49 am
Contact:

How can I reduce the amount of code for this screen?

#1 Post by zeroTheHero »

So this is the code I have-

Code: Select all

 screen Part1:
    frame:
        background "cafe (1).jpg" 
        viewport:
            side "c b r"
            draggable True
            mousewheel True
            add "confirm.png"
           
            vbox:
                text "Pikachu"                                             #1 (This space is the 'name' of the character)
                frame:
                    background "textbox4.png"                              #2 (I will be using different textboxes)
                    hbox:
                        spacing 20
                        image "pika1.jpg"                                  #3 (character image/emoji)
                        text "pika pika peeeee" at truecenter              #4 (dialogue)

                vbox:
                    xpos 0.5
                    text "Charmander" xalign 
                    frame:
                        background "textbox4.png"
                        hbox:
                            spacing 20
                            image "charmander1.png" 
                            text "charmander char char" at truecenter 
It looks like (kind of) a messaging/chat system. This is what it looks like: https://imgur.com/InboyyY

Right now I have to repeat the whole block to get in another line of text. Questions-

1. How can I write this out so I wouldn't have to repeat 'vbox: frame: hbox:' every time I want to add a new message? Basically, each new line of text has 4 places that change, the 'name', 'textbox', 'image' and 'dialogue'. So, I guess a system where these 4 places become variables which I can update is what I'd be trying to make? I seriously suck at python, my only experience is a head first book, so I have (almost) no clue what or how to do this.

2. How do I stop after each message and have the user 'click to continue' ? I tried using $ renpy.pause() but the game sorta freezes; no amount of clicking can progress the game. The only other way I can think of is a roundabout way of using ShowIf() and setVariable(), but that would mean more lines of redundant code per block and I'm hoping there's a better way to do this.

3. To add new dialogue, each block of code has to be indented below the other. It's not much hassle with 2 lines of dialogue, but in a game with hundreds of dialogues, I'm thinking it might become a problem. Do I need to change something? Or is 400 spaces before a line common in coding?

Sorry for the long post, it's a week's worth of effort and questions. I appreciate your time, thanks!

philat
Eileen-Class Veteran
Posts: 1912
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: How can I reduce the amount of code for this screen?

#2 Post by philat »

a) Well, the way you've set it up, you kind of can't reduce the code much.

b) Wouldn't it be better to use another messenger framework and modify as needed? (e.g., viewtopic.php?f=51&t=50153 )? At the very least, you could look at how it's set up and take pointers.

zeroTheHero
Regular
Posts: 64
Joined: Mon May 07, 2018 10:49 am
Contact:

Re: How can I reduce the amount of code for this screen?

#3 Post by zeroTheHero »

philat wrote: Tue Sep 04, 2018 12:49 am a) Well, the way you've set it up, you kind of can't reduce the code much.

b) Wouldn't it be better to use another messenger framework and modify as needed? (e.g., viewtopic.php?f=51&t=50153 )? At the very least, you could look at how it's set up and take pointers.
Hey, thanks for the link philat! I thought I'd found and read most of the chat/messenger style posts but this one's new.
The thread looks complex, so while I go through it can I run by you a couple of (potential) solutions I found in the documentation? It would definitely help!

Before that, let me just put a TL;DR of my post...it kinda looks too verbose

TL;DR: How can I use a basic screen over and over again while changing the text it displays?

Will these work:
1) This function "renpy.define_screen" https://www.renpy.org/doc/html/screen_p ... ine_screen There is also an example of it's usage on the top of the page.

2) Milkyman left this awesome bit of code for a messenger-ish framework in this thread viewtopic.php?t=44711#p459664
It looks a lot like what I want, but I can't wrap my head around how his function works. Maybe I should make a post about this...

Thanks for answering, though! Gotta prep myself to spend today going through your messenger thread
Last edited by zeroTheHero on Tue Sep 04, 2018 2:46 am, edited 1 time in total.

User avatar
Andredron
Miko-Class Veteran
Posts: 719
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: How can I reduce the amount of code for this screen?

#4 Post by Andredron »

zeroTheHero wrote: Tue Sep 04, 2018 2:36 am
philat wrote: Tue Sep 04, 2018 12:49 am a) Well, the way you've set it up, you kind of can't reduce the code much.

b) Wouldn't it be better to use another messenger framework and modify as needed? (e.g., viewtopic.php?f=51&t=50153 )? At the very least, you could look at how it's set up and take pointers.
Hey, thanks for the link philat! I thought I'd found and read most of the chat/messenger style posts but this one's new.
The thread looks complex, so while I go through it can I run by you a couple of (potential) solutions I found in the documentation? It would definitely help!

Before that, let me just put a TL;DR of my post...it kinda looks too verbose

TL;DR: How can I use a basic screen over and over again while changing the text it displays?

Will these work:
1) This function "renpy.define_screen" https://www.renpy.org/doc/html/screen_p ... ine_screen

2) Milkyman left this awesome bit of code for a messenger-ish framework in this thread viewtopic.php?t=44711#p459664
It looks a lot like what I want, but I can't wrap my head around how his function works. Maybe I should make a post about this...

Thanks for answering, though! Gotta prep myself to spend today going through your messenger thread
Added a simpler version, it will help

viewtopic.php?f=51&t=50153

zeroTheHero
Regular
Posts: 64
Joined: Mon May 07, 2018 10:49 am
Contact:

Re: How can I reduce the amount of code for this screen?

#5 Post by zeroTheHero »

Andredron wrote: Tue Sep 04, 2018 2:44 am Added a simpler version, it will help
viewtopic.php?f=51&t=50153
Thanks! philat posted the same link, pretty sure I'll learn something useful from it ^_^

Btw it looks like the comments in the code is in russian... I can use the google translate version, but just in case I've missed it, is there an English version as well?
Thanks again! ~

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: How can I reduce the amount of code for this screen?

#6 Post by trooper6 »

How do you use a screen over and over again while changing the text it displays? Easy, pass along the information. Screens take variables after all.

Code: Select all

screen winner_screen(name):
    frame:
        text "Today's winner is: [name]"

## The game starts here.

label start:
    "Your game starts. Who is the winner though?"
    show screen winner_screen("Judy")
    "That is cool...but with if the winner changes?"
    show screen winner_screen("Jack")
    "Oh the new winner?"

    return
But you can also just have a text1 and text2 variable and just change that variable. When you want to, like so:

Code: Select all

screen msg_screen():
    frame:
        vbox:
            text "The first text is: [text1]"
            text "The second text is [text2]"

default text1 = "How are you today?"
default text2 = "Fine."
## The game starts here.

label start:
    "Your game starts. Let's show the screen"
    show screen msg_screen()
    "That is cool...now let's change the text!"
    $text1 = text2
    $text2 = "What are you up to?"
    "Look I changed the screen!"

    return
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

zeroTheHero
Regular
Posts: 64
Joined: Mon May 07, 2018 10:49 am
Contact:

Re: How can I reduce the amount of code for this screen?

#7 Post by zeroTheHero »

trooper6 wrote: Tue Sep 04, 2018 2:59 am How do you use a screen over and over again while changing the text it displays? Easy, pass along the information. Screens take variables after all.

Code: Select all

screen winner_screen(name):
    frame:
        text "Today's winner is: [name]"

## The game starts here.

label start:
    "Your game starts. Who is the winner though?"
    show screen winner_screen("Judy")
    "That is cool...but with if the winner changes?"
    show screen winner_screen("Jack")
    "Oh the new winner?"

    return
But you can also just have a text1 and text2 variable and just change that variable. When you want to, like so:

Code: Select all

screen msg_screen():
    frame:
        vbox:
            text "The first text is: [text1]"
            text "The second text is [text2]"

default text1 = "How are you today?"
default text2 = "Fine."
## The game starts here.

label start:
    "Your game starts. Let's show the screen"
    show screen msg_screen()
    "That is cool...now let's change the text!"
    $text1 = text2
    $text2 = "What are you up to?"
    "Look I changed the screen!"

    return
Hey trooper! I literally just looked through 3 threads with your answer on them XD This was helpful, but what I'm trying to do is more like show the same screen over and over one below the other, just with different text. I got it somewhat working in my initial code, but I thought there'd be a better way than posting blocks of vboxes one below the other. Thanks for the input, though. I imagine I'd be using your logic somewhere while making my screen. Thanks again! ~

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: How can I reduce the amount of code for this screen?

#8 Post by trooper6 »

You can reuse the same screen over and over again as well. That is also not a problem!

Just create a child screen, then use that child screen in the parent screen with the use command.

Here is some code from a tester I did with screens (it was my practice to go through the documentation and make a test project that did everything in the documentation to make sure I could use all of it. I didn't get very far...but I did get through the Screen page). This bit of code was testing the use command as well as tags.

Code: Select all

default friend_list = []
default enemy_list = []

screen upper_left(title, list):
    tag l
    frame: #window and fixed didn't work with the align
        align (0.0,0.0)
        use generic_screen(title, list) 
    
screen upper_right(title, list):
    tag r
    frame:
        align (1.0,0.0)
        use generic_screen(title, list)

screen lower_left(title, list):
    tag l
    frame:
        align (0.0,0.7)
        use generic_screen(title, list) 
    
screen lower_right(title, list):
    tag r
    frame:
        align (1.0,0.7)
        use generic_screen(title, list) 
    
screen generic_screen(title, list):
    frame:
        has vbox
        text "[title]"
        $i = len(list)
        for item in list:
            if i == 1:
                text ("[item]")
            else:
                text ("[item],")
            $i -= 1

label start:
    "So I want to experiment with using the use statement."
    "I've created four screens in the four corners. The left ones use the same tag as do the right ones."
    "I'm going to show the upper ones."
    show screen upper_left("Friends:", friend_list)
    "So now I'm trying a screen where I pass in a list and title that is in the Upper Left screen"    
    "I'm showng an empty friend list."
    $ friend_list.append("Stanley")
    "Just added Stanley"
    $ friend_list.append("Hedwig")
    "Just added Hedwig"
    $ friend_list.append("Connie")
    "Just added Connie"

    "Now I'm bringing up the upper right screen using the generic screen. Look I get to use generic screen twice!"    
    show screen upper_right("Enemies:", enemy_list)
    "I'm showng an empty enemy list."
    $ enemy_list.append("Muffy")
    "Just added Muffy"
    $ enemy_list.append("Brad")
    "Just added Brad"    
    
    "Okay, now I'm going to show the lower ones and see if the uppers ones disappear because of the tags"
    show screen lower_left("Friends:", friend_list)
    show screen lower_right("Enemies:", enemy_list)
    "So that worked."
    hide screen lower_left
    hide screen lower_right

    return
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

zeroTheHero
Regular
Posts: 64
Joined: Mon May 07, 2018 10:49 am
Contact:

Re: How can I reduce the amount of code for this screen?

#9 Post by zeroTheHero »

I'm back after almost a week of testing, and I must say, thanks for the detailed answers and the Telegram messenger link! It was super informative, it took me days to roughly understand it but now that I have, I feel like a genius who's climbed mount everest :lol: I've progressed quite a bit, but now....I've run into a problem. I'm almost at the finish line and I'd appreciate some help, I've been banging my head against this for the better part of today but am still stuck :cry:

What I want to do: Remember the 4 variables above I wanted to change without having to retype the entire screen code? I worked out how with one of them, I use the '%s %' thing in python and a function that does the work for me! Now, whenever I want to add a new name to a message I can go to script.rpy and type 'msg("any_name")' and voila, it's done!
The code for it:

Code: Select all

init python:
    style.txt_base = Style(style.default)
    def msg( personName, who=False):
        store.m_msg.append((who, personName))
        renpy.restart_interaction()
        renpy.pause()
   
screen s_name:
    frame background Bak:
        frame background None :   
            viewport:
                mousewheel True
                vbox spacing 15:
                    for message in m_msg:
                        $who, personName = message

                        $ xgn = 0.0 if who else 6.0
                        button xalign xgn xmaximum 580 xpadding 20 ypadding 10:
                            text "%s"%(personName)
The script.rpy looks like this:

Code: Select all

label start:
    show screen s_name
    $Bak="cafe (1).jpg"
    $m_msg = []
    $ msg_name = "sDextra"

    pause
    
    python:
        msg( "Pikachu",  who=True )
        msg("Charmander")
    return
However, errors arise when I try to build on it and add another line to display the textbox;

Code: Select all

		...
		vbox spacing 15:
                    for message in m_msg:
                        $who, personName, Tbox = message

                        $ xgn = 0.0 if who else 6.0
                        button xalign xgn xmaximum 580 xpadding 20 ypadding 10:
                            text "%s"%(personName)
                            
                            frame background "%s"%(Tbox)                            #<-----New Line, rest is same as above
I get the error 'Tbox is not defined'. In the code, 'who' and Tbox are defined exactly in the same way (in the very same lines), but while 'who' works Tbox gives me this error.
I tried defining '$ Tbox = None' or '= False' in the script, but that just gives me 'image None not found' on the screen. How can I get this working?

Post Reply

Who is online

Users browsing this forum: Ocelot