Page 1 of 1

Gallery for Custom Characters

Posted: Thu May 30, 2019 12:59 am
by cuterobot
I came up with this gallery code for a game with customizable characters. If you get a good ending, the character is added to the gallery. Feel free to modify/use it. I took the code to read tab-separated files from a few examples PyTom posted.

The game has six images to make up a character, an eye offset to match the eyes (and optional glasses) properly to the body, and a few text variables, for a total of eleven fields. The gallery displays each character in correct order for layering.

screens.rpy

In menu area:

Code: Select all

if renpy.loadable("gallery.txt"):
    textbutton _("Gallery") action ShowMenu("gallery")
On its own:

Code: Select all

init python:
    def parse_csv(filename):
        f = file(config.gamedir + "/" + filename)
        rv = [ ]

        for l in f:
             l = l[:-1]
             rv.append(l.split('\t'))

        f.close()
        return rv


gallery.rpy

Code: Select all

default gallery_current = 1

screen gallery:
    tag menu
    add "background.jpg"
    $ pictures = parse_csv("gallery.txt")
    $ gallery_last = len(pictures)
    $ gallery_first = 1

    $ aname = pictures[gallery_current - 1][0]
    $ alast = pictures[gallery_current - 1][1]
    $ job = pictures[gallery_current - 1][2]
    $ body = pictures[gallery_current - 1][3]
    $ eye_offset = int(pictures[gallery_current -1][4])
    $ normal = pictures[gallery_current -1][5]
    $ hair = pictures[gallery_current -1][6]
    $ bhair = pictures[gallery_current -1][7]
    $ suit = pictures[gallery_current -1][8]
    $ glasses = pictures[gallery_current -1][9]
    $ ending = pictures[gallery_current -1][10]

    add bhair at Position(xpos=300, ypos=0, xanchor=0, yanchor=0)
    add body at Position(xpos=300, ypos=0, xanchor=0, yanchor=0)
    add normal at Position(xpos=300, ypos=0, xanchor=0, yanchor=eye_offset)
    if glasses != "None":
        add glasses at Position(xpos=300, ypos=0, xanchor=0, yanchor=eye_offset)
    add suit at Position(xpos=300, ypos=0, xanchor=0, yanchor=0)
    add hair at Position(xpos=300, ypos=0, xanchor=0, yanchor=0)

    text "[aname]" xpos 720 ypos 210 size 50
    if alast != "None":
        text "[alast]" xpos 720 ypos 275 size 50
    text "[job]" xpos 720 ypos 345 size 40
    text "[ending]" xpos 720 ypos 400 size 40
    text "[gallery_current] / [gallery_last]" xalign 0.1 yalign 0.08 size 40

    if gallery_current > 1:
        textbutton "Previous":
            text_size 50
            action SetVariable("gallery_current", gallery_current - 1)
            xalign 0.1
            yalign 0.92

    if gallery_current < gallery_last:
        textbutton "Next":
            text_size 50
            action SetVariable("gallery_current", gallery_current + 1)
            xalign 0.9
            yalign 0.92

    textbutton "Return":
        text_size 50
        action Return()
        xalign 0.5
        yalign 0.92

script.rpy

just before the credits:

Code: Select all

if ending_bad == 0:
    $ output = str(aname) + "\t" + str(alast) + "\t" + str(job) + "\t" + str(body) + "\t" + str(eye_offset) + "\t" + str(normal) + "\t" + str(hair) + "\t" + str(bhair) + "\t"+ str(suit) + "\t" + str(glasses) + "\t" + str(ending) + "\n"
    python:
        with open(config.gamedir + '/' + 'gallery.txt', 'a') as gal:
            gal.write(output)
Example screenshot from game:
gall.png

Re: Gallery for Custom Characters

Posted: Wed Feb 26, 2020 2:39 am
by Godline
What happens when you get this error?

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/screens.rpy", line 1523, in execute
    screen gallery:
  File "game/screens.rpy", line 1523, in execute
    screen gallery:
  File "game/screens.rpy", line 1526, in execute
    $ pictures = parse_csv("gallery.txt")
  File "game/screens.rpy", line 1526, in <module>
    $ pictures = parse_csv("gallery.txt")
NameError: name 'parse_csv' is not defined
How do you fix it?