0

I created a super simple Character and Buildable class for a miniature game I am developing and I have created a nested class where Character is for the outer class and Buildable is the inner class. I am trying to loop through resources from Character inside my upgradeBuildable function within Buildable.

class Character:

# Constructor -- Also why do comments use # and not // -_-
def __init__(self):
    self.name = "Pickle"
    self.health = 100
    self.resources = ["Fire", "Coal"]
    self.buildObj = self.Buildable()  # Object for nested 2nd class

# Function to display character info
def characterInfo(self):
    print("CharacterInfo --> " + "Name:", self.name, "| Health:", self.health, "| Resources:", self.resources)

def printRes(self):
    print(self.resources)

# Function for collecting resources
def collectResource(self, newResource):
    self.resources.append(newResource)

class Buildable:
    def __init__(self):
        self.buildings = ["Fire 1"]
        self.upgradeStatus = 0
        self.outer = Character()

    def displayBuildable(self):
        print("Buildables -->", self.buildings)

    def createBuildable(self, newBuilding):
        self.buildings.append(newBuilding)

    def upgradeBuildable(self, replaceBuilding):
        if self.upgradeStatus == 0:
            # Update selected building
            for i in range(len(self.buildings)):
                if self.buildings[i] == replaceBuilding:
                    self.buildings[i] = "2x"
                    break
            print(Character.resources)
            self.upgradeStatus = self.upgradeStatus + 1
            print(self.upgradeStatus)

When I try to access print the resource attribute from Character in upgradeBuildable I get a recursion error "RecursionError: maximum recursion depth exceeded" which is confusing to me.

All I am trying to do is try and print the resources the character has inside my inner Buildable class. Any help is much appreciated !!

My main is followed below:

from BW_Classes import Character

pick = Character()
pick.characterInfo()

# Object created for inner class -- Buildable()
build = pick.buildObj
build.displayBuildable()

print()
print("Building......")
build.createBuildable("Fire 2")
build.createBuildable("Coal Mine")
build.createBuildable("Coal Supreme")
build.createBuildable("Ocean")
build.displayBuildable()

print()
print("Upgrading....")
build.upgradeBuildable("Fire 2")
build.displayBuildable()
1
  • 2
    Welcome to Salesforce StackExchange! I think you probably stumbled here after searching for "maximum recursion depth exceeded", but this site is focused on the Salesforce platform (a cloud-based Customer Relationship Management system). You're probably looking for Stack Overflow. Commented Oct 18, 2021 at 2:06

1 Answer 1

1

I believe what you are look for is Inheritance. Inheritance basically means that a class inherits the properties and methods of another class, which I am assuming you are looking for when you say nested classes.

The error is being thrown because when you initialize the character, the function initializes the buildable. When the buildable is initialized, it initializes the character. See the loop?

Your code will probably look something like this:

class Character(Buildable):
    def __init__(self):
        self.name = "Pickle"
        self.health = 100
        self.resources = ["Fire", "Coal"]

        #initialize the buildable class
        super().__init__()

    # Function to display character info
    def characterInfo(self):
        print("CharacterInfo --> " + "Name:", self.name, "| Health:", self.health, "| Resources:", self.resources)

    def printRes(self):
        print(self.resources)

    # Function for collecting resources
    def collectResource(self, newResource):
        self.resources.append(newResource)

class Buildable():
    def __init__(self):
        self.buildings = ["Fire 1"]
        self.upgradeStatus = 0

    def displayBuildable(self):
        print("Buildables -->", self.buildings)

    def createBuildable(self, newBuilding):
        self.buildings.append(newBuilding)

    def upgradeBuildable(self, replaceBuilding):
        if self.upgradeStatus == 0:
            # Update selected building
            for i in range(len(self.buildings)):
                if self.buildings[i] == replaceBuilding:
                    self.buildings[i] = "2x"
                    break
            print(Character.resources)
            self.upgradeStatus = self.upgradeStatus + 1
            print(self.upgradeStatus)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.