So I wanted to code a function for my game that switches the current state of the program and based on the current state, the menu that is displayed is a different one.
When I call the switch_to("Main_Menu"), i receive the following error:
File "d:\PythonProjects\MenuSystem\Menu.py", line 12, in switch_to
for s in self.state.gamestates:
AttributeError: 'Game' object has no attribute 'gamestates'
It is a bit of code but really not complex at all. WHY is there an error with a Game Object when there is nothing done with a game object?
I tried so long to find out why this happens but im stuck at this problem... I hope you can help me.
Many thanks in advance !
This is the code from the main class:
game = Game()
gamestates ={
0: "Game",
1: "Main_Menu",
2: "Settings_Menu",
3: "Sound_Menu",
4: "Gamepause_Menu"
}
state = State(gamestates, "Main_Menu")
main_menu = Main_Menu(game, state)
main_menu.switch_to("Settings_Menu") # THIS CAUSES THE ERROR
This is the State Object class:
class State:
def __init__(self, gamestates: dict, current_state: str):
self.gamestates = gamestates
self.current_state = current_state
And lastly the Menu :
class Menu:
def __init__(self, game, state: State):
self.game = game
self.state = state
def switch_to(self, gamestate: str):
for s in self.state.gamestates:
if s == gamestate:
self.state.current_state = gamestate
return
raise Exception("No gamestate named '" + gamestate + "'")
class Main_Menu(Menu):
def __init__(self, game, state):
super().__init__(self, game, state)