About Classes, Class-Methods and Ren'Py/Python

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
Guga Figueiredo
Regular
Posts: 28
Joined: Wed Aug 14, 2013 2:01 pm
Contact:

About Classes, Class-Methods and Ren'Py/Python

#1 Post by Guga Figueiredo »

Ok.. so recetly i have been playing around with classes and methods in a Python IDE to better understand OOP

And I started implementing my current project's characters as objects directly in python. The class-methods i described all worked just fine.
I then used the same code directly on my script, under a init python block. It worked fine enough that methods returned what was expected of them.
However, as expected, some things were left out, like couple o lines of text I wanted to display to user along the execution of the method. Like in the following method of my Hero class that describes a combatsys:

Code: Select all

        def fightEnemy(self, enemy, **events):

            CRT = [[[3, 5], [4, 5], [5, 4], [6, 4], [7, 4], [8, 3], [9, 3], [0, 'k'], [0, 'k'], [0, 8], [0, 6], [1, 6], [2, 5]],
                   [[4, 4], [5, 4], [6, 3], [7, 3], [8, 3], [9, 3], [10, 2], [0, 'k'], [0, 8], [0, 7], [1, 6], [2, 5], [3, 5]],
                   [[5, 4], [6, 3], [7, 3], [8, 3], [9, 2], [10, 2], [11, 2], [0, 8], [0, 7], [1, 6], [2, 5], [3, 5], [4, 4]],
                   [[6, 3], [7, 3], [8, 2], [9, 2], [10, 2], [11, 2], [12, 2], [0, 8], [1, 7], [2, 6], [3, 5], [4, 4], [5, 4 ]],
                   [[7, 2], [8, 2], [9, 2], [10, 2], [11, 2], [12, 2], [14, 1], [1, 7], [2, 6], [3, 5], [4, 4], [5, 4], [6, 3]],
                   [[8, 2], [9, 2], [10, 2], [11, 1], [12, 1], [14, 1], [16, 1], [2, 6], [3, 6], [4, 5], [5, 4], [6, 3], [7, 2]],
                   [[9, 1], [10, 1], [11, 1], [12, 0], [14, 0], [16, 0], [18, 0], [3, 5], [4, 5], [5, 4], [6, 3], [7, 2], [8, 2]],
                   [[10, 0], [11, 0], [12, 0], [14, 0], [16, 0], [18, 0], ['k', 0], [4, 4], [5, 4], [6, 3], [7, 2], [8, 1], [9, 1]],
                   [[11, 0], [12, 0], [14, 0], [16, 0], [18, 0], ['k', 0], ['k', 0], [5, 3], [6, 3], [7, 2], [8, 0], [9, 0], [10, 0]],
                   [[12, 0], [14, 0], [16, 0], [18, 0], ['k', 0], ['k', 0], ['k', 0], [6, 0], [7, 0], [8, 0], [9, 0], [10, 0], [11, 0]]]

            rounds = 1
            CR = self.CS - enemy.CS
            print 'CR =', CR
            if CR > 0:
                CR = (CR + 1) / 2
            else:
                CR = CR / 2
            while self.EP > 0 and enemy.EP > 0:
                print 'Hero CS:', self.CS, '|Hero EP:', self.EP
                print 'Enemy CS:', enemy.CS, '|Enemy EP:', enemy.EP
                randomNumber = self.pickRandomNumber()
                print 'Random Number Picked:', randomNumber
                print 'Random CRT Cell:', CRT[randomNumber][CR]
                enemy.takeDMG(CRT[randomNumber][CR][0])
                if enemy.EP <= 0:
                    return '---Win!---'
                self.takeDMG(CRT[CR][randomNumber][1])
                if self.EP <= 0:
                    return '---Lose!---'
                for key in events:
                    if rounds == events[key][0]:
                        return events[key][1]
                    rounds += 1
This returned the right results of the combat every time ran, but the lines where I print out information didn't print anything at all. I understand that Python code is not Ren'Py code, so i expected as much.
But in the end, I want to display some lines of text during combat, portraying the results of each round of combat.

So what I would like to know though is: can this be done with Ren'Py code alone or can python and ren'py be mixed for such end-result? how would I go about doing that?

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: About Classes, Class-Methods and Ren'Py/Python

#2 Post by xela »

Please read this: http://lemmasoft.renai.us/forums/viewto ... =8&t=15939

It's exactly the same topic as you've started here.
Like what we're doing? Support us at:
Image

Guga Figueiredo
Regular
Posts: 28
Joined: Wed Aug 14, 2013 2:01 pm
Contact:

Re: About Classes, Class-Methods and Ren'Py/Python

#3 Post by Guga Figueiredo »

Lol... my search was unable to find this post. I was thinking the answer would be something like this.
Can I assume that methods dealing with images, ATL and more Ren'Py code will follow the same implementation like in here: http://www.renpy.org/doc/html/statement ... lents.html ?

Also, I was unable to "print" out values inside strings. Specificaly, when this line, and alike:

Code: Select all


narrator('Hero CS: [self.CS] |Hero EP: [self.EP]')

This raises a KeyError, as if trying to get value from a dict. I expected it to work like in renpy, as shown in the doc

philat
Eileen-Class Veteran
Posts: 1912
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: About Classes, Class-Methods and Ren'Py/Python

#4 Post by philat »

Correct me if I'm wrong, but isn't the [] method of interpolation a renpy feature rather than python? I would imagine it wouldn't work in python parts of the code.

Guga Figueiredo
Regular
Posts: 28
Joined: Wed Aug 14, 2013 2:01 pm
Contact:

Re: About Classes, Class-Methods and Ren'Py/Python

#5 Post by Guga Figueiredo »

I thought so too.. but since the documentation states that thses two are equivalent:

Code: Select all

e "Say something"
$ e("Say something")
I kinda expected them to behave alike.
Also, since the renpy.say() statement gets a string as an argument, what am I to do then to print out values withi this string with python say statement equivalent?

philat
Eileen-Class Veteran
Posts: 1912
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: About Classes, Class-Methods and Ren'Py/Python

#6 Post by philat »

Huh. I gave it a test run in a small project and apparently the interpolation itself DOES work (to my surprise!). So I have no idea why your code doesn't work, sorry :P But at least now we know what ISN'T the problem...

Guga Figueiredo
Regular
Posts: 28
Joined: Wed Aug 14, 2013 2:01 pm
Contact:

Re: About Classes, Class-Methods and Ren'Py/Python

#7 Post by Guga Figueiredo »

Could you post the code you ran?

Guga Figueiredo
Regular
Posts: 28
Joined: Wed Aug 14, 2013 2:01 pm
Contact:

Re: About Classes, Class-Methods and Ren'Py/Python

#8 Post by Guga Figueiredo »

I know, cause the doc also says so, that %-substitution is suposed to work with renpy.say(), but for the life of me i cannot figue out how to do that. Can say both of these didnt work:

Code: Select all

$ narrator('Hero CS: %self.CS |Hero EP: %self.EP', self.CS, self.EP)
$ narrator('Hero CS: %self.CS |Hero EP: %self.EP', self.CS, self.EP), self.CS, self.EP

philat
Eileen-Class Veteran
Posts: 1912
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: About Classes, Class-Methods and Ren'Py/Python

#9 Post by philat »

Oh, it was super simple -- just to test whether the [] method does work. I didn't try to go into classes or anything.

Code: Select all

label start:
    $ var = 1
    e "print [var]"
    python:
        narrator("print [var]")
        narrator("print %s" % var)
is literally all I ran. And all three worked fine. So I would assume the code not working is something to do with the classes... but just in case, you could try running it with the %s/%d method instead and see if that works? At this point I'm throwing spaghetti on the wall but :P


ETA: I've never seen your way of doing % interpolation before (but then again, I know very, very little of python, so that could just be my ignorance). From what I've seen, I believe the code in your case would be

Code: Select all

$ narrator('Hero CS: %d |Hero EP: %d' % (self.CS, self.EP))

Guga Figueiredo
Regular
Posts: 28
Joined: Wed Aug 14, 2013 2:01 pm
Contact:

Re: About Classes, Class-Methods and Ren'Py/Python

#10 Post by Guga Figueiredo »

Ty philat... This helped for today... makes me appreciate Pytom's efforts that much more
I will come back later for more complex stuff

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], munni, Ocelot