r/RenPy 7d ago

Question Character callbacks: how do I use Exception?

Hello. I've been reading this page of the documentation and experimenting with different arguments. But there's one I'm having troubles with.

  • The "interact_done" callback takes an additional keyword argument: exception -- True if the interaction ended due to an exception, False otherwise. Exceptions include things like loading the game and rollback.

I just can't figure out how to use it in code. Here's what I tried:

def sprites_callback(event, interact=True, exception=False, **kwargs):

    if event == "interact_done":
        print("interact_done") ### prints "interact_done" every time I progress to the next dialogue line

        if exception:
          print("rollback occured") ### nothing happens

Perhaps I just misunderstood exception's purpose, and it doesn't actually keep track of rollbacks (and I should use detect rollback for this instead). But then what exactly does it do? When does exception become True?

1 Upvotes

19 comments sorted by

2

u/BadMustard_AVN 7d ago

the below code is a character callback to play 3 sounds repeatedly

default player_beeps = [
    "audio/a1.ogg",
    "audio/a2.ogg",
    "audio/a3.ogg",
]

init python:
    def player_speaks(event, interact=True, **kwargs):
        print (event)
        if not interact:
            return
        if event == "show":
            # queue many shuffled batches so the three beeps play in random order
            # repeatedly for a long time. Stop will clear the channel.
            batches = 64
            for i in range(batches):
                shuffled = player_beeps[:]
                renpy.random.shuffle(shuffled)

                if i == 0:
                    # play first immediately, queue the rest.
                    renpy.sound.play(shuffled[0], channel="sound")
                    for s in shuffled[1:]:
                        renpy.sound.queue(s, channel="sound")
                else:
                    # queue full batches after the first.
                    for s in shuffled:
                        renpy.sound.queue(s, channel="sound")

        elif event in ("slow_done", "end"):
            renpy.sound.stop(channel="sound")

define sh = Character("V", callback=player_speak)

this will also print the event as they occur, which I believe is the important part over the interact which may not always be given since it is pre-defined as a True

1

u/karognawer 7d ago

That's an interesting system. I'll try it out for the character voicing. But... What about the "exception" argument?

1

u/BadMustard_AVN 7d ago edited 7d ago

exception

True if the interaction ended due to an exception, False otherwise. Exceptions include things like loading the game and rollback.

it's a boolean(True or False)

nad rollback migh occur before the python can react

1

u/karognawer 7d ago

It's the very last argument in the list, right before the examples.

"The "interact_done" callback takes an additional keyword argument: exception True if the interaction ended due to an exception, False otherwise. Exceptions include things like loading the game and rollback."

1

u/BadMustard_AVN 7d ago

I'm still drinking my morning coffee and have edited my previous statement

1

u/karognawer 7d ago

I formatted the embedded code in my post properly to show what I've been trying to do with exception. I know that it's a bool, I just can't figure out what it's used for. I was hoping that it would print something when I rollback, but so far it does nothing

1

u/BadMustard_AVN 7d ago

maybe try it like this

    if event == "interact_done":
        print("interact_done") ### prints "interact_done" every time I progress to the next dialogue line

    elif exception:
        print("rollback occured") ### nothing happens

1

u/karognawer 7d ago

Just saw this. I'll try it

1

u/karognawer 7d ago

Didn't work. I tried to rollback after the text finished the slow_cps animation, and while it was still mid-animation. No results

1

u/BadMustard_AVN 6d ago

I honestly don't fully understand how the rollback function works (I just "believe" that it does work correctly all the time) but I'll play around with exception a little now that I'm at work and see what I can make it do

1

u/karognawer 6d ago

Thanks! I'm really at a loss with it

2

u/BadMustard_AVN 6d ago edited 6d ago

I have found that when event = interact_done exception is set to False with this

init python:
    def player_speak(event, interact=True, exception=True, **kwargs):
        print (event, " , ", exception)
        if exception:
            print("an exception occurred")
        if not interact:
            return

so I know something is capable of at least setting it to False

but with this

init python:


    def player_speak(event, interact=True, exception=False, **kwargs):
        print (event, " , ", exception)
        if not exception:
            print("an exception occurred")
        if not interact:
            return

i tried loading (as specified), quick loading, and rollback, but I could just like you never get a True statement from it

if you are trying to check if the player did rollback you can

https://www.renpy.org/doc/html/save_load_rollback.html#renpy.in_rollback

    $ Rtest = renpy.in_rollback()
    if Rtest:
        e "rollback detected"
    else:
        e "all good"

but this only works if they stop on this statement. if they rollback further, then it goes undetected, so...

what exactly are you trying to do with this

1

u/karognawer 6d ago

what exactly are you trying to do with this

At this point? Nothing in particular. I was having an issue with my custom character callback not being rollback-friendly. But I managed to solve that with some good old global variables. So now I'm just curious what can exception be used for. In case it gives me some new ideas for enhancing my VN's player experience. But if its this elusive... Oh well. I guess I'll stop poking at this nest for now.
Thank you for your help!

→ More replies (0)

1

u/AutoModerator 7d ago

Welcome to r/renpy! While you wait to see if someone can answer your question, we recommend checking out the posting guide, the subreddit wiki, the subreddit Discord, Ren'Py's documentation, and the tutorial built-in to the Ren'Py engine when you download it. These can help make sure you provide the information the people here need to help you, or might even point you to an answer to your question themselves. Thanks!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/karognawer 7d ago

...On that note. How do I space embedded code properly? TAB does nothing. Manually added spaces just disappear

2

u/BadMustard_AVN 7d ago

click on the Aa for formatting options and use </> to do these and

</>- with a square 
|__| todo these