• Advertisement
Sign in to follow this  

[Python] Pickling and Class Instances

This topic is 2569 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I've had this problem for several days now, and I'm not sure what to do about it. I'm writing a game prototype in Python, using PyGame. I save my game data using the pickler module; the game is all contained within a handful of instances, so I dump these three instances into the pickle file, and voilà. Almost everything worksfine upon loading the game again, with one single exception. Instances of the class Projectile no longer interact properly with instances of the class Tile (i.e. map tiles). I've basically isolated the problem: it appears that Projectiles created after loading the game no longer consider themselves to be in a list inside the main GameWorld instance.

All projectiles, when created, are added to a list in the GameWorld instance called self.gameObjects; additionally, they all have a variable called self.game, which contains the GameWorld instance they are in. But within a projectile, if that projectile is fired after loading a game and I check whether the projectile is in the self.game's self.gameObjects, the result is False. I think that the projectile thinks the new, post-load GameWorld instance is not the same as the old post-load game instance, but I can't figure out why.

I managed to get things working somewhat by having the GameWorld and Projectile classes in the same file, and having the projectile operate with game.gameObjects (game being the instance of GameWorld created in the same file) instead of self.game.gameObjects, but this solution doesn't allow me to bring my classes tidily into some other folder. I'm at a loss; can someone tell me how Pickler might mess with instances? I've read the documentation part on Pickler, but I'm not at a level where I can easily understand what they're talking about.

Failing that, here is the prototype, at the moment. M switches between game modes; in Gameplay mode, left-clicking shoots projectiles. F4 saves the game. In the starting menu, arrows up and down change selection.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement