r/RenPy
A place for discussion about the Ren'Py visual novel engine and related topics and technologies.
•
A Short Posting Guide (or, how to get help)
Guide
How can I create a dynamic scrollbar in Ren'py without repeating the code?
Question
Hi everyone,
I'm new to Ren'py and I'm still learning how to use it. I'm currently trying to create a simple inventory system, and I want to add a scrollbar if there are more slots that can fit on the screen.
I have a conditional argument in my code that activates the scrollbar if there are more than 30 slots. However, I don't like repeating my code like this. Is there a better way to do this?
For example: if if slot_count > 30: draggable True mousewheel True scrollbars "vertical": Inventory code block. else: draggable False mousewheel False: The same inventory code block repeating again except the mouse will argument is falls.
Here is my code:
vbox: text "Inventory" xpos 5 ypos -5 # Conditional argument for activating and deactivating mouse scroll. if slot_count > 30: vpgrid cols 7 spacing 5 draggable True mousewheel True scrollbars "vertical": for slot in range(slot_count): frame: maximum(155, 155) if slot < len(inventory): background Image("images/inventory/inventory_hud/slot_bg.png") add Image("images/inventory/" + inventory[slot] + ".png", $ inv_item_name = inventory[slot].replace('_', ' ') text inv_item_name style style["inv_item_name"] else: background Image("images/inventory/inventory_hud/slot_bg.png") else: vpgrid cols 7 spacing 5 draggable False mousewheel False: for slot in range(slot_count): frame: maximum(155, 155) if slot < len(inventory): background Image("images/inventory/inventory_hud/slot_bg.png") add Image("images/inventory/" + inventory[slot] + ".png", $ inv_item_name = inventory[slot].replace('_', ' ') text inv_item_name style style["inv_item_name"] else: background Image("images/inventory/inventory_hud/slot_bg.png")