Ren'Py classes and functions from Python in Penpy

Evildad

Member
Jan 7, 2019
113
218
Good day.
I am back after a long time with a mountain of questions.
I had started "Anna and her family" and paused for time reasons.
I'm currently retraining as an application developer in Germany and am now through with the basics. Now I wanted to slowly get back into Renpy, which is a disaster after three months of Java. o_O And when I look at my old code it makes me sick. :sick:

Now for my questions.
How can I use classes and functions from Python in Penpy. I find the docu on this a bit meagre.
Of course I have to open Python, but is there anything else I have to consider when I use it? Does it affect the rollback function, do variables declared in python need to be re-declared in renpy and can I even use project oriented programming as it was beaten into me and does it even make sense in renpy?
Many questions....

Thanks for your efforts!
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,369
15,285
Many questions....
To which the documentation, automatically installed on your computer with the SDK, answers relatively well. And also questions to which you already knew the answer one year ago when you made this thread, among other threads.

I could say more, but since, so far, you systematically discarded everything I answered to you, I'll not waste my time.
 

Evildad

Member
Jan 7, 2019
113
218
1. then why do they write at all?
2. if you scroll to the end of the post, I have accepted and applied it.
3. i know that Renpy is a language of its own with a very simple syntax compared to most other languages. However, one should not ignore that the language is written in Python.
4. in the university forum it was explained to me how to write my own functions such as jump, menu etc. for Renpy. They were really nice and goal-oriented!

Thanks anyway! And sorry for stealing your time!
 

rayminator

Engaged Member
Respected User
Sep 26, 2018
3,041
3,140
Class with function



example only:
Python:
class NPC(object):
        def __init__(self, name, niceName, location, isActive, lbt=""):
            self.name = name
            self.niceName = niceName
            self.location = location
            self.lbt = lbt
            self.isActive = isActive

        @property
        def avatar(self):
            global location
            avtr = "avatar/{}/{}_{}.png".format(self.name, self.name, location)
            if renpy.loadable(avtr):
                return avtr
            else:
                return "images/avatar/empty.png"

default NPCS = []
default lbt = ""

label InitialiseVariables:


    $ NPCS.append(NPC("mom", "Mom", "Ryan", True, lbt="sally_kitchen_talk"))
    $ NPCS.append(NPC("mom", "Mom", "Parent", False, lbt="sally_parent_talk"))
    $ NPCS.append(NPC("mom", "Mom", "bathroom", False, lbt="sally_bathroom_talk"))
    $ NPCS.append(NPC("mom", "Mom", "Livingroom", False, lbt="sally_livingroom_talk"))
    # NPC Values name, niceName, location, isActive, lbt label

    return

screen character_screen():

    for q in NPCS:
        if q.location == location and q.isActive:
            imagebutton:
                idle q.avatar
                hover q.avatar
                focus_mask True
                action Call(q.lbt)
 
  • Like
Reactions: Evildad

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,369
15,285
1. then why do they write at all?
To point out that you should start by reading the documentation.

Not only it would answer your questions, more precisely and more in depth than anyone could do here, but it would also show you that there's no need to reinvent the wheel.
 

Evildad

Member
Jan 7, 2019
113
218
but it would also show you that there's no need to reinvent the wheel.
As mentioned in the first post, I am currently training to become an application developer (still at the beginning). And one of my tasks is to create interfaces according to customer requirements.
This means that we also have to write our own libraries for Java, Python and co. and also understand all the backgrounds. But a simple documentation that only describes the basic uses is not enough.

For example, we have to write our own print statements, or compose strings from chars in arrays ourselves, and so on.

I don't want to reinvent the wheel. But I still have to be able to do it.

And for that we should choose areas that are the most fun for us. We should then learn these in depth, and that is Python and Renpy for me.
 

cisco_donovan

Member
Game Developer
Sep 18, 2020
218
284
Hi Evildad, a few things I'd say here:

1) A lot of programming is learned best by doing. Practice practice practice. Make mistakes, experiment, break stuff.
2) Keep things as simple as you can, all the time.
3) Try to solve your question yourself before asking here. You will learn so much faster. I think all your questions above can be solved by experimenting.
4) Use ren'py syntax (not python) as much as possible. It is brilliant. Do you really NEED a class? Are you trying to solve a problem that you can't solve without classes?
5) Use these forums to ask very specific questions, like "Why do I get this error when I declare a class?" or "why does it say this variable is not defined?".
6) The renpy docs (in English at least) really are very very good. They're so good that sometimes you'll have to hunt a bit to find the information you need.
 
  • Like
Reactions: Evildad and Bev_

Evildad

Member
Jan 7, 2019
113
218
First of all, thank you for all the answers. Also for those who got my mind working.

Of course I have taken everything to heart. Also your contribution anne O'nymous!
I've tried and tested a lot and got it right by reading and asking questions!

New questions have arisen, but more about that in another post.
 

Greslux

Member
Apr 24, 2022
243
335
I use classes all the time and it surprises me that most of the games I've seen aren't class based. People write hundreds of variables instead of creating one class and writing all the attributes in one place.

1. Is there anything to consider? I do not think so.
2. Sorry, I don't know about the rollback function.
3. You don't need to re-declare variables.
4. Can you use OOP? You can. I'm all for using OOP, even if some people think it's redundant.
5. Does it make sense? Undoubtedly.