If I have created my own class which has some attributes that are pygame.Surfaces and I would like to save this object to a file, how can I do this as an error occurs when I try.
The class which I have created is an object which is essentially the following (Item class is included because the player has items which have attributes that are pygame.Surfaces):
class Item:
def __init__(self,name,icon):
self.name = name
self.icon = pygame.image.load(icon)
class Player(pygame.Sprite):
def __init__(self,{some_attrs})
skins = [{a pygame.Surface from a load image},{another}]
self.money = 0
self.items = [{Item object}]
Then when I try to save, I use the following:
with open('save.dat','wb') as file:
pickle.dump(player , file , protocol = 4)
#I have also tried without the protocol argument
But I get the following error:
Traceback (most recent call last):
File "{file path}", line 883, in <module>
save_progress()
File "{file path}", line 107, in save_progress
pickle.dump(player,file,protocol=4)
TypeError: can't pickle pygame.Surface objects
FYI, Everything that I have put in curly braces ({ and }) is just something that I have abbreviated or left out because it wasn't needed
If you need any more detail, I can easily add it if you reply what you need