pythonrenpy

getting attribute error in renpy where object has no attribute EDIT: Additional information


I've been trying to make a pesudo RPG like game on renpy using python coding. I'm mostly using this as a basic testing ground and organized my code into different rpy files. But right now, I am having trouble trying to tie these skills from the subclass to the main class. Here's the character class

#Detective Attributes

class Detective:
    def __init__(self, name, intelligence, psyche, strength, dexterity):
        self.name = name
        self.intelligence = intelligence
        self.psyche = psyche
        self.strength = strength
        self.dexterity = dexterity

#Intelligence skills

class IntSkill(Detective):
    def __init__(self, logic, history, persuasion, deception):
        super().__init__(self, name, intelligence, psyche, strength, dexterity)

        self.logic = logic
        self.history = history
        self.persuasion = persuasion
        self.deception = deception

default detective = Detective("nameless", 5, 5, 5, 5)

I feel as though I should just post the full script so people will know what I'm looking for. Trying to apply pesudo RPG like effects where the attributes of the detective class will also carry into the skills that are supposed to be defined as the child of the class.

label start:


unknown "Can you feel it?"

"..."

unknown "Can you see it?"

"..."

unknown "The spark of life flowing back in your mortal coil."

unknown "It may feel empty, cold and dark at first. That's always the case like this."

unknown "Now, your last moments, do you remember them, child of man?"

unknown "'Sorry detective, but the game was rigged from the start.'"

"First silence,"

"then there was gunfire."

"Three shots were fired."

"The last two were in rapid succession, as though fired at the same time."

unknown "Yes, yes it is exactly as you remember it."

unknown "But do you remember anything after? Anything before?"

"No... I don't think I do."

unknown "Nobody really remembers their experiences after death."

unknown "But most would remember their time before their death. Do you not know?"

" I don't know if I know myself. Who am I?"

unknown "Ahh, scrambled and fried like eggs over an open flame."

"Something happened?"

unknown "Yeah child of man... you died."

"I knew that from what you were saying."

unknown "And do you remember nothing before then?"

"Some double fucking mother fucker with poor fashion sense."

"And a... bird folk?"

unknown "Ah so some memories have survived."

"Why is this happening?"

unknown "Because sometimes the universe has a cosmological joke to play on all of us,"

unknown "even me."

"So what happens now?"

unknown "First I will ask you a couple questions."

menu:
     unknown "What is your most definitive attributes?"

     "My vast intellect to unraveling the Material world":
         $ nameless.intelligence += 2

         jump after_attribute_1

     "My intimate relationship with the Immaterial.":
         $ nameless.psyche += 2

         jump after_attribute_1

     "My martial prowess":
         $ nameless.strength += 2

         jump after_attribute_1

     "My flexability and nimbleness.":
         $ nameless.dexterity += 2

         jump after_attribute_1

label after_attribute_1:

menu:
     unknown "Good... now, what is your weakest trait?"

     "I struggle to grasp the basics of Material science" if nameless.intelligence == 5:
         $ nameless.intelligence -= 2
         jump after_attribute_2

     "I am deaf to the Immaterial." if nameless.psyche == 5:
         $ nameless.psyche -= 2
         jump after_attribute_2

     "I am physically weak." if nameless.strength == 5:
         $ nameless.strength -= 2
         jump after_attribute_2

     "I trip over my own feet."if nameless.dexterity == 5:
         $ nameless.dexterity -= 2
         jump after_attribute_2

label after_attribute_2:

unknown "Now let us see how you handle critical thinking."

unknown "What do you think happened?"

if nameless.intelligence.logic >= 7:
    "\[Passive Logic Successful] I think I got shot in the head."

if nameless.intelligence.logic < 7:
    "Balls if I know"


return

And this is what I keep on getting.

[code]
I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 126, in script
    if int.logic >= 7:
  File "game/script.rpy", line 126, in <module>
    if int.logic >= 7:
AttributeError: type object 'int' has no attribute 'logic'

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 126, in script
    if int.logic >= 7:
  File "renpy/ast.py", line 1892, in execute
    if renpy.python.py_eval(condition):
  File "renpy/python.py", line 2249, in py_eval
    return py_eval_bytecode(code, globals, locals)
  File "renpy/python.py", line 2242, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "game/script.rpy", line 126, in <module>
    if int.logic >= 7:
AttributeError: type object 'int' has no attribute 'logic'

Windows-10-10.0.19041
Ren'Py 7.4.6.1693
Tales of Tellus Mindthrob 0.1
Thu Jun 24 22:48:31 2021
[/code]

I've been trying to use things like class inheritor but I just don't know what I am doing wrong.


Solution

  • What's happening is that when you're defining Detective you're passing an integer (of type int) as the value of intelligence:

                       #  intelligence = 5 ---.
    default detective = Detective("nameless", 5, 5, 5, 5)
    

    So RenPy thinks that intelligence is an integer, not an object with sub-properties from IntSkills. Later, when you write code like nameless.intelligence.logic >= 7, it gets confused. RenPy thinks, "But intelligence is a number. How can something like the number 4 have a logic property?"

    There's more than one way to approach this, but I would recommend making intelligence a more complex type, so that you could then do something like detective.intelligence.raw += 4 for when you need a raw intelligence score value (what you are using for detective.intelligence now) and detective.intelligence.logic for the logic score.

    To do this, the code might look like this:

    init python:
    
        class Detective:
            def __init__(self, name, intelligence, psyche, strength, dexterity):
                self.name = name
                self.intelligence = intelligence
                self.psyche = psyche
                self.strength = strength
                self.dexterity = dexterity
        
        class IntSkills:
            def __init__(self, raw, logic, history, persuasion, deception):
                self.raw = raw
                self.logic = logic
                self.history = history
                self.persuasion = persuasion
                self.deception = deception
    
    default  nameless =     Detective(
            "nameless", 
            IntSkills(10, 2, 2, 4, 5),
            5, 5, 5
     )
    define unknown = "Unknown character"
    
    label start:
        # some scene
        scene bg room
        
        # adjust raw intelligence
        $ nameless.intelligence.raw  -= 2
    
        unknown "Now let us see how you handle critical thinking."    
        unknown "What do you think happened?"
        
        # use logic
        if nameless.intelligence.logic >= 7:
            "\[Passive Logic Successful] I think I got shot in the head."
        
        if nameless.intelligence.logic < 7:
            "Balls if I know"
            
        "You have [nameless.intelligence.raw] base intelligence"
        "You have  [nameless.intelligence.logic] logic"
            
        return