Skip to content

Instantly share code, notes, and snippets.

@Lezalith
Last active September 25, 2022 04:43
Show Gist options
  • Save Lezalith/f6ea469f10f10d21f53c2c5f592cd12f to your computer and use it in GitHub Desktop.
Save Lezalith/f6ea469f10f10d21f53c2c5f592cd12f to your computer and use it in GitHub Desktop.
Originally by methanoliver, a displayable that loops text shown with cps. Great for text speed preview in preferences! Everything explained at https://www.lezcave.com/text-speed-preview/
init -10 python:
class LoopingSlowText(renpy.Displayable):
# text is a string, text to be displayed.
# loop_time is float, delay after the text finishes showing before the loop repeats.
# cps is either int or None. cps of the Text, if None, it is taken from preferences.text_cps.
def __init__(self, text, loop_time = 2.0, cps = None, **properties):
# Juicy renpy.Displayable stuff.
super(LoopingSlowText, self).__init__()
# Because dividing by zero is not nice.
if loop_time <= 0:
raise Exception("loop_time of LoopingSlowText must be larger than 0.")
# Time before the loop repeats after the text is done showing.
self.loop_time = loop_time
# Whether to use cps given or from preferences.
self.cps = cps
# Store original arguments for recreating the Text child later
self.original_text = text
self.original_properties = properties
# Counts how many loops have elapsed.
# This is to check whether a new child should be created.
self.current_count = 0
# Text displayable that represents LoopingSlowText.
self.current_child = self.new_text()
# Creates a new Text child based on the text and properties given to __init__,
# and the current cps taken from preferences.
# Creating a new child resets the slow_cps property.
def new_text(self):
# Use self.cps, unless it is None, in which case use preferences.text_cps
cps = (preferences.text_cps if self.cps == None else self.cps)
return Text(self.original_text, slow_cps = cps, **self.original_properties)
# Triggered on events or renpy.redraw call.
def render(self, width, height, st, at):
# Trigger this function again when possible,
# to test and/or update all of this stuff again.
renpy.redraw(self, 0)
# Use self.cps, unless it is None, in which case use preferences.text_cps
cps = (preferences.text_cps if self.cps == None else self.cps)
# How long it should take for the text to finish showing given the current cps.
text_time = len(self.original_text) / cps
# How long one whole loop takes.
# This means (how long the text takes to show + delay before repeat)
time_of_one_loop = text_time + self.loop_time
# Check if a new child should be created.
if int(st / time_of_one_loop) > self.current_count:
# Increment the count.
self.current_count = int(st / time_of_one_loop)
# Create a new child.
self.current_child = self.new_text()
# We fool the animation timebase, by dividing time by our loop time
# and keeping the remainder, so that every time the loop time would be
# reached, animation goes back in time.
st = st % time_of_one_loop
at = at % time_of_one_loop
# Create a render (canvas).
render = renpy.Render(width, height)
# Place the Text child onto it, with the fooled st and at.
render.place(self.current_child, st = st, at = at)
# Return the render.
return render
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment