layeredimage with variable = "not defined"?

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
Message
Author
BunnyInfernal
Regular
Posts: 34
Joined: Wed Dec 26, 2018 10:45 am
Contact:

layeredimage with variable = "not defined"?

#1 Post by BunnyInfernal »

Any suggestion on what I'm doing wrong here?

Code: Select all

    default avatar_directory = "mirror_full_2"

    layeredimage hair_front:
        if pc_hair_color == 1: ## brown
            "/images/avatar/[avatar_directory]/hair_[pc_hair_length]_brown.png"

        elif pc_hair_color == 2: ## blonde
            "/images/avatar/[avatar_directory]/hair_[pc_hair_length]_blonde.png"

        elif pc_hair_color == 3: ## black
            "/images/avatar/[avatar_directory]/hair_[pc_hair_length]_black.png"

        elif pc_hair_color == 4: ## red
            "/images/avatar/[avatar_directory]/hair_[pc_hair_length]_red.png"
I originally had the variable in an init block, but read that it should be "default" instead. Since making the change, however, I now get the error:

Code: Select all

NameError: name 'avatar_directory' is not defined

User avatar
PyTom
Ren'Py Creator
Posts: 16088
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: layeredimage with variable = "not defined"?

#2 Post by PyTom »

Please post the complete traceback, so I can figure this out.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

BunnyInfernal
Regular
Posts: 34
Joined: Wed Dec 26, 2018 10:45 am
Contact:

Re: layeredimage with variable = "not defined"?

#3 Post by BunnyInfernal »

Ah, I missed a section.

Code: Select all

    default avatar_directory = "mirror_full_2"

    layeredimage hair_front:
        if pc_hair_color == 1: ## brown
            "/images/avatar/[avatar_directory]/hair_[pc_hair_length]_brown.png"

        elif pc_hair_color == 2: ## blonde
            "/images/avatar/[avatar_directory]/hair_[pc_hair_length]_blonde.png"

        elif pc_hair_color == 3: ## black
            "/images/avatar/[avatar_directory]/hair_[pc_hair_length]_black.png"

        elif pc_hair_color == 4: ## red
            "/images/avatar/[avatar_directory]/hair_[pc_hair_length]_red.png"

        elif pc_hair_color == 5: ## green
            im.MatrixColor("/images/avatar/" + avatar_directory + "/hair_" + str(pc_hair_length) + "_brown.png", im.matrix.tint(0.8, 1.0, 0.8))
Here's the traceback:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/pics_avatar.rpy", line 232, in script
    layeredimage hair_front:
  File "game/pics_avatar.rpy", line 232, in script
    layeredimage hair_front:
  File "renpy/common/00layeredimage.rpy", line 794, in execute_layeredimage
    rai.execute()
  File "renpy/common/00layeredimage.rpy", line 786, in execute
    l.extend(i.execute())
  File "renpy/common/00layeredimage.rpy", line 435, in execute
    l.extend(i.execute())
  File "renpy/common/00layeredimage.rpy", line 396, in execute
    return [ Condition(self.condition, eval(self.image), **properties) ]
  File "game/pics_avatar.rpy", line 248, in <module>
    im.MatrixColor("/images/avatar/" + avatar_directory + "/hair_" + str(pc_hair_length) + "_brown.png", im.matrix.tint(0.8, 1.0, 0.8))
NameError: name 'avatar_directory' is not defined

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

Full traceback:
  File "renpy/bootstrap.py", line 326, in bootstrap
    renpy.main.main()
  File "renpy/main.py", line 515, in main
    renpy.game.context().run(node)
  File "game/pics_avatar.rpy", line 232, in script
    layeredimage hair_front:
  File "game/pics_avatar.rpy", line 232, in script
    layeredimage hair_front:
  File "renpy/ast.py", line 2009, in execute
    self.call("execute")
  File "renpy/ast.py", line 1997, in call
    return renpy.statements.call(method, parsed, *args, **kwargs)
  File "renpy/statements.py", line 278, in call
    return method(parsed, *args, **kwargs)
  File "renpy/common/00layeredimage.rpy", line 794, in execute_layeredimage
    rai.execute()
  File "renpy/common/00layeredimage.rpy", line 786, in execute
    l.extend(i.execute())
  File "renpy/common/00layeredimage.rpy", line 435, in execute
    l.extend(i.execute())
  File "renpy/common/00layeredimage.rpy", line 396, in execute
    return [ Condition(self.condition, eval(self.image), **properties) ]
  File "renpy/python.py", line 2249, in py_eval
    return py_eval_bytecode(code, globals, locals)
  File "renpy/python.py", line 2242, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "game/pics_avatar.rpy", line 248, in <module>
    im.MatrixColor("/images/avatar/" + avatar_directory + "/hair_" + str(pc_hair_length) + "_brown.png", im.matrix.tint(0.8, 1.0, 0.8))
NameError: name 'avatar_directory' is not defined

Windows-10-10.0.19041
Ren'Py 7.4.6.1693

User avatar
PyTom
Ren'Py Creator
Posts: 16088
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: layeredimage with variable = "not defined"?

#4 Post by PyTom »

Oh, so there's your problem. im.MatrixColor will be evaluated at init time, when the LayeredImage is defined. Since avatar_directory won't work there, you get a crash.

I'd suggest switching over to using a Transform with matrixcolor, and then using a dynamic image.

Code: Select all

Transform("/images/avatar/[avatar_directory]/hair_[pc_hair_length]_red.png", matrixcolor=TintMatrix(Color(rgb=(0.8, 1.0, 0.8))))
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

BunnyInfernal
Regular
Posts: 34
Joined: Wed Dec 26, 2018 10:45 am
Contact:

Re: layeredimage with variable = "not defined"?

#5 Post by BunnyInfernal »

Given the way I was doing it, I'm obviously unfamiliar... :lol:

Can I get an example of how to do it with a dynamic image? (Is this what we're talking about?)

My instinct was to do this:

Code: Select all

    default avatar_directory = "mirror_full_2"

    image hair_front_green = Transform("/images/avatar/[avatar_directory]/hair_[pc_hair_length]_red.png", matrixcolor=TintMatrix(Color(rgb=(0.8, 1.0, 0.8))))

    layeredimage hair_front:
        if pc_hair_color == 1: ## brown
            "/images/avatar/[avatar_directory]/hair_[pc_hair_length]_brown.png"

        elif pc_hair_color == 2: ## blonde
            "/images/avatar/[avatar_directory]/hair_[pc_hair_length]_blonde.png"

        elif pc_hair_color == 3: ## black
            "/images/avatar/[avatar_directory]/hair_[pc_hair_length]_black.png"

        elif pc_hair_color == 4: ## red
            "/images/avatar/[avatar_directory]/hair_[pc_hair_length]_red.png"

        elif pc_hair_color == 5: ## green
            hair_front_green
But that obviously doesn't work. :?

Code: Select all

NameError: name 'hair_front_green' is not defined

User avatar
emz911
Regular
Posts: 103
Joined: Fri Jun 23, 2017 2:23 pm
Contact:

Re: layeredimage with variable = "not defined"?

#6 Post by emz911 »

You need "hair_front_green" in quotes since it is defined as an image. Without quotes, it would look for a variable

Post Reply

Who is online

Users browsing this forum: No registered users