Obtaining Attributes From 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
Bazzababes
Newbie
Posts: 3
Joined: Sat Aug 10, 2019 1:10 pm
Contact:

Obtaining Attributes From a Class

#1 Post by Bazzababes » Sat Aug 10, 2019 1:18 pm

Hi, I am trying to obtain an attribute from a class in renpy, and then display it to the user.


Here is some example code:

Code: Select all

python:
    class survivor :
        def __init__(self):
            self.fname = fname
            self.sname = sname
            self.age = age
            self.health = currhealth
            self.maxhealth = maxhealth
            self.will = currwill
            self.maxwill = maxwill
            self.ability = ability

    Samuel = survivor("Samuel","Jordie",17,6,6,2,2,"Strength")

# The game starts here.

label start:
    "Welcome to Title Of Game!"
    "What level of difficulty would you like to play on?"
    "[Samuel.health]"
When I run this code I receive this error.

Code: Select all

"[Samuel.health] KeyError: u'Samuel'
I am unsure as to why this occurs. Any help appreciated! :D

User avatar
Angelo Seraphim
Regular
Posts: 32
Joined: Tue May 21, 2019 8:00 am
Completed: Enamored Risks (NaNoReNo 2020)
Projects: 616 Charagma
Organization: GLSUoA
Deviantart: glsuoa
itch: glsuoa
Location: London, UK
Discord: Just A Concept#9599
Contact:

Re: Obtaining Attributes From a Class

#2 Post by Angelo Seraphim » Sat Aug 10, 2019 2:33 pm

Code: Select all

init python:
	class Survivor(object):
	    def __init__(self, fname, sname, age, currhealth, maxhealth, currwill, maxwill, ability):
	        self.fname = fname
	        self.sname = sname
	        self.age = age
	        self.health = currhealth
	        self.maxhealth = maxhealth
	        self.will = currwill
	        self.maxwill = maxwill
	        self.ability = ability


default Samuel = Survivor("Samuel", "Jordie", 17, 6, 6, 2, 2, "Strength")

# The game starts here.
label start:
    "Welcome to Title of Game!"
    "What level of difficulty would you like to play on?"
    "[Samuel.health]"
I've done a test in pycharm. Maybe test it first in RenPy and see the results.
I think you forgot to do is add the arguments. Other than that everything else should work properly.

Hope this works for you. :D
Image

Bazzababes
Newbie
Posts: 3
Joined: Sat Aug 10, 2019 1:10 pm
Contact:

Re: Obtaining Attributes From a Class

#3 Post by Bazzababes » Sat Aug 10, 2019 3:55 pm

Angelo Seraphim wrote:
Sat Aug 10, 2019 2:33 pm

Code: Select all

init python:
	class Survivor(object):
	    def __init__(self, fname, sname, age, currhealth, maxhealth, currwill, maxwill, ability):
	        self.fname = fname
	        self.sname = sname
	        self.age = age
	        self.health = currhealth
	        self.maxhealth = maxhealth
	        self.will = currwill
	        self.maxwill = maxwill
	        self.ability = ability


default Samuel = Survivor("Samuel", "Jordie", 17, 6, 6, 2, 2, "Strength")

# The game starts here.
label start:
    "Welcome to Title of Game!"
    "What level of difficulty would you like to play on?"
    "[Samuel.health]"
I've done a test in pycharm. Maybe test it first in RenPy and see the results.
I think you forgot to do is add the arguments. Other than that everything else should work properly.

Hope this works for you. :D
Sorry im new to oop! Thanks :D

Post Reply

Who is online

Users browsing this forum: Bing [Bot]