Question: Define generated variable to show images

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Posts: 2
Joined: Wed Jul 29, 2020 12:02 pm

Question: Define generated variable to show images

#1 Post by PlantMan » Wed Jul 29, 2020 3:07 pm

I have images of trees (examples: apple, orange) and their parts specifically, similarly named.

I coded a random generator that can combine and create image names. From the generated name I want to display the image on screen. But I cannot define the variable to show the image.
The code that doesn't work is default cat = "%(theplant)s". It just shows %(theplant)s rather than apple.jpg for example. If I define the variable and image directly with default cat = "apple.jpg" it works.
Or must one define every single image used within the code?
Joke: Or am I comparing apples and oranges here?

Questions, opinions, and help appreciated. Thank you for taking the time to look at this, and have a nice day!

Code: Select all

label start:
    #randomly selects a tree
    $ randplant = renpy.random.choice(['apple'])
    #randomly selects a part of the tree, blank for entire tree
    $ randpart = renpy.random.choice(['','_flower','_leaf','_fruit','_seed'])
    #displays the first selection, apple being the only option.
    #displays the second selection, one of 5 options.
    #Combines the two random selections and adds .jpg file extension.
    $theplant=  randplant+randpart+".jpg"
    #display the results of the combined items and file extention
    #jump to the next part
    jump table

label table:
    "breakcheck4" #test

    #default cat = "apple.jpg"  ###When active###
                                ###and when next line deativated###
                                ###The code works###

    default cat = "%(theplant)s"    ###This is the problem###
                                    ###I can't set default to "%(theplant)s"###
                                    ###At least not correctly####

    "[cat]" #displays as  %(plant)s  literally rather than the x_x.jpg
    "breakcheck5" #test
    image work = "[cat]"
    "breakcheck6" #test
    show work at topright
    "breakcheck7" #test

label quit:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 61, in script
    show work at topright
Exception: DynamicImage u'[cat]': could not find image. (u'%(theplant)s')

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 61, in script
    show work at topright
  File "I:\Games\renpy-7.3.5-sdk\renpy\", line 1183, in execute
    show_imspec(self.imspec, atl=getattr(self, "atl", None))
  File "I:\Games\renpy-7.3.5-sdk\renpy\", line 1153, in show_imspec
  File "I:\Games\renpy-7.3.5-sdk\renpy\", line 717, in show
    sls.add(layer, img, key, zorder, behind, at_list=at_list, name=name, atl=atl, default_transform=default_transform, transient=transient)
  File "I:\Games\renpy-7.3.5-sdk\renpy\display\", line 1077, in add
  File "I:\Games\renpy-7.3.5-sdk\renpy\display\", line 677, in set_transform_event
  File "I:\Games\renpy-7.3.5-sdk\renpy\display\", line 488, in set_transform_event
  File "I:\Games\renpy-7.3.5-sdk\renpy\display\", line 717, in set_transform_event
  File "I:\Games\renpy-7.3.5-sdk\renpy\display\", line 651, in find_target
    raise Exception(error)
Exception: DynamicImage u'[cat]': could not find image. (u'%(theplant)s')

ImageVariable 1.0
Wed Jul 29 14:03:42 2020
The last line even explains it defines %(theplant)s literally rather than using apple.jpg. So I am doing it wrong.

Thanks again!

User avatar
Posts: 31
Joined: Sun Aug 12, 2018 8:32 am
itch: dgameboy101b

Re: Question: Define generated variable to show images

#2 Post by dGameBoy101b » Fri Aug 07, 2020 10:13 am

The default statement is executed during load time which happens before the start label is reached so the variable substitution cannot be performed as the variable isn't defined yet. ... -statement
So to fix this you can just use a regular python assignment instead of a default statement.

Code: Select all

label table:
    $ cat = theplant
    image work = "[cat]"

Post Reply

Who is online

Users browsing this forum: No registered users