Is it possible to highlight some text when hovering over button?

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
OrsonDeWitt
Regular
Posts: 45
Joined: Thu Dec 06, 2018 3:23 am
Contact:

Is it possible to highlight some text when hovering over button?

#1 Post by OrsonDeWitt »

Hi!
So the subject does not explain it in full.
There are 10 variables in my game. When a player hovers over a choice button, I would want the player to see which variable from the HUD his or her choice is going to affect. The thing is that each choice has different variable that it affects, and many times this choice is affecting more than one variable. How would one go about making this work? Thank you!
I cannot reply to messages anymore.
> Your IP has been blocked because it is blacklisted.

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

Re: Is it possible to highlight some text when hovering over button?

#2 Post by philat »

I don't know what kind of hover effect you want, but in principle, you would use a boolean variable to control whether the text is "hovered" or not. Then use the button hovered/unhovered actions to control the boolean variable.

OrsonDeWitt
Regular
Posts: 45
Joined: Thu Dec 06, 2018 3:23 am
Contact:

Re: Is it possible to highlight some text when hovering over button?

#3 Post by OrsonDeWitt »

True, but how would I call these booleans from inside a label?
Say, this is the dialogue:
menu:
yes: on hover boolean STAT1
no: on hover boolean STAT2 & STAT3

I would need to somehow have custom choice buttons that I can call from from within labels, each representing the right boolean(s)
I cannot reply to messages anymore.
> Your IP has been blocked because it is blacklisted.

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

Re: Is it possible to highlight some text when hovering over button?

#4 Post by philat »

Well yes. There are any number of ways to do so, but I suppose one of the the easier ones is to use menu arguments. https://www.renpy.org/doc/html/menus.ht ... -arguments

OrsonDeWitt
Regular
Posts: 45
Joined: Thu Dec 06, 2018 3:23 am
Contact:

Re: Is it possible to highlight some text when hovering over button?

#5 Post by OrsonDeWitt »

Thanks a lot! Didn't think it would have been built in into renpy already.
The documentation doesn't explain much, though, so I have no clue how to actually work with it. Say, I need s_1 to have 1 added to it and s_2 to have 2 subtracted when a choice is made, how should the code look like? Would appreciate your help!
I cannot reply to messages anymore.
> Your IP has been blocked because it is blacklisted.

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: Is it possible to highlight some text when hovering over button?

#6 Post by hell_oh_world »

menu arguments should work, but you need to modify the choice screen in screen.rpy to make it work...

Code: Select all

## Choice screen ###############################################################
##
## This screen is used to display the in-game choices presented by the menu
## statement. The one parameter, items, is a list of objects, each with caption
## and action fields.
##
## https://www.renpy.org/doc/html/screen_special.html#choice

default var = None # take note of the name of the variable

screen choice(items):
    style_prefix "choice"

    vbox:
        for i in items:
            textbutton i.caption action i.action:
              hovered SetVariable("var", i.kwargs.get("var")) # added this line and the line below...
              unhovered SetVariable("var", None)

default var_1 = 0
default var_2 = 0

screen the_vars(): # depends on where you're showing the text telling what var is hovered...
  text "Var_1: {}".format(var_1):
    if var == "var_1":
      color "#f00" # colors this red if the var is hovered...
  
  text "Var_2: {}".format(var_2):
    if var == "var_2":
      color "#f00"

label start:
  show the_vars

  menu choose:
    "Var 1" (var="var_1"):
      $ var_1 += 1
    "Var 2" (var="var_2"):
      $ var_2 += 1
      
  jump choose
since you have 10 variables you say... i think it's good to take note of the name of the variable or a number (var = 0 / if var == 0 / etc...) as an identifier to tell which is hovered or what, boolean only lets you choose between two values true and false so it won't work if you have more than 2 variables like you said.

using objects or containing the variables inside iterables (dicts, list) would also make the process easier...
using dictionaries would be...

Code: Select all

default myvars = dict(
  var1=dict(val=0, hovered=False), # var1 and var2 are the supposed to be names of the variables
  var2=dict(val=0, hovered=False), # add more below if needed...
)

## you can also use this code in creating the dictionary.
# default varnames = ["str", "int", "agi",] # list here the names of the vars...
# default myvars = {vn: dict(val=0, hovered=False) for vn in varnames} # loops instead to create the dict, to access do... myvars["str"]["val"] += 1

screen choice(items):
    style_prefix "choice"

    vbox:
        for i in items:
            textbutton i.caption action i.action:
              if i.kwargs.get("var"):
                hovered SetDict(myvars[i.kwargs["var"]], "hovered", True)
                unhovered SetDict(myvars[i.kwargs["var"]], "hovered", False)

screen the_vars():
  vbox:
    for vn, vv in myvars.items(): # we loop in the dict instead of typing one by one...
      hbox:
        text "{} value: {}".format(vn, vv["val"])
        text "hovered: {}".format(vv["hovered"]) 

label start:
  show the_vars

  menu choose:
    "Var 1" (var="var1"): # pass the var name as declared in the dict myvars
      $ myvars["var1"]["val"] += 1
    "Var 2" (var="var2"):
      $ myvars["var2"]["val"] += 1
      
  jump choose

OrsonDeWitt
Regular
Posts: 45
Joined: Thu Dec 06, 2018 3:23 am
Contact:

Re: Is it possible to highlight some text when hovering over button?

#7 Post by OrsonDeWitt »

hell_oh_world, thanks a lot for your help! Unfortunately it doesn't do anything on hover...
Here's the code I've tried in my choices screen:

Code: Select all

default var = None
default agriculture = 0
...
text "agriculture: {}".format(agriculture):
    if var == "var_1":
    color "#f00" # colors this red if the var is hovered...
...
 vbox:
        style "choicebox"
        for i in items:
            textbutton i.caption:
                style "choicebutton" action i.action
                hovered SetVariable("var", i.kwargs.get("var")) # added this line and the line below...
                unhovered SetVariable("var", None)
                  
Here's the dialogue:

Code: Select all

    menu:
            "Yes" (var="var_1"):
            "No" (var="var_2"):
EDIT:
fixed it! "default var = None" shouldn't have been inside a screen but outside of it. It works now, thank you very much, hell_oh_world!!
I cannot reply to messages anymore.
> Your IP has been blocked because it is blacklisted.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], myur, peach_light