I'm trying to set up a screen displayable that, when shown, will display text that is bound to the mouse cursor. I've created a custom displayable based on Apricotorange's flashlight tutorial (viewtopic.php?f=51&t=20872) that takes a displayable as an argument and (re)draws it at the mouse position whenever the mouse is moved. Since it needs a displayable and not just a string I am using the Text() function to convert it.
It works great when I pass it a normal text displayable. However I plan to use it to display info on an item when moused over in the inventory. This means I need it to display variables. The problem happens when I pass Text() a string with a variable included (using square brackets) that needs interpolation. I get the exception: KeyError: u'item'. The variable I am trying to interpolate is an Item object named item. I'm therefore assuming the problem is that interpolation doesn’t happen until inside the Text() function and it can no longer reference the object "item".
I have tried a bunch of different workarounds but nothing I do seems to fix the core problem of having to use Text(). Is there a way to force renpy to interpolate first and then pass the string? Or should I perhaps be taking a different approach for this? Thanks!
Screen code:
Code: Select all
screen tooltip(item=False):
if item:
default txt = "[item[0].name]: [item[0].desc] (Value: [item[0].value])"
default txt2 = "Test Test Test"
hbox:
xalign 0.5 yalign 1.0
text "[item[0].name]: [item[0].desc] (Value: [item[0].value])"
add MouseSnap(Text(txt2))
#add MouseSnap("[item[0].name]: [item[0].desc] (Value: [item[0].value])", item)
#add Text(txt)
#add MouseSnap(Text("[item[0].name]: [item[0].desc] (Value: [item[0].value])"))
Edit: They try to do similar things, except for the text line that simply displays the correct text but does not bind it to the mouse.
Custom displayable code:
Code: Select all
class MouseSnap(renpy.Displayable):
def __init__(self, dis):
super(MouseSnap, self).__init__()
# Displayable to snap to mouse
self.child = dis
# Start outside game window
self.pos = (-1, -1)
def render(self, width, height, st, at):
render = renpy.Render(config.screen_width, config.screen_height)
# If we are outside the window render nothing
if self.pos == (-1, -1):
return render
# Render the displayable
child_render = renpy.render(self.child, width, height, st, at)
# Draw the image centered on the cursor.
x, y = self.pos
x += 25
y += 25
render.blit(child_render, (x, y))
return render
def event(self, ev, x, y, st):
# Re-render if the position changed.
if self.pos != (x, y):
renpy.redraw(self, 0)
# Update stored position
self.pos = (x, y)
def visit(self):
return [ self.child ]