0

I've been trying to make a text-based game and figured that I probably need a saving system. This is what I've done so far:

class Player:
    def __init__(self, health, defence, att, inventory, money, name):
        self.health = health
        self.defence = defence
        self.att = att
        self.inventory = inventory
        #set the inventory as a list and add items to it
        self.money = money
    def strike(self, enem):
        ...
    def addtoinv(self, item):
        ...

Here's a snippet of my code that makes the Player class.

Player = Player(20, 5, 0, [], 0)

Here I'm making the player object.

f = open("save_file.dat", "wb+")
pickle.dump([Player], f)

And here is where the issue occurs. I'm trying to serialize the object and save it to a file but instead it throws this error:

_pickle.PicklingError: Can't pickle <class '__main__.Player'>: it's not the same object as __main__.Player
0

1 Answer 1

1

You are overwriting your class Player with the variable Player. This is reassigning Player to an instance of the class instead of the class itself.

All you need to do is rename your variable to something else

p = Player(20, 5, 0, [], 0)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I thought it might have been the class that was the issue, but instead it was the name of the object being the same as the class.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.