Cycle and preview fonts in game.

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Cycle and preview fonts in game.

#1 Post by henvu50 »

Instead of changing the font filename every time in code, then tabbing back into Ren'py waiting for it to refresh, now you can just press hotkeys in game to cycle through and preview fonts from a specified font folder.

Example of use:
1. Create a fonts folder in your game directory, and put all fonts in there.
2. Add this code to the screen main_menu() like this, or anywhere else you want to preview a font with your game's text.

Code: Select all

# screens.rpy

########## [1/4] ADD THIS BLOCK ###########
init python:
    import re
    # this gets a dir list of files
    def file_list(dir=""):
        list = renpy.list_files()
        rv = []
        for f in list:
            if re.match(dir,f):
                rv.append(f[(len(dir)):])
        return rv
########## /ADD THIS BLOCK ###########	

########## [2/4] ADD THIS VARIABLE ###########
default cycleFontIndex  = 0 

screen main_menu():
    
    ################ [3/4] ADD THIS BLOCK ############
    # access all fonts in the fonts folder in the game directory
    $ files = file_list("fonts")
    # hotkeys that you can change if you want that change index value
    key "i" action SetVariable('cycleFontIndex', cycleFontIndex+1)
    key "u" action SetVariable('cycleFontIndex', cycleFontIndex-1)
    vbox:
        # debug to verify the index is working and font files are successfully accessed
        text str(cycleFontIndex)
        text str(files[cycleFontIndex])
    ##################/ADD THIS BLOCK ##################

    if gui.show_name:
        vbox:
            style "main_menu_vbox"
            text "[config.name!t]":
                font "fonts" + files[cycleFontIndex]    ########## [4/4] ADD THIS FONT STYLE ###########
                style "main_menu_title"
3. Now press the u and i hotkeys at the main menu to preview all the fonts for your main menu logo. This code can be used anywhere in game to preview any text with a style.

Keep in mind, putting this part in a style does not work.

Code: Select all

font "fonts" + files[cycleFontIndex]
Instead you have to put it in the button's properties like this:

Code: Select all

textbutton "Test":
    text_font "fonts" + files[cycleFontIndex]
    action NullAction()

Post Reply

Who is online

Users browsing this forum: No registered users