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,

Save:
Code: Select all
pickle_file = open("save.dat","w")
cPickle.dump(Level,pickle_file)
cPickle.dump(CurrentSword, pickle_file)     
cPickle.dump(CurrentSwordV,pickle_file)
cPickle.dump(UserName,pickle_file)
cPickle.dump(CurrentArmorV,pickle_file)
cPickle.dump(CurrentArmor,pickle_file)
pickle_file.close()


Load:
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)
pickle_file.close()
Last edited by Yoriz on Mon Jul 29, 2013 12:19 pm, edited 1 time in total.
Reason: Added code tags
manbearpig69
 
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:
        cPickle.dump(data,myfile)

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

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

-Mek
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 :)
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

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)
output.close()

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

print type(read_current_armor_v)
print 5 + read_current_armor_v

Output
Code: Select all
<type 'int'>
15
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests