[SOLVED] Trouble with hovered animation for choice buttons

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.
Message
Author
User avatar
hell_oh_world
Regular
Posts: 175
Joined: Fri Jul 12, 2019 5:21 am
Projects: Pathfinding
Organization: NILA
Contact:

Re: Trouble with hovered animation for choice buttons

#16 Post by hell_oh_world » Mon Oct 07, 2019 3:47 pm

Kinmoku wrote:
Mon Oct 07, 2019 11:03 am
So this works but all menu choices hover/ highlight at the same time. Otherwise, it's working just how I want it to.
Oops... Kinda forgot that there were many buttons and that using only one variable will trigger the other. Try this instead.

Code: Select all

screen choice(items):
    style_prefix "choice"
    default hovered_button  = "" ## Create a variable that will store a somewhat unique identifier for each buttons
    
    frame:
        xsize None
        ysize None
        xpadding 20
        ypadding 20
        xalign 0.5
        yalign 0.4
        background Frame("gui/menu_frame.png", 44, 44)
            
        vbox:
            yalign 0.41
            
            $ choice_number = 1 # This is the unique identifier that will be stored in the hovered_button variable.
            for i in items:
                if " (grey)" in i.caption:
                    button:
                        action i.action
                        style "menu_choice_grey_button"
                        text i.caption.replace(" (grey)", "") style "menu_choice_grey_text"

                else:
                    button:
                        xysize (575, 80)
                        action i.action                        
                        hovered SetScreenVariable("hovered_button", "choice_" + choice_number)
                        unhovered SetScreenVariable("hovered_button", "")
                        
                        showif hovered_button == "choice_" + choice_number:
                            add "gui/menuchoice.png" at choicehover
                        else:
                            add "gui/menuchoice_blank.png"
                            
                        text i.caption xalign 0.5 yalign 0.5 style "choice_button_text"
                $ choice_number += 1

User avatar
Kinmoku
Veteran
Posts: 447
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: Love IRL, Memories
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

Re: Trouble with hovered animation for choice buttons

#17 Post by Kinmoku » Tue Oct 08, 2019 3:20 am

hell_oh_world wrote:
Mon Oct 07, 2019 3:47 pm
Oops... Kinda forgot that there were many buttons and that using only one variable will trigger the other. Try this instead.
I tried this but I get an error:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 5120, in script
    menu:
  File "game/screens.rpy", line 1071, in execute
    screen choice(items):
  File "game/screens.rpy", line 1071, in execute
    screen choice(items):
  File "game/screens.rpy", line 1077, in execute
    frame:
  File "game/screens.rpy", line 1086, in execute
    vbox:
  File "game/screens.rpy", line 1090, in execute
    for i in items:
  File "game/screens.rpy", line 1091, in execute
    if " (grey)" in i.caption:
  File "game/screens.rpy", line 1099, in execute
    button:
  File "game/screens.rpy", line 1099, in keywords
    button:
TypeError: coercing to Unicode: need string or buffer, int found

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 5120, in script
    menu:
  File "/Applications/renpy-6.99.11-sdk/renpy/ast.py", line 1626, in execute
    choice = renpy.exports.menu(choices, self.set, args, kwargs, item_arguments)
  File "/Applications/renpy-6.99.11-sdk/renpy/exports.py", line 983, in menu
    rv = renpy.store.menu(new_items)
  File "/Applications/renpy-6.99.11-sdk/renpy/exports.py", line 1217, in display_menu
    rv = renpy.ui.interact(mouse='menu', type=type, roll_forward=roll_forward)
  File "/Applications/renpy-6.99.11-sdk/renpy/ui.py", line 295, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "/Applications/renpy-6.99.11-sdk/renpy/display/core.py", line 2699, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, **kwargs)
  File "/Applications/renpy-6.99.11-sdk/renpy/display/core.py", line 3091, in interact_core
    root_widget.visit_all(lambda i : i.per_interact())
  File "/Applications/renpy-6.99.11-sdk/renpy/display/core.py", line 541, in visit_all
    d.visit_all(callback, seen)
  File "/Applications/renpy-6.99.11-sdk/renpy/display/core.py", line 541, in visit_all
    d.visit_all(callback, seen)
  File "/Applications/renpy-6.99.11-sdk/renpy/display/core.py", line 541, in visit_all
    d.visit_all(callback, seen)
  File "/Applications/renpy-6.99.11-sdk/renpy/display/screen.py", line 430, in visit_all
    callback(self)
  File "/Applications/renpy-6.99.11-sdk/renpy/display/core.py", line 3091, in <lambda>
    root_widget.visit_all(lambda i : i.per_interact())
  File "/Applications/renpy-6.99.11-sdk/renpy/display/screen.py", line 440, in per_interact
    self.update()
  File "/Applications/renpy-6.99.11-sdk/renpy/display/screen.py", line 625, in update
    self.screen.function(**self.scope)
  File "game/screens.rpy", line 1071, in execute
    screen choice(items):
  File "game/screens.rpy", line 1071, in execute
    screen choice(items):
  File "game/screens.rpy", line 1077, in execute
    frame:
  File "game/screens.rpy", line 1086, in execute
    vbox:
  File "game/screens.rpy", line 1090, in execute
    for i in items:
  File "game/screens.rpy", line 1091, in execute
    if " (grey)" in i.caption:
  File "game/screens.rpy", line 1099, in execute
    button:
  File "game/screens.rpy", line 1099, in keywords
    button:
  File "<screen language>", line 1102, in <module>
TypeError: coercing to Unicode: need string or buffer, int found

Darwin-17.7.0-x86_64-i386-64bit
Ren'Py 7.3.2.320
My Friend Katie 1.0
Tue Oct  8 09:16:30 2019

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

Re: Trouble with hovered animation for choice buttons

#18 Post by philat » Tue Oct 08, 2019 3:54 am

Limited to the error itself: you need str() around choice_number since you're trying to concatenate a string and an integer.

User avatar
Kinmoku
Veteran
Posts: 447
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: Love IRL, Memories
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

Re: Trouble with hovered animation for choice buttons

#19 Post by Kinmoku » Tue Oct 08, 2019 4:06 am

philat wrote:
Tue Oct 08, 2019 3:54 am
Limited to the error itself: you need str() around choice_number since you're trying to concatenate a string and an integer.
Ah fantastic! That's it working perfectly :) Thank you both for your help.

In case anyone wants to know the final code, it's here:

Code: Select all

screen choice(items):
    style_prefix "choice"
    default hovered_button = "" #is_showable  = False
    
    frame:
        xsize None
        ysize None
        xpadding 20
        ypadding 20
        xalign 0.5
        yalign 0.4
        background Frame("gui/menu_frame.png", 44, 44)
            
        vbox:
            yalign 0.41
            $ choice_number = 1
            
            for i in items:
                if " (grey)" in i.caption:
                    button:
                        action i.action
                        style "menu_choice_grey_button"
                        text i.caption.replace(" (grey)", "") style "menu_choice_grey_text"

                else:
                    button:
                        xysize (575, 80)
                        action i.action                        
                        hovered SetScreenVariable("hovered_button", "choice_" + str(choice_number)) ## toggle the flag
                        unhovered SetScreenVariable("hovered_button", "")
                        
                        showif hovered_button == "choice_" + str(choice_number): ## the showif statement that will trigger the event
                            add "gui/menuchoice.png" at choicehover
                        else:
                            add "gui/menuchoice_blank.png"
                            
                        text i.caption xalign 0.5 yalign 0.5 style "choice_button_text"
                        
                $ choice_number += 1
In script.rpy:

Code: Select all

image menuchoice = "gui/menuchoice.png"
image menuchoice_blank = "gui/menuchoice_blank.png"
image hover_anim:
    contains choicehover

transform choicehover:
    on show:
        crop_relative True
        alpha 0 crop (0,0, 0.0, 1.0)
        linear 0.15 crop (0,0, 0.3, 1.0)
        linear 0.25 alpha 1.0 crop(0,0, 1.0, 1.0 )
        
    on hide:
        crop_relative True
        alpha 1.0 crop (0,0, 1.0, 1.0)
        linear 0.25 crop (0,0, 0.3, 1.0)
        linear 0.15 alpha 0.0 crop(0,0, 0.0, 1.0 )


User avatar
hell_oh_world
Regular
Posts: 175
Joined: Fri Jul 12, 2019 5:21 am
Projects: Pathfinding
Organization: NILA
Contact:

Re: Trouble with hovered animation for choice buttons

#20 Post by hell_oh_world » Tue Oct 08, 2019 5:22 am

philat wrote:
Tue Oct 08, 2019 3:54 am
Limited to the error itself: you need str() around choice_number since you're trying to concatenate a string and an integer.
I totally forgot that it needs type casting, unlike Java that will convert any numerical value into string once it has been concatenated with a string. Nice! Thanks for correcting that out. Glad his/her problem is solved.

Post Reply

Who is online

Users browsing this forum: No registered users