Help with cPickle (Beginner)

This is the place for queries that don't fit in any of the other categories.

Help with cPickle (Beginner)

Postby manbearpig69 » Mon Jul 29, 2013 10:27 am

i'm not very experienced in python and i've ran into some trouble with cPickle, im making a text based adventure game and im trying to save some variables as integers however whenever i load those variables back in it seems to turn them into strings and i cant use the for mathematical equations here is my current load/save code
CurrentArmorV is what im mainly having trouble with aswell as UserName not saving at all,

Code: Select all
pickle_file = open("save.dat","w")
cPickle.dump(CurrentSword, pickle_file)     

Code: Select all
pickle_file = open("save.dat","r")
level = cPickle.load(pickle_file)
CurrentSword = cPickle.load(pickle_file)
CurrentArmor = cPickle.load(pickle_file)
CurrentArmorV = cPickle.load(pickle_file)     
CurrentSwordV = cPickle.load(pickle_file)
UserName = cPickle.load(pickle_file)
Last edited by Yoriz on Mon Jul 29, 2013 12:19 pm, edited 1 time in total.
Reason: Added code tags
Posts: 2
Joined: Fri Jul 19, 2013 9:09 am

Re: Help with cPickle (Beginner)

Postby Mekire » Mon Jul 29, 2013 12:15 pm

Please read this New Users, Read This

You need to put all the variables you wish to preserve into a dict or some such and then pickle that object.

Make a dict of your items:
Code: Select all
state_info = {"Name"   : UserName,
              "Level"  : Level,
              "Sword"  : CurrentSword,
              "SwordV" : CurrentSwordV,
              "Armor"  : CurrentArmor,
              "ArmorV" : CurrentArmorV}

And a save and load function:
Code: Select all
def save_game(filename,data):
    with open(filename,'wb') as myfile:

def load_game(filename):
    with open(filename,'rb') as myfile:
        data = cPickle.load(myfile)
    return data

Sample saving:
Code: Select all
Sample loading:
Code: Select all
new_info = load_game("test.txt")

Last edited by Yoriz on Mon Jul 29, 2013 12:22 pm, edited 1 time in total.
Reason: Moved the first line from my post to yours :)
New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Posts: 1627
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

Re: Help with cPickle (Beginner)

Postby Yoriz » Mon Jul 29, 2013 12:17 pm

You have not included the objects you are pickling.

I've never used pickle before but this is based on the python docs example and works ok.

Code: Select all
import cPickle

current_armor_v = 10

output = open('data.pkl', 'wb')
cPickle.dump(current_armor_v, output)

pkl_file = open('data.pkl', 'rb')
read_current_armor_v = cPickle.load(pkl_file)

print type(read_current_armor_v)
print 5 + read_current_armor_v

Code: Select all
<type 'int'>
New Users, Read This
Join the #python-forum IRC channel on!
User avatar
Posts: 1474
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Return to General Coding Help

Who is online

Users browsing this forum: snippsat and 8 guests