Welcome to Dream.In.Code
Getting Help is Easy!

Join 135,955 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,648 people online right now. Registration is fast and FREE... Join Now!




Convert a string to a class name

 
Reply to this topicStart new topic

Convert a string to a class name

Astano
14 May, 2008 - 04:33 AM
Post #1

New D.I.C Head
*

Joined: 9 Jul, 2007
Posts: 4


My Contributions
I am beginning to learn python and am creating a text based rpg. I am having a problem with a bit of the code. I have code that writes details to a save file including player stats etc.

This also saves the players current location which is a reference to an instance of a class. Not sure if this is needed, but this is the code for the location file:

CODE

class Locations:
    def __init__ (self, x, y, north, south, east, west):
        self.x = x
        self.y = y
        self.n = north
        self.s = south
        self.e = east
        self.w = west
        
#Initialise the locations
a1_1 = Locations(1,1,0,0,1,0)
a1_3 = Locations(1,3,0,0,1,0)
a2_1 = Locations(2,1,0,1,1,1)
a2_2 = Locations(2,2,1,1,0,0)
a2_3 = Locations(2,3,1,0,1,1)
a3_1 = Locations(3,1,0,0,0,1)
a3_3 = Locations(3,3,0,0,0,1)



#Set the directional variables to point to the location a player
#would be in if they moved in that direction
a1_1.e = a2_1
a1_3.e = a2_3
a2_1.s = a2_2
a2_1.e = a3_1
a2_1.w = a1_1
a2_2.n = a2_1
a2_2.s = a2_3
a2_3.n = a2_2
a2_3.e = a3_3
a2_3.w = a1_3
a3_1.w = a2_1
a3_3.w = a2_3


The method I use to save and then to load is below:

CODE

                save = raw_input("Do you want to save?")
                if save == "y":
                        SaveString = str(Player1.n) + "\n" + str(Player1.l) + "\n" + str(Player1.hp) + "\n" + \
                                         str(Player1.a) + "\n" + str(Player1.d) + "\n" + str(Player1.xp) + "\n" + \
                                         str(Player1.maxhp) + "\n" + str(Player1.heal) + "\n" + \
                                         str(Player1.dmin) + "\n" + str(Player1.dmax) + "\n" + str(Player1.g)
                        SaveGame = open("save.txt",'w')
                        SaveGame.write(SaveString)
                        SaveGame.close()

CODE
#Initialise the player with default stats
Player1 = Player('Default',1,30,30,10,2,2,0,1,3,100)

#The bit that loads a previously saved game - at the moment there can only be one saved game at a time
LoadGame = raw_input("Do you want to continue on a previously saved adventure?")

if LoadGame == "y":

        z = open('save.txt','r')
        x = z.readline()        
        Player1.n = x[:-1]
        Player1.l = int(z.readline())
        Player1.hp = int(z.readline())
        Player1.maxhp = int(z.readline())
        Player1.heal = int(z.readline())
        Player1.a = int(z.readline())
        Player1.d = int(z.readline())
        Player1.xp = int(z.readline())
        Player1.dmin = int(z.readline())
        Player1.dmax = int(z.readline())
        Player1.g = int(z.readline())
        CurrentPosition


        print "\nWelcome back " + Player1.n + ", good to see you again."


There is nothing currently to save the location as my problem is that when I load the info back into the variables it is loaded as a string and that won't point to the class instance I need it to.

I hope that makes sense?

What I need to be able to do is convert the string into something that points to the correct instance of the class Locations.

Can anyone help?

(Just incase it is needed I have zipped up the complete set of files I use to run the game.)


Attached File(s)
Attached File  AstanoRPG.zip ( 3.73k ) Number of downloads: 30
User is offlineProfile CardPM
+Quote Post

.Maleficus.
RE: Convert A String To A Class Name
16 May, 2008 - 02:35 AM
Post #2

D.I.C Head
**

Joined: 7 Mar, 2008
Posts: 126



Thanked: 2 times
My Contributions
http://python.active-venture.com/lib/module-pickle.html

More specifically, http://python.active-venture.com/lib/pickle-inst.html.
User is offlineProfile CardPM
+Quote Post

Astano
RE: Convert A String To A Class Name
17 May, 2008 - 09:33 AM
Post #3

New D.I.C Head
*

Joined: 9 Jul, 2007
Posts: 4


My Contributions
Thank you very much! That's an enormous help smile.gif

This post has been edited by Astano: 17 May, 2008 - 09:33 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 09:15AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month