What's an efficient way of storing data in python, like a database?
I need to store data for stats in a game, as well as enemy information.
Storing data in python
Forum rules
Ren'Py specific questions should be posted in the Ren'Py Questions and Annoucements forum, not here.
Ren'Py specific questions should be posted in the Ren'Py Questions and Annoucements forum, not here.
-
- Regular
- Posts: 52
- Joined: Sat Feb 09, 2008 1:50 pm
- Projects: SSS:Renaissance, Canadense (WIP)
- Contact:
Re: Storing data in python
There are a few ways of doing it, depending on comfortable you are with programming.
The simplest would be to use a python dictionary, which stores data as key-data pairs. An empty dictionary is initialised with curly braces: And then you assign data to the dictionary thus: A good way of tracking stats for a game would be to store the character's name as the key in a dictionary, and then store a dictionary of the various stats as the data. (Basically we are storing a bunch of dictionaries inside a master dictionary) Then when you want to get the data back again:
A more powerful way of doing this would be to use classes and object oriented programming techniques, but this is more complicated and probably not worth it if you just want to store some data in a sensible way.
The simplest would be to use a python dictionary, which stores data as key-data pairs. An empty dictionary is initialised with curly braces:
Code: Select all
D = {}
Code: Select all
D['Name'] = 'James'
D['Age'] = 35
D['Health'] = 100
Code: Select all
Stats = {}
James = {}
James['Health'] = 78
James['Money'] = 12
Mark = {}
Mark['Health'] = 21
Mark['Money'] = 156
Stats['James'] = James
Stats['Mark'] = Mark
Code: Select all
Stats['Mark']['Health']
>>> 21
-
- Newbie
- Posts: 10
- Joined: Thu Jul 16, 2009 8:13 pm
- Projects: Unbeatable Battles (Pre-production)
- Contact:
Re: Storing data in python
Thank you!
I'll be using this method for data storage!
I'll be using this method for data storage!
-
- Newbie
- Posts: 10
- Joined: Thu Jul 16, 2009 8:13 pm
- Projects: Unbeatable Battles (Pre-production)
- Contact:
Re: Storing data in python
Actually, I'm familiar with OOP.
I'd actually like to know how to store data with Class objects.
I'd actually like to know how to store data with Class objects.
- killdream
- Veteran
- Posts: 325
- Joined: Wed Nov 05, 2008 1:05 pm
- Projects: EVūL (WIP), insilo (WIP), Cute Demon Crashers!
- Deviantart: robotlolita
- Github: robotlolita
- Location: World's End (aka Brazil)
- Contact:
Re: Storing data in python
Eto... just create a class? o.oMew151 wrote:I'd actually like to know how to store data with Class objects.
Code: Select all
class human:
# the object constructor
def __init__(self, age, sex, weight, height):
self.age = age
self.sex = sex
self.weight = weight
self.height = height
Code: Select all
james = human(31, #age
"male", #sex
67, #weight (in kg)
170, #height (in cm)
)
Code: Select all
class MaleHuman(human):
def __init__(self, age, weight, height):
human.__init__(self, age, "male", weight, height) # inherits from base class
-
- Regular
- Posts: 52
- Joined: Sat Feb 09, 2008 1:50 pm
- Projects: SSS:Renaissance, Canadense (WIP)
- Contact:
Re: Storing data in python
I think most people persist data in python using pickle, cpickle (basically just a faster pickle) or shelve. Renpy probably has its own recommended way of storing things though.killdream wrote: If you need persistent data, you can use CVS files, or write a file format of your own using xdrlib/another module or getting the pain of writing/reading it directly using binary data. Or you can even use some DBGS system like MySQL and the likes (which I don't recommend, anyways, unless you're making a MMO game).
Who is online
Users browsing this forum: No registered users