Best way to style text conditionally?

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
Obscura
Eileen-Class Veteran
Posts: 1431
Joined: Fri Mar 09, 2012 6:58 pm
Projects: Coming Out On Top
Location: United States
Contact:

Best way to style text conditionally?

#1 Post by Obscura »

Let's say you want to ask the player a lot of questions. Each of the menu choices will be colored differently depending on whether or not the player has agreed for help.

Code: Select all

menu:	
  "Do you want my help?"
  "Yes":
    $help=True
  "No":
    $help=False


menu:
  "How do you feel about this issue?"
  "Good.": #this word will be in GREEN text if help==True, otherwise default black
    pass    
  "Bad.": #this word will be in RED text if help==True, otherwise default black
    pass 
  "So so.": #this word will be in YELLOW text if help==True, otherwise default black
    pass 
	 	
menu:
  "What's your favorite animal?"
    "Lion.": #this word will be in YELLOW text if help==True, otherwise default black
      pass
    "Cat.": #this word will be in GREEN text if help==True, otherwise default black
      pass
    "Moose.": #this word will be in RED text if help==True, otherwise default black
      pass 

#and so forth for several questions using Green, Red, Yellow options
What can be done to make this work? I can't find a way to make the inline style optional.
I also thought I might be able to define a style and change its color depending on the value of "help", but it doesn't seem like you can make styles optional in that way.
Would registering a style preference work better in this case? But if so, what would you be registering exactly since it's the default text?
Coming Out On Top - An Adult Gay Dating Sim
website

User avatar
IrinaLazareva
Veteran
Posts: 399
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy
Organization: SunShI
Location: St.Petersburg, Russia
Contact:

Re: Best way to style text conditionally?

#2 Post by IrinaLazareva »

to continue to develop idea
viewtopic.php?f=8&t=25278#p310660
viewtopic.php?f=8&t=38655&p=448822#p448822

Code: Select all

default mhelp = False
screen choice(items):
    style_prefix "choice"
    vbox:
        for i in items:
            if "(g)" in i.caption:
                textbutton i.caption.replace("(g)", "") action i.action:
                    if mhelp:
                        text_color '#0f0'
            elif "(r)" in i.caption:
                textbutton i.caption.replace("(r)", "") action i.action:
                    if mhelp:
                        text_color '#f00'      
            elif "(y)" in i.caption:
                textbutton i.caption.replace("(y)", "") action i.action:
                    if mhelp:
                        text_color '#fe0'         
            else:
                textbutton i.caption action i.action         #<< default


label start:
    menu:
        "Do you want my help?"
        "Yes":
            $ mhelp=True
        "No":
            $ mhelp=False
    menu:
        "How do you feel about this issue?"
        "Good.(g)": #this word will be in GREEN text if help==True, otherwise default black
            pass    
        "Bad.(r)": #this word will be in RED text if help==True, otherwise default black
            pass 
        "So so.(y)": #this word will be in YELLOW text if help==True, otherwise default black
            pass 
    menu:
        "What's your favorite animal?"
        "Lion.(y)": #this word will be in YELLOW text if help==True, otherwise default black
            pass
        "Cat.(g)": #this word will be in GREEN text if help==True, otherwise default black
            pass
        "Moose(r).": #this word will be in RED text if help==True, otherwise default black
            pass 
    return

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Best way to style text conditionally?

#3 Post by Remix »

The most recent nightlies (we are talking a few days back) have extra features for menu blocks, allowing:

Code: Select all

menu:
    "Choice 1" (1, color="#f00"):
        pass
    "Choice 2" (2, color="#0ff") if unlocked:
        pass
which presumable passes the parameters through to the item in the choice.

I've not had time to try this new feature yet, though believe it also allows passing arbitrary parameters on the menu: line which get passed to the screen
Frameworks & Scriptlets:

User avatar
Obscura
Eileen-Class Veteran
Posts: 1431
Joined: Fri Mar 09, 2012 6:58 pm
Projects: Coming Out On Top
Location: United States
Contact:

Re: Best way to style text conditionally?

#4 Post by Obscura »

Thanks very much, IrinaLazareva! I'm not sure if I'll be able to use that since I already have multiple menus and am not sure how I'd integrate this into each one of them. But that's an extremely useful code to know for the future. :)

@Remix, thanks for the heads up. That looks like a great new feature.
Coming Out On Top - An Adult Gay Dating Sim
website

Post Reply

Who is online

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