Sonny Vesali
Sonny Vesali

Reputation: 119

I can't figure out why this AttributeError is persisting

Hello People of Stack Overflow [my first post, pardon any idiosyncrasies in the question structure, etc.] As the title states I got this piece of code where there is a clearly defined 'opening-scene' function in the Map class but I get an Attribute Error [The Manor class reference in the dictionary only has one function def enter(self):

Any suggestions/tips/explanations are greatly appreciated, Thanks!

Here is the Code typed out:

class Manor(Scenes):
"""First encounter class."""

def enter(self):
    """First encounter."""
    print("""plot fodder""")

class Map(object):
    """The Map w/ a data dictionary using classes."""

    scenes = {
        'manor': Manor(),
        'master_bedroom': MasterBedroom(),
        'dining_room': DiningRoom(),
        'escape_scene': EscapeTheHouse(),
        'death': Death(),
        'finished': Finished()
    }

    def __init__(self, start_scene):
        """Instance initiator."""
        self.start_scene = start_scene

    def next_scene(self, scene_name):
        """Move to next class."""
        value = Map.scenes.get(scene_name)
        return value

    def opening_scene(self):
        """Mechanism that starts."""
        return self.next_scene(self.start_scene)


class Engine(object):
    """Engine of the game class."""

    def __init__(self, scene_map):
        """instance."""
        self.scene_map = scene_map

    def play(self):
        """Engine of the class."""
        current_scene = self.scene_map.opening_scene()
        last_scene = self.scene_map.next_scene('finished')

        while current_scene != last_scene:
            next_scene_name = current_scene.enter()
            current_scene = self.scene_map.next_scene(next_scene_name)

        current_scene.enter()


a_map = Map.scenes[('manor')]
a_game = Engine(a_map)
a_game.play()

And here is the powershell error: line 159, in play current_scene = self.scene_map.opening_scene() AttributeError: 'Manor' object has no attribute 'opening_scene'

A

The class includes multiple conditional branches within the function, but that I think it might be out of the scope of the question, and if it isn't I'll add that as well

Let me know if I failed to include any more information!

Upvotes: 0

Views: 45

Answers (1)

Grismar
Grismar

Reputation: 31329

You assign Map.scenes[('manor')] to a_map:

a_map = Map.scenes[('manor')]

which is an instance of Manor(), as defined here:

scenes = {
        'manor': Manor(),
        # ...
}

You then pass that instance of Manor() to the Engine() constructor here:

a_game = Engine(a_map)

It assigns it to self.scene_map here:

def __init__(self, scene_map):
        """instance."""
        self.scene_map = scene_map

And then you call .play(), which does this:

current_scene = self.scene_map.opening_scene()

So, it is trying to call .opening_scene() on an instance of Manor(), but Manor has no opening_scene method, so it fails.

Upvotes: 1

Related Questions