[SOLVED] Change character sprite depending on if the character is talking or not

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
User avatar
HeartsIntoMinds
Newbie
Posts: 7
Joined: Sun Mar 19, 2023 8:30 am
itch: heartsintominds
Contact:

[SOLVED] Change character sprite depending on if the character is talking or not

#1 Post by HeartsIntoMinds »

I currently have some character sprites with two separate ones for each emotion: one with the mouth open for when they are talking, and one with the mouth closed for when they're not talking. I'm looking to find a way to have Ren'Py automatically change these sprites for me instead of me manually inserting the code after every single line of dialogue between characters.

I'm currently running some auto highlight code by Wattson that highlights the sprite of whoever is talking, and there is a variable defined in there (speaking_char) that shows whoever is currently talking, so my plan was to use ConditionSwitch with that variable to have it switch back and forth between the talking and non-talking sprites, something like this:

Code: Select all

image chelsea neutral = ConditionSwitch(
"speaking_char = chelsea", "chelsea_neutral_talk.png",
"True", "chelsea_neutral.png"
)
Where I'm currently stuck is that as a part of Wattson's code I need to use the At command when defining the image, like so:

Code: Select all

image chelsea neutral = At('chelsea_neutral', sprite_highlight('chelsea'))
This is where I'm stuck at the moment as I'm unsure how to have both the ConditionSwitch and At commands in the same image definition. I'm still relatively new to Ren'Py so I'm a little stumped at the moment. If someone could please give me a hand with having both of these work together at the same time or any workarounds I can use I would be really appreciative, thanks.
Last edited by HeartsIntoMinds on Fri Mar 24, 2023 2:03 am, edited 1 time in total.

philat
Eileen-Class Veteran
Posts: 1926
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Change character sprite depending on if the character is talking or not

#2 Post by philat »

Haven't looked at the auto highlight code, but you could probably just stick the ConditionSwitch in the At().

Code: Select all

image chelsea_cs = ConditionSwitch(#stuff)
image chelsea neutral = At('chelsea_cs', sprite_highlight('chelsea'))

User avatar
HeartsIntoMinds
Newbie
Posts: 7
Joined: Sun Mar 19, 2023 8:30 am
itch: heartsintominds
Contact:

Re: Change character sprite depending on if the character is talking or not

#3 Post by HeartsIntoMinds »

I'm getting a syntax error regarding the ConditionSwitch, which is really weird because I'm using it exactly how the example in the documentation is coded.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 11, in script
    image trina_cs = ConditionSwitch(
  File "game/script.rpy", line 11, in script
    image trina_cs = ConditionSwitch(
  File "game/script.rpy", line 11, in <module>
    image trina_cs = ConditionSwitch(
SyntaxError: invalid syntax (<none>, line 1)

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

Full traceback:
  File "C:\Users\zero557\Desktop\Programs\renpy-8.0.3-sdk\renpy\bootstrap.py", line 277, in bootstrap
    renpy.main.main()
  File "C:\Users\zero557\Desktop\Programs\renpy-8.0.3-sdk\renpy\main.py", line 558, in main
    renpy.game.context().run(node)
  File "game/script.rpy", line 11, in script
    image trina_cs = ConditionSwitch(
  File "/home/tom/ab/renpy-build/tmp/install.linux-x86_64/lib/python3.9/site-packages/future/utils/__init__.py", line 441, in raise_
  File "game/script.rpy", line 11, in script
    image trina_cs = ConditionSwitch(
  File "C:\Users\zero557\Desktop\Programs\renpy-8.0.3-sdk\renpy\ast.py", line 1230, in execute
    img = renpy.python.py_eval_bytecode(self.code.bytecode)
  File "C:\Users\zero557\Desktop\Programs\renpy-8.0.3-sdk\renpy\python.py", line 1085, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "game/script.rpy", line 11, in <module>
    image trina_cs = ConditionSwitch(
  File "C:\Users\zero557\Desktop\Programs\renpy-8.0.3-sdk\renpy\display\layout.py", line 1652, in ConditionSwitch
    code = renpy.python.py_compile(cond, 'eval')
  File "C:\Users\zero557\Desktop\Programs\renpy-8.0.3-sdk\renpy\python.py", line 1027, in py_compile
    raise e
  File "C:\Users\zero557\Desktop\Programs\renpy-8.0.3-sdk\renpy\python.py", line 979, in py_compile
    raise orig_e
  File "C:\Users\zero557\Desktop\Programs\renpy-8.0.3-sdk\renpy\python.py", line 972, in py_compile
    tree = compile(source, filename, py_mode, ast.PyCF_ONLY_AST | flags, 1)
SyntaxError: invalid syntax (<none>, line 1)
Here's the code I've written, again I'm still very new to Ren'Py but I don't see what I'm doing wrong here. I've doublechecked that the spelling of the .png files is correct, and speaking_char is given a default value of 'None' in the auto callback script.

Code: Select all

define trina = Character("Trina", callback = name_callback, cb_name = "trina", color="#00a716")

image trina_cs = ConditionSwitch(
    "speaking_char = trina", "trina_neutral_talk.png",
    "True", "trina_neutral.png")
image trina neutral = At('trina_cs', sprite_highlight('trina'))
I'm hoping your idea works as the chelsea_neutral refers to the .png file itself rather than an image definition made in Ren'Py. That'd be pretty neat if i can refer to either a .png file or an image definition, because then in theory I could just nest the image definition a bunch of times and apply different effects to it each time.

philat
Eileen-Class Veteran
Posts: 1926
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Change character sprite depending on if the character is talking or not

#4 Post by philat »

"speaking_char = trina" should be "speaking_char == trina"

User avatar
HeartsIntoMinds
Newbie
Posts: 7
Joined: Sun Mar 19, 2023 8:30 am
itch: heartsintominds
Contact:

Re: Change character sprite depending on if the character is talking or not

#5 Post by HeartsIntoMinds »

Oh my goodness that's embarassing haha, totally slipped my mind to distinguish between using = for defining variables and == for checking them, thank you so much for spotting that.

The game does run now, though they're stuck on the non-talking sprites. I'm assuming this is because the images are only being defined once, the code is being re-run every time there is a new line of dialogue, so they're just stuck as the non-talking sprites.

I might be out of luck honestly, I can't use the callbacks because every character needs to have the same callback as part of the autohighlight script. I tried chucking all the image definitions in the callback, but it throws up another syntax error (I'm unsure as to if I can even use the image command in the autohighlight script as it's code that needs to be run on startup, hence the 00 at the start of the file name), plus it seems equally messy to need to redefine every single image for every single line of dialogue.

I've learned that I can at least change the sprite along with a new line of dialogue, like so:

Code: Select all

image trina neutral = At('trina_neutral', sprite_highlight('trina'))
image trina neutral_talk = At('trina_neutral_talk', sprite_highlight('trina'))

trina neutral_talk "Test line of dialogue."
show trina neutral
I'm happy with this for the most part, it's just that second line that will get tedious and bloat the script, because it needs to go after every line of dialogue. Are there any methods you know of that come to mind where I can also have that second line to be part of the first line of dialogue to simplify things? If not that's okay, you've already been a huge help and I appreciate it.

philat
Eileen-Class Veteran
Posts: 1926
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Change character sprite depending on if the character is talking or not

#6 Post by philat »

I bothered to look at the auto highlight code, and the condition should probably be "speaking_char == 'trina'" (note the use of single vs double quotes to preserve the string).

User avatar
HeartsIntoMinds
Newbie
Posts: 7
Joined: Sun Mar 19, 2023 8:30 am
itch: heartsintominds
Contact:

Re: Change character sprite depending on if the character is talking or not

#7 Post by HeartsIntoMinds »

Perfect, that's done it! It's working exactly as intended now. I had tried using " instead of ' before around the string, but I was unaware that they couldn't be used interchangeably so it naturally threw up an error when I attempted that. Absolute legend philat, thanks for helping a newbie out :D

Post Reply

Who is online

Users browsing this forum: No registered users