How to pass variables in action Show("screen") ?

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
Pippin123
Regular
Posts: 52
Joined: Sat Jan 31, 2015 7:33 pm
Contact:

How to pass variables in action Show("screen") ?

#1 Post by Pippin123 »

I have a screen called
LaserShot (x,y)

In another screen, there's an imagebutton that's supposed to call the first screen when clicked.
so the syntax is something like

Code: Select all

action Show("LaserShot")
Except I don't know how to pass the x,y variables.

I tried

Code: Select all

action (Show("LaserShot",(x,y))
, but no luck.

Anyone know the correct way to do this ?
Thanks

User avatar
PyTom
Ren'Py Creator
Posts: 16096
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: How to pass variables in action Show("screen") ?

#2 Post by PyTom »

You have to pass them as named arguments.

Code: Select all

action Show("LaserShot", x=x, y=y)
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

User avatar
Kaji
Regular
Posts: 87
Joined: Thu Nov 17, 2022 10:17 pm
Github: Kaji01
Discord: Kaji#7767
Contact:

Re: How to pass variables in action Show("screen") ?

#3 Post by Kaji »

Found this while trying to do the same sort of thing this evening and was concerned because I was getting TypeError notices when trying it out, and I wasn't sure if I was seeing anything about this in the Screen Actions documentation or not.

Was just about to post a question about it when I noticed one last thing to try out—and it started working.

TL;DR: If adding the named arguments isn't working for you, make sure that the screen definition includes those arguments! Once I added the parameters to my screen it started working.

=> This will fail and produce a TypeError because the screen doesn't know what to do with the values:

Code: Select all

screen item_entry():

...

action Show("item_entry", dissolve, selected_entry=selected_entry)
=> This will work because now it knows what to do with the value you're sending over:

Code: Select all

screen item_entry(selected_entry):

...

action Show("item_entry", dissolve, selected_entry=selected_entry)
Last edited by Kaji on Tue Jan 24, 2023 10:49 am, edited 1 time in total.

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1000
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: How to pass variables in action Show("screen") ?

#4 Post by m_from_space »

Kaji wrote: Thu Jan 19, 2023 8:20 pm Found this while trying to do the same sort of thing this evening and was concerned because I was getting TypeError notices when trying it out, and I wasn't sure if I was seeing anything about this in the Screen Actions documentation or not.
Actually, you can just simply pass the arguments without keywords, if you remind yourself, that the second argument has to be the transition that is used when showing the screen. In the case you don't want to pass the transition argument, you can just use keyword arguments of course. There is also a mistake in your example.

So let's assume we have a screen:

Code: Select all

screen sumtext(x, y):
    text str(x+y)
You can either do:

Code: Select all

# let's use normal arguments
Show("sumtext", None, 3, 4)
or

Code: Select all

# let's use keyword arguments
Show("sumtext", x=3, y=4)

Post Reply

Who is online

Users browsing this forum: Semrush [Bot]