Changing Transform with Variable

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
paulyboyx
Newbie
Posts: 11
Joined: Thu Mar 10, 2022 1:20 am
Contact:

Changing Transform with Variable

#1 Post by paulyboyx »

I've been knocking my head against this problem for a few hours now. I'm trying to change the "on hide" animation depending whether a variable is true or false:

Code: Select all

default reject = False
transform giftGiven:
    xalign .30 yalign .45
    on hide:
        linear 0.5 xoffset (-200 if reject else 200) alpha 0.0 #got the "else" from the Renpy subreddit, which often gives inaccurate code

#...

show gift at giftGiven
$reject = True
hide gift 
The "gift" image here should move off to the left (xoffset -200) when hidden (since the variable "reject" is True).

Instead, when hidden, the "gift" image moves off to the right (xoffset 200). I assume this is because when I first showed the image, the variable "reject" was False. But I want the "on hide" animation to change appropriately when the variable has changed-- which it currently isn't doing.

I've tried putting conditional lines in the transform:

Code: Select all

transform giftGiven:
    xalign .30 yalign .45
    if reject == True:
        linear 0.5 offset -200
    elif reject == False:
        linear 0.5 offset 200
Which throws an error, because apparently you can't mix Python with transform language.

I've tried writing a function statement, but I can't make sense of the documentation.

My brain is fried. Please send help.

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

Re: Changing Transform with Variable

#2 Post by philat »

You can simply show the image again to update, but I would probably parameterize it to be clearer.

Code: Select all

transform test_transform(direction=True):
    # direction: True for right (accepted), False for left (rejected)
    xalign 0.5 yalign 0.5
    on hide:
        linear 0.5 xoffset (200 if direction else -200)

label start:
    $ rejected = True
    show test_image at test_transform(rejected)
    e "You've created a new Ren'Py game."
    $ rejected = False
    show test_image at test_transform(rejected)
    hide test_image
    e "Once you add a story, pictures, and music, you can release it to the world!"

paulyboyx
Newbie
Posts: 11
Joined: Thu Mar 10, 2022 1:20 am
Contact:

Re: Changing Transform with Variable

#3 Post by paulyboyx »

Thanks so much! I didn't think to show the image again, and now it works like a charm.

Post Reply

Who is online

Users browsing this forum: Google [Bot]