Do I need to define all the attributes in a class?

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
k3lit0
Newbie
Posts: 10
Joined: Sun Feb 05, 2017 2:07 am
Contact:

Do I need to define all the attributes in a class?

#1 Post by k3lit0 »

I'm trying to make a character creation system. I have a class for 'character' with all the variables included, but down the line when I reference them I get an error saying it's not defined (unless I separately define it, which defeats the point I think?)

Code: Select all

init python:

    maxstat = 10
    minstat = 0

    class character:
        def __init__(self, name, race, might, agility, toughness, alacrity, vigor, intellect, alertness, spirit, education, willpower, charisma, guile, intuition, tact, essence, money):
            self.name = name
            self.race = race
            self.might = 0
            self.agility = 0
            self.toughness = 0
            self.alacrity = 0
            self.vigor = 0
            self.intellect = 0
            self.alertness = 0
            self.spirit = 0 
            self.education = 0
            self.willpower = 0
            self.charisma = 0 
            self.guile = 0
            self.intuition = 0
            self.tact = 0
            self.essence = 0
            self.money = 100

    character = Character()

    character.race = _('Not Selected')
    character.name = _('Not Selected')
Then a bit later I'll have something like

Code: Select all

    text _("Name: [character.name] \nRace: [character.race] \nMight: [character.might]"):
and "character.might" gives an error for not being defined.

Code: Select all

AttributeError: 'ADVCharacter' object has no attribute 'might'


So I could just say character.might = 0 like I did with race and name, but I'd rather not do that for everything, since I've already done it up above. Or should that just say self.might = might ?

Sort of got in over my head and I'm hoping someone can help me out. I can do what I want to do but I'd like to save some time and learn the right way.

User avatar
Chekhov
Regular
Posts: 113
Joined: Tue Jun 26, 2018 9:19 am
Projects: Pluton
Contact:

Re: Do I need to define all the attributes in a class?

#2 Post by Chekhov »

I'm pretty sure it's a mistake to give the same name to a class as the instance of a class. Then the program might look for the class's might, but the class has no might attribute, it's just the instances that do.

So instead of doing:

character = Character()

use

john = Character()

You're also making some case sensitive errors, as the definition of the class isn't capitalized.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Do I need to define all the attributes in a class?

#3 Post by Remix »

You might benefit from reading through

viewtopic.php?f=51&t=47911

It uses the character namespace to allow a Stats instance to sit next to the ADVCharacter instance and have the single reference reflect both (basically you can write - e "Hello World" and e.might += 2 and it uses the Ren'Py character to speak or the stats class to manipulate stats)
Frameworks & Scriptlets:

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

Re: Do I need to define all the attributes in a class?

#4 Post by philat »

Character() is a reserved class anyway, so don't use that. (Remix's link is good, but probably over your head at this point.)

drKlauz
Veteran
Posts: 239
Joined: Mon Oct 12, 2015 3:04 pm
Contact:

Re: Do I need to define all the attributes in a class?

#5 Post by drKlauz »

Also create your characters using default or after start label.

Code: Select all

default bob=Character()
## or
label start:
  $bob=Character()
I may be available for hire, check my thread: viewtopic.php?f=66&t=51350

k3lit0
Newbie
Posts: 10
Joined: Sun Feb 05, 2017 2:07 am
Contact:

Re: Do I need to define all the attributes in a class?

#6 Post by k3lit0 »

So let's say there's just the one player character with all these stats. Would you just have a variable called might, another called agility, etc? What's the benefit of making it a class? I can change it to player or something if character isn't ok.

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Do I need to define all the attributes in a class?

#7 Post by RicharDann »

Classes are generally used when you need to have more than one instance of an object. Instance here means different copies of the same object but with different attributes. You have a Player class, for example, and then you can create individual characters, each with their own unique name, race, might, amount of gold, etc). If you will only need to use one player character, you do not strictly need to make a class just for them, but it can help in having your code more organized.

You shouldn't use character (beginning lowercase) to define a class, it is bad practice, and Character with uppercase is the name of a reserved class already defined in Ren'Py. The error you were getting with ADVCharacter is because it was trying to access that class.

So you would first need to change your class name to something like Player (uppercase), like you yourself suggested.

Code: Select all

    class Player:
        def __init__(self, *args):
As for the stats, it again depends on how you plan to use your Player object. Will you be using it just for your main character, or do you want to be able to create multiple characters instances?
The most important step is always the next one.

k3lit0
Newbie
Posts: 10
Joined: Sun Feb 05, 2017 2:07 am
Contact:

Re: Do I need to define all the attributes in a class?

#8 Post by k3lit0 »

RicharDann wrote: Fri Sep 13, 2019 10:33 am Classes are generally used when you need to have more than one instance of an object. Instance here means different copies of the same object but with different attributes. You have a Player class, for example, and then you can create individual characters, each with their own unique name, race, might, amount of gold, etc). If you will only need to use one player character, you do not strictly need to make a class just for them, but it can help in having your code more organized.

You shouldn't use character (beginning lowercase) to define a class, it is bad practice, and Character with uppercase is the name of a reserved class already defined in Ren'Py. The error you were getting with ADVCharacter is because it was trying to access that class.

So you would first need to change your class name to something like Player (uppercase), like you yourself suggested.

Code: Select all

    class Player:
        def __init__(self, *args):
As for the stats, it again depends on how you plan to use your Player object. Will you be using it just for your main character, or do you want to be able to create multiple characters instances?
Just the main character. I'm not even making a real renpy game, just a character maker that will then spit out your selections. It's for a tabletop rpg. I'm sure renpy's not perfect for that but it's what I've got and am somewhat used to. So I'd like it if you could then save your game(saving your character) and make different ones.

k3lit0
Newbie
Posts: 10
Joined: Sun Feb 05, 2017 2:07 am
Contact:

Re: Do I need to define all the attributes in a class?

#9 Post by k3lit0 »

Changing to Player fixed some problems, but if I don't define each stat I get an error still. Like I still need Player.might = 0. Is that normal? I have the self.might = 0 bit already so I assumed that covered it. If not, what does that actually do?

User avatar
isobellesophia
Miko-Class Veteran
Posts: 979
Joined: Mon Jan 07, 2019 2:55 am
Completed: None
Projects: Maddox and Friends! (AI Teacher friend), Friendly Universities! (Soon)
Organization: Friendly Teachers series
Deviantart: SophBelle
itch: Child Creation
Location: Philippines, Mindanao
Contact:

Re: Do I need to define all the attributes in a class?

#10 Post by isobellesophia »

k3lit0 wrote: Fri Sep 13, 2019 11:11 am Changing to Player fixed some problems, but if I don't define each stat I get an error still. Like I still need Player.might = 0. Is that normal? I have the self.might = 0 bit already so I assumed that covered it. If not, what does that actually do?
I think you need to set them to default.

Code: Select all

default might = 0
....
and do all of the above of charcater stats.
I am a friendly user, please respect and have a good day.


Image

Image


User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Do I need to define all the attributes in a class?

#11 Post by RicharDann »

k3lit0 wrote: Fri Sep 13, 2019 11:11 am Changing to Player fixed some problems, but if I don't define each stat I get an error still. Like I still need Player.might = 0. Is that normal? I have the self.might = 0 bit already so I assumed that covered it. If not, what does that actually do?
This happens because you're passing might and the rest of the stats as arguments but then you're not using them, or rather, because you're passing them without a default value.

You can do this (I only add a couple stats as an example, to save space, but you should do this with all your stats):

Code: Select all

init python:

    class Player():
        # here we define each attribute with a default value
        def __init__(self, name=_('Not selected'), race=_('Not selected'), might=0, agility=0):

            # then we set each instance's attribute to that value
            self.name = name 
            self.race = race 
            self.might = might 
            self.agility = agility
Then when characters are created:

Code: Select all

# in this first example, we define a Player instance with every default value
default player1 = Player() #their might = 0, agility = 0, etc.

# Here we define another Player instance but we set their name to Rock, and their might to 5.
# The other attributes use the default values set in the class definition.
default player2 = Player(name="Rock", might=5)
The most important step is always the next one.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]