Reputation: 1
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()
Upvotes: 0
Views: 112
Reputation: 43
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)
Upvotes: 1