This page is out of date

You've reached a page on the Ren'Py wiki. Due to massive spam, the wiki hasn't been updated in over 5 years, and much of the information here is very out of date. We've kept it because some of it is of historic interest, but all the information relevant to modern versions of Ren'Py has been moved elsewhere.

Some places to look are:

Please do not create new links to this page.


Conditional Side Images

If you want your character to have a side image depending on a certain state variable, you can do it like this

   $ e = Character('Emmett',
          color="#c8ffc8",
          window_left_padding=160,
          show_side_image=ConditionSwitch(
          "express == 'serious'", "e_serious.png",
          "express == 'happy'", "e_happy.png",
          "express == 'right'", "e_right.png",
          "express == 'normal'", "e_normal",
        )
    )

Which would be okay if you're going to use only one character with conditional side image. As you start adding more characters, this will result in code repetition however.

A thin-wrapper around ConditionSwitch, using Python can solve this problem:

init python:
  def conditional_portrait(status_var, filename_prefix, states):
        args = []
        for s in states:
            args.append( "%s == '%s'" % (status_var, s) )
# The following line defines the template for your image files
            args.append( Image("%s_%s.png" % (filename_prefix, s)) )
        return ConditionSwitch(*args)


$ e = Character(
        'Emmett',
        color = "#c8ffc8",
        window_left_padding = 160,
        show_side_image = conditional_portrait("express", "e", ["serious", "happy", "right", "normal"])
      )

$ express = "normal"
e "...?"
$ express = "right"
e "Oh no!"

In fact, if you have a commonly used set of states, you can make it into a default argument

init python:
  def conditional_portrait(status_var, filename_prefix, states=["serious", "happy", "right", "normal"]):
    ...
    ...

Now you can create characters like this

$ e = Character(
        'Emmett',
        color = "#c8ffc8",
        window_left_padding = 160,
        show_side_image = conditional_portrait("express", "e") )

skipping the third parameter for conditional_portrait function. You can however override the default argument whenever you want, for example when a character needs a different set of states

$ s = Character(
        'Sakura',
        color = "#c8ffc8",
        window_left_padding = 160,
        show_side_image = conditional_portrait("sakura_express", "s", ["different", "set", "of", "states"])
)

Here we assumed that the filenames for portrait images are like "e_happy.png", "e_sad.png", but of your, you can alter this behavior of conditional_portrait function. Just modify the line

args.append( Image("%s_%s.png" % (filename_prefix, s)) )

for your needs. Here, "%s_%s.png" is the format string, the first %s will be replaced by the filename_prefix and the second one will be replaced by a state, such as happy or sad. If your filenames are like e-happy.jpg for instance, all you need to do is to modify that line to

args.append( Image("%s-%s.jpg" % (filename_prefix, s)) )

In fact, if you like, you can make it into a parameter for conditional_portrait function the as well, with a particular default value, the same way we did for states. Just be careful that the given format string has two %s flags.

  def conditional_portrait(status_var, filename_prefix,
        states=["serious", "happy", "right", "normal"], filename_format="%s_%s.png", **kwargs):
        args = []
        for s in states:
            args.append( "%s == '%s'" % (status_var, s) )
            args.append( Image(filename_format % (filename_prefix, s)) )
        return ConditionSwitch(*args, **kwargs)

It's possible to add an extra **kwargs as well, in case you want to pass extra arguments to ConditionSwitch, such as "xalign=0". You can also hard-code them into the function by modifying the ConditionSwitch call as well, without using kwargs, but it's more flexible this way.