Defining and defaulting variables? [SOLVED]

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
DoubleProlix
Regular
Posts: 33
Joined: Thu Oct 19, 2017 8:04 pm
Contact:

Defining and defaulting variables? [SOLVED]

#1 Post by DoubleProlix »

I'm trying to get my head around the best way to initiate variables for use. Say I've got an NPC class:

Code: Select all

init -1:
    python:
        class NPC(object):
            def __init__(self, name):
                self.name = name
                self.alive = True
                self.hasMet = False
                self.relationship = 1
                self.isFollowing = False
and I've got a few NPCs:

Code: Select all

default Bailey = NPC("Bailey")

default Larson = NPC("Larson")

default Mary = NPC("Mary")
Do I also need to go ahead and add in some defines and defaults for each variable for each NPC?

Code: Select all

define Mary.name = "Mary"
default Mary.alive = True
default Mary.hasMet = True
default Mary.relationship = 8
default Mary.isFollowing = False
Last edited by DoubleProlix on Sat Jun 23, 2018 7:39 pm, edited 1 time in total.
Current Project: Thaumarotica - an occult erotic thriller
SubscribeStar - Patreon - Itch.io

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Defining and defaulting variables?

#2 Post by kivik »

If you know you need to create each character with unique values, you'd be better off adding those as parameters to your constructor:

Code: Select all

init -1:
    python:
        class NPC(object):
            def __init__(self, name, alive=True, hasMet=False, relationship=1, isFollowing=False):
                self.name = name
                self.alive = alive
                self.hasMet = hasMet
                self.relationship = relationship
                self.isFollowing = isFollowing
Basically you give each parameter a default value - so if you create an object without those parameters, they get the default values; if you pass in parameters, what you pass in will be used instead.

So you can create your Mary like this:

Code: Select all

# alive and isFollowing are same as the default parameter values, so we don't pass them in
default Mary = MPC("Mary", hasMet=False, relationship=8)
Also, don't use define and default on object attributes - it's a bit pointless and Renpy sometimes throws a fit (I haven't tested it extensively). Also, your define Mary.name statement would probably crash Renpy at launch - because it'd get executed before your default Mary = NPC() statement. Plus, even though you may think "Mary's name is never going to change", you're actually asking Renpy to save the Mary object, so Mary.name would still be saved as part of the Mary object regardless.

Hope this helps and makes sense!

DoubleProlix
Regular
Posts: 33
Joined: Thu Oct 19, 2017 8:04 pm
Contact:

Re: Defining and defaulting variables?

#3 Post by DoubleProlix »

So noted, thanks!
Current Project: Thaumarotica - an occult erotic thriller
SubscribeStar - Patreon - Itch.io

Post Reply

Who is online

Users browsing this forum: Google [Bot], piinkpuddiin