[Solved] Call python function through textbutton

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
Michael Regal
Newbie
Posts: 11
Joined: Fri Mar 25, 2022 11:38 pm
Contact:

[Solved] Call python function through textbutton

#1 Post by Michael Regal » Fri Apr 01, 2022 7:25 pm

Please help. This might be a simple solution but I'm having problems. I have this:

Code: Select all

$ player_inv.take(potion)
which is a function inside a class here:

Code: Select all

        def take(self, item, qty=1):
            if self.qty(item):
                item_location = self.check(item)
                self.inv[item_location][1] += qty
            else:
                self.inv.append([item,qty])

But I want to call it from a textbutton. How do I do this? :(

Please help!
Last edited by Michael Regal on Sat Apr 02, 2022 2:06 pm, edited 1 time in total.

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

Re: Call python function through textbutton

#2 Post by emz911 » Fri Apr 01, 2022 7:39 pm

You want to give the button an "action Function(...)" https://www.renpy.org/doc/html/screen_a ... l#Function

Michael Regal
Newbie
Posts: 11
Joined: Fri Mar 25, 2022 11:38 pm
Contact:

Re: Call python function through textbutton

#3 Post by Michael Regal » Sat Apr 02, 2022 5:51 am

emz911 wrote:
Fri Apr 01, 2022 7:39 pm
You want to give the button an "action Function(...)" https://www.renpy.org/doc/html/screen_a ... l#Function
Yes, I know this much. But I don't know the syntax beyond "action Function(args...)"

It's fine though. I've about given up on this :)

User avatar
Ocelot
Eileen-Class Veteran
Posts: 1883
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Call python function through textbutton

#4 Post by Ocelot » Sat Apr 02, 2022 6:43 am

Did you read reference? It explains syntax:
Function(callable, *args, **kwargs)
This Action calls callable with args and kwargs.

callable
Callable object.
That means you place what you call (the thing before brackets) as callable:
Function(player_inv.take ...
args
position arguments to be passed to callable.
Since you are using a single positional argument, you place it here:
Function(player_inv.take, potion)
< < insert Rick Cook quote here > >

Michael Regal
Newbie
Posts: 11
Joined: Fri Mar 25, 2022 11:38 pm
Contact:

Re: Call python function through textbutton

#5 Post by Michael Regal » Sat Apr 02, 2022 2:06 pm

Ocelot wrote:
Sat Apr 02, 2022 6:43 am
Did you read reference? It explains syntax:
Function(callable, *args, **kwargs)
This Action calls callable with args and kwargs.

callable
Callable object.
That means you place what you call (the thing before brackets) as callable:
Function(player_inv.take ...
args
position arguments to be passed to callable.
Since you are using a single positional argument, you place it here:
Function(player_inv.take, potion)
Thank you!

Post Reply

Who is online

Users browsing this forum: Alex, nyeowmi