(solved) Class function calls

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
User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

(solved) Class function calls

#1 Post by Milkymalk »

I'm mildly confused.

This code, put into a Python console, returns an expected value of 67.2:

Code: Select all

class bplayer(object):
    def __init__(self, name, stats, tags={}):
        self.name = name
        self.stat = stats
        self.tags = tags
        
    def playvalue(self, game):
        v = 0
        for i in range(len(self.stat)):
            v += game.stat[i] * self.stat[i]
        s = 0
        for i in range(len(game.stat)):
            s += game.stat[i]
        v = v / (s*5) * 100
        for i in game.tags:
            if i in self.tags:
                v += self.tags[i]
        return(v)

class bgame(object):
    def __init__(self, name, stats, info, tags):
        self.name = name
        self.stat = stats
        self.tags = tags

tim = bplayer('Tim', [4,4,2,4,3,4,5,1])
halma = bgame('Halma', [3,3,4,2,4,5,2,2], [(2,7),(30,30),3,7,7], ['nodice'])


print(tim.playvalue(halma))
This in Ren'py, however, returns 0 as a value:

Code: Select all

init python:
    class bplayer(object):
        def __init__(self, name, stats, tags={}):
            self.name = name
            self.stat = stats
            self.tags = tags
            
        def playvalue(self, game):
            v = 0
            for i in range(len(self.stat)):
                v += game.stat[i] * self.stat[i]
            s = 0
            for i in range(len(game.stat)):
                s += game.stat[i]
            v = v / (s*5) * 100
            for i in game.tags:
                if i in self.tags:
                    v += self.tags[i]
            return(v)
    
    class bgame(object):
        def __init__(self, name, stats, info, tags):
            self.name = name
            self.stat = stats
            self.tags = tags

    tim = bplayer('Tim', [4,4,2,4,3,4,5,1])
    halma= bgame('Halma', [3,3,4,2,4,5,2,2], [(2,7),(30,30),3,7,7], ['nodice'])

label start:
    $ pv = tim.playvalue(halma)
    "Tim has [pv]\%."
It's completely identical except for print being substituted with Ren'py script.

I even get an error if I don't calculate pv first and instead put the function call itself in the say statement, being told that the attribute "playvalue" does not exist for "tim".
Last edited by Milkymalk on Mon Jun 26, 2017 1:34 am, edited 1 time in total.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

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: Class function calls

#2 Post by trooper6 »

The problem you are having is that when you are putting your code into a python console, that console is a Python 3 console. Renpy is based off of Python 2. Python 2 and Python 3 handle division differently.

In Python 2, 5/2 = 2 (because 2 is an integer and you provided them with integers so they stayed with integers).
In Python 3, 5/2 = 2.5 (which is a float, but Python 3 automatically translates integers to floats if there is a remainder...but Python 2 doesn't)

All of this is detailed in many places, but for example, check this website: http://www.geeksforgeeks.org/division-o ... in-python/

If you want 67.2, you need to turn your v and s variables into floats, so the division will return floats. I've corrected the code for you.
Also, remember that current best practices is to declare all your non-persistent variables using default.

Code: Select all

init python:
    class bplayer(object):
        def __init__(self, name, stats, tags={}):
            self.name = name
            self.stat = stats
            self.tags = tags
            
        def playvalue(self, game):
            v = 0.0
            for i in range(len(self.stat)):
                v += game.stat[i] * self.stat[i]
            s = 0.0
            for i in range(len(game.stat)):
                s += game.stat[i]
            v = v / (s*5) * 100
            for i in game.tags:
                if i in self.tags:
                    v += self.tags[i]
            return(v)
    
    class bgame(object):
        def __init__(self, name, stats, info, tags):
            self.name = name
            self.stat = stats
            self.tags = tags
            
default tim = bplayer('Tim', [4,4,2,4,3,4,5,1])
default halma= bgame('Halma', [3,3,4,2,4,5,2,2], [(2,7),(30,30),3,7,7], ['nodice'])

label start:
    $pv = tim.playvalue(halma)
    "Tim has [pv]\%"
    return
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

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: Class function calls

#3 Post by Milkymalk »

Oh shoot, I'm so used to Python letting me do whatever I want compared to other languages that I forgot about the integer/float problem. Thank you! It was indeed a Python 3 console.

And yes, I just forgot using default for my variables. You are looking at 90% of what my code is currently consisting of ;) But thank you for reminding me!
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

Post Reply

Who is online

Users browsing this forum: decocloud