[SOLVED]add and subtract problem.

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
johandark
Veteran
Posts: 356
Joined: Sat Apr 30, 2016 11:04 am
Completed: Wild Guards, In Dreams
Projects: Pact with a witch
Deviantart: johandarkweb
Location: Barcelona
Contact:

[SOLVED]add and subtract problem.

#1 Post by johandark »

Code: Select all

init python:
        
    # This class keeps all the points,
    # allowing the variables to be acessed 
    # in functions.
    class PointFucker(object):

        def __init__(self):

            self.dp = 0

    # Make instance of class
    pf = PointFucker()

    # Adds num points to dp and notifys
    # the player
    def add_dp(num):
        pf.dp += num
        renpy.notify(str(num) + " {size=12}DÍDAC POINT!!{/size}")
        
    # Subtract num points to dp and notifys
    # the player
    def sub_dp(num):
        pf.dp -= num
        renpy.notify(str(num) - " {size=12}DÍDAC POINT!!{/size}")
and here is the label start:

Code: Select all

    p "I think I love you!"
    
    $ add_dp(3)

    p "But you are a bad person..."
    
    $ sub_dp(3)

    p "I don´t know, I´m a little crazy."
I have 2 problems with this code:

1.

$ add_dp(3) Works fine.

but $ sub_dp(3) gives me this message:
renpyproblem.jpg
2.

When I rollback (on the one that works) every time I rollback there it adds more and more points... Is there a way to avoid adding more points?


The 2nd problem is not such a big deal as the first one, but it would be nice to know if there´s a solution.

after all with the 2nd problem I can block rolling back, but it would be nice to know if there´s a way.

Thanks!
Last edited by johandark on Mon Jul 18, 2016 12:32 pm, edited 1 time in total.

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: def add and subtract problem.

#2 Post by trooper6 »

So, I didn't particularly like the way your code was set up so I changed it a bit to make it more logical. The code I have works perfectly fine. I'll show you the code, then I'll explain what I did and why.

Code: Select all

define p = Character('Player', color="#c8ffc8")
define li = Character('Love Interest', color="#c8ffc8")

default pf = PointFucker()

init python:        
    class PointFucker(object):
        def __init__(self):
            self.dp = 0
            
        def ch_dp(self, num):
            self.dp += num
            renpy.notify(str(num) + " {size=12}DÍDAC POINT!!{/size}")
            
screen points():
    frame:
        xalign 1.0
        text "Total Points: [pf.dp]"

    

# The game starts here.
label start:
    show screen points
    "The Game Starts"
    
    $ pf.ch_dp(3)
    p "I think I love you!"
    
    $ pf.ch_dp(3)
    p "I really love you!"

    $ pf.ch_dp(-3)
    p "But you are a bad person..."

    $ pf.ch_dp(-3)
    p "And you are smelly..."
    
    p "I don´t know, I´m a little crazy."
    
    menu:
        "But in the end..."
        "I want to marry you":
            $ pf.ch_dp(5)
        "I never want to see you again you":
            $ pf.ch_dp(-5)
    
    p "What do you say?"
    
    if pf.dp > 0:
        li "I want to marry you, too!"
    else:
        li "Well...okay then...you are smelly, too."
        
    "The end"
    
    return
    
1) In your old code, you created an instance of PointFucker in the init python block. Whenever you create an instance of a class or a variable it should be done using "default" (the old proper way was to declare it at the start of start label, but default is the new way). When you do this, the instance/variable participates properly in saving/rollback.

2) In your old code you had two different functions to deal with points: one add, one subtract. But you don't need two functions, you can just have one and pass positive and negative numbers. Also, the functions were all by themselves, but because the functions really only deal with things involving PointFucker, it makes more sense for that function to belong to PointFucker rather than belonging to the program as a whole. So I made the function part of PointFucker.

3) You were worried about rollback...However, rollback shouldn't be a problem. I made a little screen and placed it in the top right corner of the screen that shows how many points you have at any given time. If you test rolling back and forward, you will see that rollback works.

4) I personally like having the notify right before the line of text that triggers it...but that is a personal preference. See if you like it my way.

5) I also added in a menu and an if/else--two things people often want in point games.
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
johandark
Veteran
Posts: 356
Joined: Sat Apr 30, 2016 11:04 am
Completed: Wild Guards, In Dreams
Projects: Pact with a witch
Deviantart: johandarkweb
Location: Barcelona
Contact:

Re: def add and subtract problem.

#3 Post by johandark »

Thank you very much! That solved the problem!!

I added a second person doing this:

Code: Select all

default pf = PointFucker()

default pk = PointKeeper()

init python:        
    class PointFucker(object):
        def __init__(self):
            self.dp = 0
            
        def ch_dp(self, num):
            self.dp += num
            renpy.notify(str(num) + " {size=12}DÍDAC POINT!!{/size}")
            
    class PointKeeper(object):
        def __init__(self):
            self.np = 0
            
        def ch_np(self, num):
            self.np += num
            renpy.notify(str(num) + " {size=12}NEUS POINT!!{/size}")
later I used a label top-left to show points.

Code: Select all

init:
    
    #Screen button where Points of life and other stuff is showed.
    
    screen button:
        hbox xalign 0.01:
            
            text "{size=15}Dídac Puntos: [pf.dp]{/size}" outlines [(1, "000", 0, 1)]
            
            text "{size=15} Neus Puntos: [pk.np]{/size}" outlines [(1, "000", 0, 1)]
and with renpy.notify I used this script:

Code: Select all

transform _notify_transform:
    # These control the position.
    yalign .1

    # These control the actions on show and hide.
    on show:
        alpha 0 xalign 1.1
        linear .5 alpha 1.0 xalign .9
    on hide:
        linear .5 alpha 0.0 xalign 1.1

later in text I used this.

Code: Select all

label start:
        
    show screen button
    n "You´re nice."
    
    $ pf.ch_dp(3)
    
    p "¡¿Qué?! ¿A dónde me llevas ahora payaso? ¡Me vas a arrancar el brazo!"
    
    $ pf.ch_dp(-8)

    n "Os detenéis delante de la puerta de los servicios."
    
    $ pf.ch_dp(4)
    
    p "Oye… si te estás cagando me parece muy bien… ¿Pero por qué diablos me llevas al lavabo contigo?"
    
    $ pk.ch_np(4)

    d "Cállate… Shhh… Hazme un favor ¿quieres? Pase lo que pase que nadie entre aquí... ¿De acuerdo?"
    
    $ pk.ch_np(-4)
I´m very noob on programming... I hope this is the best way to work on this kind of things.

I hope the way I made to add a second person is the correct way.

I mean using the "default pk = PointKeeper()" second default...

Using only one didn´t work.

Thank you very much! It helped me a lot!

Post Reply

Who is online

Users browsing this forum: Bing [Bot]