[Solved] How to make hovering a choice box change a variable?

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
Asarus
Newbie
Posts: 15
Joined: Sun Oct 22, 2017 10:12 am
Projects: Bullwhip
Contact:

[Solved] How to make hovering a choice box change a variable?

#1 Post by Asarus »

I'm trying to make a choice screen where there's a big protagonist figure that slides in and who will change facial expressions when you hover over the different choices. This way, I'm trying to give the player a better feel about what "mood" each choice has.

Image

I've done it the simplest way I could think of. I made the big protagonist figure a character (named "chopro") with several layers of graphics, and I show him before starting a choice sequence. To change his expression, I need to change the variable that controls his face layer ("choice_exp"). Right now, the value of this variable is "choice_attentive", an attentive face. If I want a choice to make the protagonist figure smile, the variable's value needs to change to "choice_delighted". The figure should then immediately update with its new expression. But how can I make hovering a choice button alter the variable "choice_exp" to the value "choice_delighted"? And can I change the variable once more, back to "choice_attentive" when the choice button is no longer hovered?

Somehow, I need to attach the text string "choice_delighted" to a choice, preferably without this actually being contained in the text of the choice box. Then, I need to make hovering the choice alter the variable "choice_exp" to this string, and reverse it when the button is unhovered. Does anyone have any suggestions how I might pull this off?

This is how the protagonist figure's graphics is defined:

Code: Select all

#Choice screen protagonist figure
# Character definition
define chopro = Character('Choice', color="#f8f1e5", who_suffix = '', what_prefix='"', what_suffix='"')
# Sprite composition
default choice_background = "choice_background" #darkens everything behind the choice screen
default choice_overlay = "choice_overlay" #the blue and yellow stripes behind the protagonist figure
default choice_body = "choice_body" #protagonist figure's headless body
default choice_exp = "choice_attentive" #protagonist figure's head with facial expression
default choice_eff = "choice_none" #an extra layer where I can add effects like bruises, blushes, etc.
image chopro = LiveComposite(
    (1920, 1080),
    (0, 0), "images/char/choice/[choice_background].png",
    (0, 0), "images/char/choice/[choice_overlay].png",
    (0, 0), "images/char/choice/[choice_exp].png",
    (0, 0), "images/char/choice/[choice_body].png",
    (0, 0), "images/char/choice/[choice_eff].png"
    )
This is how it looks in the script when I run a choice screen:

Code: Select all

window hide
show chopro at left with easeinleft
menu:
    "Disbelieve it":
        hide chopro with easeoutleft
        pro "Don't be silly, it's a bird."
    "Pretend not to notice":
        hide chopro with easeoutleft
        pro "What? Where? There's nothing."
    "Dude, that's totally a UFO":
        hide chopro with easeoutleft
        pro "DON'T ABDUCT ME!"
This is what my choice screen in screens.rpy looks like:

Code: Select all

## Choice screen

screen choice(items):
    style_prefix "choice"

    vbox:
        for i in items:
            textbutton i.caption action i.action

## When this is true, menu captions will be spoken by the narrator. When false,
## menu captions will be displayed as empty buttons.
define config.narrator_menu = True

style choice_vbox is vbox
style choice_button is button
style choice_button_text is button_text

style choice_vbox:
    xalign 0.5
    ypos 405
    yanchor 0.5

    spacing gui.choice_spacing

style choice_button is default:
    properties gui.button_properties("choice_button")
    hover_sound "sfx/click.wav" #This makes the button click when it is hovered
    #activate_sound "sfx/click.wav" #This makes the button click when you click it

style choice_button_text is default:
    properties gui.button_text_properties("choice_button")
Last edited by Asarus on Wed Jul 04, 2018 3:32 am, edited 1 time in total.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1461
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Discord: xavimat
Contact:

Re: How to make hovering a choice box change a variable?

#2 Post by xavimat »

I suggest you to create your own screen, instead of using a menu. You can do it the other way, changing the screen choice, but a custom screen will be clearer.
Tell us if you need help to create your own screen.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
Asarus
Newbie
Posts: 15
Joined: Sun Oct 22, 2017 10:12 am
Projects: Bullwhip
Contact:

Re: How to make hovering a choice box change a variable?

#3 Post by Asarus »

xavimat wrote: Thu Jun 28, 2018 4:32 pm I suggest you to create your own screen, instead of using a menu. You can do it the other way, changing the screen choice, but a custom screen will be clearer.
Tell us if you need help to create your own screen.
Ah, yep, I've cheated and made it as simple as possible because I couldn't figure out how to edit the choice screen or make an entirely new one. Not yet, anyway. My abilities with page creation are still sadly limited. I can't quite grasp in which parts of the code I can do what. If someone is willing to help me build such a screen, I would be very, very grateful.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1461
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Discord: xavimat
Contact:

Re: How to make hovering a choice box change a variable?

#4 Post by xavimat »

Ok. First advice, maybe you know this already but, to be sure:
Renpy uses actually different languages in label, screen or transforms.
Labels hold the narration as it goes on, and have a flow and an order, they can jump/call/return.
Screens are defined on their own, and showed or called from labels when needed.

To create a screen, put the definition of the screen in any ".rpy" file inside the game folder (or any subfolder). Renpy will find it.

Code: Select all

screen my_test_screen():
    vbox:
        text "Hey! I am a text"
        textbutton "Close me!" action Return()
This code does not do anything. Renpy will prepare a screen with a vbox (vertical box) and two elements in it (a text and a textbutton). But renpy will not show that until you ask it nicely.
In a label, you can call the screen:

Code: Select all

label start:
    scene black with None
    call screen my_test_screen
    return
In this code, we ask renpy to show the screen and wait for a return (that's the meaning of "call screen"). If the screen had no Return() or other means of going on, the game would be stuck.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

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: How to make hovering a choice box change a variable?

#5 Post by Remix »

Personally I'd go with altering screen choice in screens.rpy so you can run the system using menu: blocks as normal.

If you add a prefix to the caption, you can then test for that and add a hovered ...

Code: Select all

default choice_exp = "choice_attentive"

label start:

    menu:

        "attentive¦choice a":

            "mmm"

        "delighted¦choice b":

            "mmm"

    "..."

# in screens.rpy


screen choice(items):

    style_prefix "choice"

    vbox:
        
        text "[choice_exp]" ### remove this, just here to show change

        for i in items:

            if "¦" in i.caption:

                $ caption_parts = i.caption.split("¦")

                textbutton caption_parts[1]:

                    hovered SetVariable("choice_exp", "choice_{}".format(caption_parts[0]))

                    action i.action 

            else:

                textbutton i.caption action i.action
I used broken vertical bars as a caption separator (ctrl-alt-backtick on UK keyboard) as it is rare to use elsewhere btw.
Frameworks & Scriptlets:

User avatar
Asarus
Newbie
Posts: 15
Joined: Sun Oct 22, 2017 10:12 am
Projects: Bullwhip
Contact:

Re: How to make hovering a choice box change a variable?

#6 Post by Asarus »

xavimat wrote: Thu Jun 28, 2018 6:55 pm Ok. First advice, maybe you know this already but, to be sure:
Renpy uses actually different languages in label, screen or transforms.
Labels hold the narration as it goes on, and have a flow and an order, they can jump/call/return.
Screens are defined on their own, and showed or called from labels when needed.

To create a screen, put the definition of the screen in any ".rpy" file inside the game folder (or any subfolder). Renpy will find it.

Code: Select all

screen my_test_screen():
    vbox:
        text "Hey! I am a text"
        textbutton "Close me!" action Return()
This code does not do anything. Renpy will prepare a screen with a vbox (vertical box) and two elements in it (a text and a textbutton). But renpy will not show that until you ask it nicely.
In a label, you can call the screen:

Code: Select all

label start:
    scene black with None
    call screen my_test_screen
    return
In this code, we ask renpy to show the screen and wait for a return (that's the meaning of "call screen"). If the screen had no Return() or other means of going on, the game would be stuck.
Thank you for the kind explanation, xavimat! I know somewhat well by now the label/narration language. The screens are definitely the most difficult because they are so technical.

I created the example screen you've given me here. Yep, it gets called from the script, and shows a black page with a text and a text button that brings you out from the screen. So, I now know how to create and call a simple screen, which is good! All right. From this screen, I can find no way to bring my protagonist figure in, because I assume the language of showing a character isn't the language of screens. I can't use the show or the add commands to bring in his LiveComposite image, so I guess he needs to be assembled in a different way.

I could use the add command to put each image layer on top of each other, though it doesn't feel right, as if this is a very rigid way of doing it. I could add them as imagebuttons instead, because they seem more flexible, but I wouldn't know exactly what to do with them once they're there. Do you have a suggestion?
Last edited by Asarus on Sat Jun 30, 2018 6:08 pm, edited 1 time in total.

User avatar
Asarus
Newbie
Posts: 15
Joined: Sun Oct 22, 2017 10:12 am
Projects: Bullwhip
Contact:

Re: How to make hovering a choice box change a variable?

#7 Post by Asarus »

Remix wrote: Thu Jun 28, 2018 8:49 pm Personally I'd go with altering screen choice in screens.rpy so you can run the system using menu: blocks as normal.

If you add a prefix to the caption, you can then test for that and add a hovered ...

Code: Select all

default choice_exp = "choice_attentive"

label start:

    menu:

        "attentive¦choice a":

            "mmm"

        "delighted¦choice b":

            "mmm"

    "..."

# in screens.rpy


screen choice(items):

    style_prefix "choice"

    vbox:
        
        text "[choice_exp]" ### remove this, just here to show change

        for i in items:

            if "¦" in i.caption:

                $ caption_parts = i.caption.split("¦")

                textbutton caption_parts[1]:

                    hovered SetVariable("choice_exp", "choice_{}".format(caption_parts[0]))

                    action i.action 

            else:

                textbutton i.caption action i.action
I used broken vertical bars as a caption separator (ctrl-alt-backtick on UK keyboard) as it is rare to use elsewhere btw.
Hey, you know. This worked. *single tear of joy rolls off chin*

Thank you for showing me this. I now understand a bit more about how to extract the captions from the choice buttons and the potential for processing them and making them useful like this. Also, the split command is great. I've made good use of this in other programming languages before.

Thanks so much for helping me, Remix :D

Post Reply

Who is online

Users browsing this forum: No registered users