[Solved] Correct Ren'py way to trigger hover state of other displayables when one dislpayable is hovered?

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
henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

[Solved] Correct Ren'py way to trigger hover state of other displayables when one dislpayable is hovered?

#1 Post by henvu50 »

Let's say you have four image buttons, and you want the hover state of all of them to trigger when one imagebutton is hovered. Is using a boolean variable, hovered and unhovered the best way to achieve this? Like this?

Code: Select all

# if any of the displayables are hovered, set this variable to true, if unhovered, set this variable to false
default anyButtonsHovered= False

   screen test():

    imagebutton: 
        # give the illusion that this is hovered by showing the hover image as idle
        if anyButtonsHovered: 
            idle "1_hover.png"
        else:
            idle "1_idle.png" # show normal idle 
        hover "1_hover.png"
        hovered SetVariable("anyButtonsHovered", True)
        unhovered SetVariable("anyButtonsHovered", False)
        action NullAction()

    textbutton "Text":
        hovered SetVariable("anyButtonsHovered", True)
        unhovered SetVariable("anyButtonsHovered", False)
        if anyButtonsHovered:
            text_outlines [ (3, "#000000", 0, 0) ] #give the illusion that this is hovered by showing outlines for the text
        else:
            text_outlines [ (0, "#000000", 0, 0) ]
        action NullAction()

    imagebutton: 
        if anyButtonsHovered:
            # give the illusion that this is hovered
            idle "1_hover.png" 
        else:
            idle "1_idle.png"
        hover "1_hover.png"
        hovered SetVariable("anyButtonsHovered", True)
        unhovered SetVariable("anyButtonsHovered", False)
        action NullAction()
        
I was wondering if there was a better way to do this?
Last edited by henvu50 on Fri Jul 16, 2021 5:16 pm, edited 1 time in total.

User avatar
Alex
Lemma-Class Veteran
Posts: 3090
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Correct Ren'py way to trigger hover state of other displayables when one dislpayable is hovered?

#2 Post by Alex »

The question is why do you need this?
Hovered state is really means that button is hovered by mouse pointer or selected via gamepad or keyboard. Normally, you unable to click two not overlapped buttons by single mouse pointer... So, you want to use this feature for something that it's not suited for, that's why it looks buggy/inconvenient.

Also, there's a 'selected' state for the button - this might be the thing you need.
Here's some useful information about button's behavior - viewtopic.php?f=8&t=26492#p322596

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: Correct Ren'py way to trigger hover state of other displayables when one dislpayable is hovered?

#3 Post by henvu50 »

Alex wrote: Thu Jul 15, 2021 5:25 pm The question is why do you need this?
In my game you can travel from room to room. You can do this by either clicking the image in the room, or clicking another image in a navigation wheel that represents that room. So there's two imagebuttons that do the same thing. I want both of their hover state to trigger if either one is hovered.
Hovered state is really means that button is hovered by mouse pointer or selected via gamepad or keyboard. Normally, you unable to click two not overlapped buttons by single mouse pointer... So, you want to use this feature for something that it's not suited for, that's why it looks buggy/inconvenient.

Also, there's a 'selected' state for the button - this might be the thing you need.
Here's some useful information about button's behavior - viewtopic.php?f=8&t=26492#p322596
Hmmm, the latter solution could work. i've worked with that method actually. Hmmm good thinking. I'll try that. It would be nice if there was an easy way to just control any displayable's state with one line of python code.

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: Correct Ren'py way to trigger hover state of other displayables when one dislpayable is hovered?

#4 Post by henvu50 »

I ended up relying on flags to control the hover state of other displayables.

Okay, one more thing.

In a point and click game, you have interactables (npc's or objects) in the scene. I want to create an action that will trigger the hover state of a certain group of displayables, by id or tag. When the user presses this button, they can see what items can be interacted with in the scene.

This would require code like this:

Code: Select all

#PSEUDO CODE
key "r" action ToggleVariable(renpy.get_displayables_by_id("group_interactables").hover)
So with one line, you can select a group of displayables and just set their hover state to true. I bet renpy can do stuff like that, but I can't figure out how to do it.

User avatar
Alex
Lemma-Class Veteran
Posts: 3090
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Correct Ren'py way to trigger hover state of other displayables when one dislpayable is hovered?

#5 Post by Alex »

henvu50 wrote: Fri Jul 16, 2021 2:03 pm ...So with one line, you can select a group of displayables and just set their hover state to true. I bet renpy can do stuff like that...
You can use variables to set selected state of your displayables. So, if several displayables uses the same variable they all will be set as selected.
When you add displayables to the screen you can set what variable they will use (so you don't need to get the id). This might be a property of your displayables that set for one of those variables, but this depends of how you've designed them.
Sample

Code: Select all

default flag_1 = False
default flag_2 = False

screen test_selected_scr():
    vbox:
        spacing 5
        align(0.5, 0.05)
        textbutton "Text 1" action NullAction() selected flag_1
        textbutton "Text 2" action NullAction() selected flag_2
        textbutton "Text 3" action NullAction() selected flag_1
        textbutton "Text 4" action NullAction() selected flag_2
        null
        textbutton "Select even" action ToggleVariable('flag_2', True, False)
        textbutton "Select odd" action ToggleVariable('flag_1', True, False)

# The game starts here.
label start:
    "..."
    show screen test_selected_scr
    "?!"

Post Reply

Who is online

Users browsing this forum: Google [Bot]