variables and 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
bebeesun
Regular
Posts: 30
Joined: Sat Jan 23, 2016 5:20 pm
Contact:

variables and classes

#1 Post by bebeesun » Fri Jan 29, 2016 4:57 am

im back again and there's something that i need help with that is very simple. but i cant figure it out bc i just learned about classes today so im new to it. im trying to make a menu that displays a list of characters and how much they are in love with the main character....

Code: Select all

init python:
    

    class Lover():
        def __init__(self, name, points):
            self.name = name
            self.points = points 
            lovers.append(self)
        
     

            
screen lovers:
    modal True
    $ lovers= [name for name in lovers]
    frame:
        xpos 942
        ypos 650
        xsize 510
        ysize 405
        
        vbox:
            label "lovers"
            if name in lovers:
                 text name.name + #points go here....
                 textbutton "Close" action Hide("lovers")
i just want to be able to display the characters points next to the name thats all.

User avatar
Iylae
Regular
Posts: 73
Joined: Sat Jan 09, 2016 6:57 am
Location: Cornwall, UK
Contact:

Re: variables and classes

#2 Post by Iylae » Fri Jan 29, 2016 5:12 am

Try the following:

Code: Select all

init python:
    
    lovers = []   #initialise the lovers variable as an array/list using []
    class Lover():
        def __init__(self, name, points):
            self.name = name
            self.points = points 
            lovers.append(self)
        
    Lover("Ariadne",0)   #create a lover
    Lover("Belle",0)     #create another lover

            
screen lovers():
    modal True
    frame:

        # I removed the pos vars because it wouldn't display on my resolution

        vbox:
            label "lovers"
            for lover in lovers:    # note the change here.
                text lover.name + ": " + str(lover.points)    # as lover.points is an integer, we use str() to turn it into a string
            textbutton "Close" action Hide("lovers")
                 
label start:
    show screen lovers  #  actually show the screen
    ""  # waits for the user to click before ending
Image
  If we are what we repeatedly do, then good coding is not an act, but a habit

bebeesun
Regular
Posts: 30
Joined: Sat Jan 23, 2016 5:20 pm
Contact:

Re: variables and classes

#3 Post by bebeesun » Fri Jan 29, 2016 5:24 am

thank you so much!!

bebeesun
Regular
Posts: 30
Joined: Sat Jan 23, 2016 5:20 pm
Contact:

Re: variables and classes

#4 Post by bebeesun » Fri Jan 29, 2016 5:42 am

wait i have another question...they dont show up on the list at the same time. only one does.

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

Re: variables and classes

#5 Post by xela » Fri Jan 29, 2016 6:17 am

bebeesun wrote:wait i have another question...they dont show up on the list at the same time. only one does.
They should both show up, but that is a bad code that will fail you in a real gameplay on saving, try:

Code: Select all

init:
   
    default lovers = []

    python:
        class Lover(object):
            def __init__(self, name, points):
                self.name = name
                self.points = points
                lovers.append(self)
       

screen lovers():

    modal True

    frame:
        has vbox
        label "lovers"
        for lover in lovers:    # note the change here.
            text lover.name + ": " + str(lover.points)    # as lover.points is an integer, we use str() to turn it into a string
        textbutton "Close" action Hide("lovers")
                 

label start:
    $ Lover("Ariadne", 0) # create a lover
    $ Lover("Belle", 0) # create another lover

    show screen lovers # actually show the screen

    pause # waits for the user to click before ending
That prolly won't fix whatever might be causing the issue with the other actor not showing but it will save properly.
Like what we're doing? Support us at:
Image

bebeesun
Regular
Posts: 30
Joined: Sat Jan 23, 2016 5:20 pm
Contact:

Re: variables and classes

#6 Post by bebeesun » Fri Jan 29, 2016 6:31 am

thankk you!! your code actually fixed the problem. that also happened to me last time i tried making a menu idk what im doing to make that happen but thank you!!

User avatar
Iylae
Regular
Posts: 73
Joined: Sat Jan 09, 2016 6:57 am
Location: Cornwall, UK
Contact:

Re: variables and classes

#7 Post by Iylae » Fri Jan 29, 2016 6:57 am

Honestly I need to post-it note the default keyword to my monitor. Thanks for picking up on that xela.

... Also instantiating variables in start: and not in init:
Last edited by Iylae on Fri Jan 29, 2016 7:04 am, edited 1 time in total.
Image
  If we are what we repeatedly do, then good coding is not an act, but a habit

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

Re: variables and classes

#8 Post by xela » Fri Jan 29, 2016 7:02 am

Iylae wrote:Honestly I need to post-it note the default keyword to my monitor. Thanks for picking up on that xela.
NP.

+ You shouldn't instantiate classes in the init, you may run into a similar issue with saving/loading.
Like what we're doing? Support us at:
Image

Post Reply

Who is online

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