How to import another file classes

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
suihakei
Newbie
Posts: 21
Joined: Sun Jun 23, 2019 12:12 pm
Contact:

How to import another file classes

#1 Post by suihakei »

I'm a Japanese.
So my English is may be bad.
Sorry.

I don't want to make files redundant with scripts.
So I have defined a Python class in a separate file.
However, the file cannot be read.
a.jpg
a.jpg (13.18 KiB) Viewed 642 times

Code: Select all

python:
    import utils.python_random
b.jpg
b.jpg (8.54 KiB) Viewed 642 times
error:
c.jpg
The contents of python_random.rpy are:

Code: Select all

python:
    class PythonRandom(object):
        def __init__(self, list_for_random):
            self.list_for_random = list_for_random

        def pick(self):
            import random
            return random.choice(self.list_for_random)
Python should be able to load this way.
But not with renpy.
How does renpy do this?

Thank you.

User avatar
Enchant00
Regular
Posts: 136
Joined: Tue Jan 12, 2016 1:17 am
Contact:

Re: How to import another file classes

#2 Post by Enchant00 »

You don't need to import your utils.python_random. The good thing about RENPY is that no matter where the file is located within the main directory, they are connected. So if you want to use your class PythonRandom, you can just create an object for it in any other rpy file and access it directly. You only need to use the import statement if you have python modules that aren't part of your game.

suihakei
Newbie
Posts: 21
Joined: Sun Jun 23, 2019 12:12 pm
Contact:

Re: How to import another file classes

#3 Post by suihakei »

Thank you for your reply!

> So if you want to use your class PythonRandom, you can just create an object for it in any other rpy file and access it directly.

Oh I see!
I understand!

However, without init I got the same error.
Is init absolutely necessary?

error:

Code: Select all

python:
    class PythonRandom(object):
        def __init__(self, list_for_random):
            self.list_for_random = list_for_random

        def pick(self):
            import random
            return random.choice(self.list_for_random)
success:

Code: Select all

init python:
    class PythonRandom(object):
        def __init__(self, list_for_random):
            self.list_for_random = list_for_random

        def pick(self):
            import random
            return random.choice(self.list_for_random)

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: How to import another file classes

#4 Post by Remix »

A file in ./utils/will be parsed late though... If you want things defined in those files available in others it might be best to rename utils to _utils

Ren'Py semi interprets files even before doing the inits, so it does expect to find things which are referenced in one file even if they are defined elsewhere (it expects to have already read them)
Frameworks & Scriptlets:

suihakei
Newbie
Posts: 21
Joined: Sun Jun 23, 2019 12:12 pm
Contact:

Re: How to import another file classes

#5 Post by suihakei »

Thank you for your reply!

I changed init python to python, and changed utils/ to _utils.
コメント 2019-08-08 223044.jpg
コメント 2019-08-08 223044.jpg (15.93 KiB) Viewed 572 times
However, an error occurred.
コメント 2019-08-08 223044.jpg
コメント 2019-08-08 223044.jpg (15.93 KiB) Viewed 572 times
I still thought init was necessary.

But I could do what I wanted with init.
Thank you for your answer.

Post Reply

Who is online

Users browsing this forum: Bing [Bot]