Search code examples
pythonrenpy

Ren-py: Why doesn't this work?


I want the images to be loaded depending on a variable "ImgVal"

$ ImgVal = []
$ image bob = "b_neutral_%s.png" % ImgVal
$ image bob_sad = "b_sad_%s.png" % ImgVal
$ image bob_happy = "b_happy_%s.png" % ImgVal

Solution

  • The value passed by the % operator has to be of type string (or a dictionary, if you use %(val_name)s instead of %s).

    For example:

    ImgVal = 1
    # some calculation to evaluate Imgval
    print "b_neutral_%s.png" % str(ImgVal) # cast ImgVal from int to str
    

    Note that if you want to use a array you'll need some construct like this:

    ImgVal = [1, 2, 3, 4]
    for i in ImgVal:
        print "b_neutral_%s.png" % str(i)
    

    Edit:

    I've never worked with renpy, but looking in the documentation about using python, you are using the $ sign incorrectly, you should only use it for raw python code. The % operator is python only, therefore you'll have to use it and evaluate the string, then pass it to renpy. This could probably work:

    $ ImgVal = "h1c1"
    $ img_str = "b_neutral_%s.png" % ImgVal
    image bob = img_str
    $ img_str = "b_sad_%s.png" % ImgVal
    image bob_sad = img_str
    $ img_str = "b_happy_%s.png" % ImgVal
    image bob_happy = img_str
    

    The only thing I can tell you for sure is that you were using $ incorrectly and that your string formatting was wrong in your first example.