Programatically added hotspots on an image map
Posted: Mon Oct 31, 2022 7:44 am
I am looking forward to a better way to describe and add hotspots to an image map. I have a screen that defines for a basic Point&Click behavior, where I can add a location python object as a parameter. This object defines every aspect of the screen so I can reuse this screen all over my game.
Here is the screen's snippet:
I have python classes for the Location and for the Hotspots:
When I define a scene I do it in python like so:
My problem is that I'm not t satisfied with this framework. Sometimes I need to 'hide" hotspots where the player is not allowed to click yet. Like Bar, I have a visible member, where I write python statements in string format. I evaluate this in the Hotspot's is_visible method: eval(self.visible). It works but eval is slow and I feel like I missing out on some feature of from Ren'Py.
Here is the screen's snippet:
Code: Select all
screen pointnclick_screen(location):
tag pointnclick
layer "master"
zorder 0
imagemap:
ground [location.name]
for i,spot in enumerate(location.spots):
if spot.is_visible():
imagebutton:
pos spot.position
idle spot.sprite
focus_mask spot.sprite
action Jump(spot.target)
Code: Select all
class Location:
def __init__(self, name, spots):
self.name = name
self.spots = spots
class Hotspot:
def __init__(self, position, sprite, target = None, visible = None):
self.position = position
self.sprite = sprite
self.target = target
self.visible = visible
def is_visible(self):
if self.visible:
return eval(self.visible)
else:
return True
Code: Select all
define my_scene = Location("bg myscene",
[
# Foo
Hotspot((2918, 1027),
sprite = "sprite foo",
target = "ask_foo"),
# Bar
Hotspot((6370, 957),
sprite = "sprite bar",
target = "goto_bar",
visible = "not need_to_ask_foo"),
]
- I can't write the variable name as is, because - according to the compiler - the variable is not known at that point, and because sometimes I need to add a "not" or make combined/complex conditional statements.
- I know I can't use lambdas
- I played with renpy.curry(), but messed it up.
- Maybe my whole python approach is wrong
- The pointnclick_screen is more complex (I pasted here only the relevant parts), including frames, keymaps, and control buttons, so I really want to reuse it rather than create one for every scene.