0

I am currently looking into implementing a save functionality to a small rpg I have made using the shelve module which uses pickle.

After finding out I couldn't pickle pygame surfaces I followed advice suggesting passing a dictionary key instead as my object attribute. Once I had spent hours fixing the game, systematically checking all the surfaces and actually getting the start menu up and running I got a new message error stating pygame clock objects can't be serialized either. Much to my dismay, since, unlike for the surfaces I can't pre create all the clocks I need since I use a lot of them (attack timers, animation timers, AI timers., etc ) all specific to given objects. I.e instead of having one attack timer for all my sprites I have one for each so that I have more flexibility. Furthermore, the number of clock will depend on the number of objects I create, and, to some extent this number is random. I really can't think of a way around while keeping an object oriented structure I.e avoiding generating 1000s timers and linking to objects.

So to recap on the questions I actually have:

1) how can I shelve pygame timers ?

2) if it is not possible what alternative would you recommend ?

3) before spending anymore time on implementing this save functionality, what other pygame objects are not serializable, or where could I find this information ?

I know I ask a lot but I feel quite frustrated having spent so much time getting those surfaces sorted just to have the same issue with timers and wondering what will be next... Although I guess it's all part of learning ;)

Sorade
  • 915
  • 1
  • 14
  • 29

1 Answers1

2

I believe you may be attempting to address your problem in the wrong way. I would say that instead of attempting to serialize python objects directly, you should stick to only saving the data that you actually need in the form of dictionaries.

I cannot think of a case when you would actually need to use more than one pygame clock. I do not know your exact situation, but I would recommend a structure like this.

clock = pygame.time.Clock()
time = 0

attack_clock1 = {'time_interval': 10, 'time': 0}
attack_clock2 = {'time_interval': 20, 'time': 0}
clocks = [attack_clock1, attack_clock2]

while True:
    #perform game logic here

    #use time variable to run other "clocks"
    #example
    for c in clocks:
        if condition:
            c['tick'] += 1
            if c['tick'] > c['time_interval']:
                #do something

    time += 1

    clock.tick(FPS)

Again, I do not know your exact situation, but I hope you can gather something useful from this sort of structure. This sort of advice would probably be better suited for a comment, but I do not have enough reputation to do so. Good Luck!

jasekp
  • 990
  • 1
  • 8
  • 17