How to access Python class in Renpy?

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
tofuuu
Regular
Posts: 51
Joined: Tue Apr 24, 2018 7:15 pm
Contact:

How to access Python class in Renpy?

#1 Post by tofuuu » Wed Aug 22, 2018 10:18 pm

Hi! I'm sorry if you saw me too often. I was playing around with renpy and python, so i tried to access a python class in renpy. I want to get user input for the numbers, but somehow it didn't work, Btw i know this code isn't applied for visual novel, i just took an easiest example to tested out. So here's my code

Code: Select all

python:
    class Operation:
        num= []
        def __init__(self):
            n = input("How many numbers? ")
            n = int(n)
            i = 0
            while i < n:
                temp_num= input("Enter number-%d: " %i)
                temp_num= int(temp_num)
                self.num.append(temp_num)
                i+=1

        def print_num(self):
            return self.num
and in the label start, i called it like this:

Code: Select all

label start:
	$ objek_a = Operation()
	$ ang = objek_a.print_num()
	x "[ang]"
But i got some errors. Can you help me how to fix it? Please :( Thank you

tofuuu
Regular
Posts: 51
Joined: Tue Apr 24, 2018 7:15 pm
Contact:

Re: How to access Python class in Renpy?

#2 Post by tofuuu » Wed Aug 22, 2018 10:52 pm

ah solved it! i should put the python block inside the start label. Thank you! Hope you have a good day :)

rames44
Veteran
Posts: 232
Joined: Sun May 29, 2016 4:38 pm
Contact:

Re: How to access Python class in Renpy?

#3 Post by rames44 » Thu Aug 23, 2018 11:46 am

The slightly “more Ren’py way” of doing this is to move the class to an “init python” block (instead of just a “python” one). That way, the class definition will be set up before any of your script is run. (All the “init” stuff is done before any script runs, so you can put it in its own file if you want)

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: How to access Python class in Renpy?

#4 Post by trooper6 » Thu Aug 23, 2018 12:00 pm

Yeah, class definitions shouldn't be inside the start label.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

tofuuu
Regular
Posts: 51
Joined: Tue Apr 24, 2018 7:15 pm
Contact:

Re: How to access Python class in Renpy?

#5 Post by tofuuu » Thu Aug 23, 2018 6:21 pm

But if i use init python, i can't access renpy.input, since i want to ask user for their input :(

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: How to access Python class in Renpy?

#6 Post by trooper6 » Thu Aug 23, 2018 9:23 pm

Some things shouldn't be classes. But just labels that you call.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

rames44
Veteran
Posts: 232
Joined: Sun May 29, 2016 4:38 pm
Contact:

Re: How to access Python class in Renpy?

#7 Post by rames44 » Fri Aug 24, 2018 11:51 am

Again, the “more Ren’py way” would be to separate the functions - ask for input using “normal” script, and then if you need a class to use that info, pass it to the class via a $ line or an in-line Python block. What happens if you need to update the class between 0.1 and 0.2? The way you’ve done it, someone with a save won’t get the updates, because they will be past that block of Python.

As trooper6 quite correctly points out, you can create callable sections of Ren’py script if you need reusable script-based “stuff.”

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], span4ev