Having set up a similar type of search system for an educational app I'm working on in RenPy, here are a couple insights that I've found helpful.
1. The screen refreshes with every event. So when the user clicks on something or types something into the box a refresh occurs.
2. Conditional logic can still be put into screen code. In the case of IF statements, raw Python code
does work in most cases, assuming the functions were properly defined in advance in a
python: or
init python: block.
Based on this, the process I use is to create a library that acts as a registry for things I intend to be searchable. Something like the following:
Code: Select all
init python:
import math
# Define our story list here
stories = [
{
"title": _("Test Script{#title_for_story_list}"),
"prefix": "test_script",
"summary": _("Temporary Script for Feature Testing{#description_for_story_list}"),
"image": "_test_script.png"
},
{
"title": _("This Dumb Programmer{#title_for_story_list}"),
"prefix": "dumb_programmer",
"summary": _("Pardon our dust...{#description_for_story_list}"),
"image": "_dumb_programmer.png"
},
{
"title": _("Effects Test{#title_for_story_list}"),
"prefix": "effects_test",
"summary": _("Temporary Script for Effects Testing{#description_for_story_list}"),
"image": "_effects_test.png"
},
}
story_grid_rows = math.ceil(len(stories) / gui.file_slot_cols)
story_grid_cells = math.ceil(story_grid_rows * gui.file_slot_cols)
The grid cell rows/columns calculations at the bottom are useful if you're using a grid view, since it (...rather inconveniently) wants to know up front how many cells to expect (you'll also have to add dummy cells to the end to pad the final row to the proper length).
Back to the meat of things though, in your case you may want to add a
"character": "Jason{#character_name_for_search}" type of field to each registry entry. From there when building the cells showing things you can put the code building it behind an if statement. Something like the following:
Code: Select all
$ current_grid_cells = story_grid_cells ## Creating a copy so we can adjust the number of anticipated cells during a search
for i in range(current_grid_cells):
if (i < len(stories)):
if (stories[i]["character"] == search_term):
button:
action Start(stories[i]["prefix"] + "_start") ## Start telling story from Label in string
has vbox
add im.Scale("images/title_cards/" + stories[i]["image"], config.thumbnail_width, config.thumbnail_height) xalign 0.5
text stories[i]["title"]:
style "slot_time_text"
text stories[i]["summary"]:
style "slot_name_text"
else: ## Story not about the character you're searching for, skip it
$ current_grid_cells = current_grid_cells + 1 ## Add a dummy button to the end so that the count lines up
else: ## Create an empty button
button:
has vbox
add im.Scale("images/title_cards/_title_card_placeholder.jpg", config.thumbnail_width, config.thumbnail_height) xalign 0.5
text "":
style "slot_time_text"
text "":
style "slot_name_text"
If you want to be even more slick and not have a ton of empty cells at the end you could do a quick check before the FOR loop where you build the grid and count the number of stories that meet the criteria in advance; then you wouldn't need to worry about the first ELSE branch at all.
One final thought: Using buttons (image or text) to have people choose which character to search for will save you a
world of headaches. Users mistype things and capitalize things differently, and if you get into translating it then they may search for different versions of the character's name. It'd be better to have a button they can click on to set the
search_term used in the filter, which you can then programmatically ensure matches with the possible values in the registry. With some light tweaking on this you can even allow people to do things like tap on multiple buttons to find stories featuring all of the chosen characters (e.g. by storing the selections in a set and then ensuring that the story registry entry contains all of the characters in the set).
Using some of the tricks here you can also use screen variables to show and hide pieces of the screen when building things so that, for example, you can have a "Show Filters" button that displays the buttons, along with another to hide them.
Anyway...I know that's a whole lot to drop, so I'll leave it there for now. Hope it helps!