• Advertisement
Sign in to follow this  

having multiple monsters in one text file

This topic is 1786 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

for a while in my text based game, monsters have been loaded via random variables applied to lists and that are therefore generated.


However, for a while i have wondered how i could have all the monster attributes in an easily modifiable text file containing all the data.


i know how it will work, what it do is for example, i will call foo.txt





bites #success verb

bite #fail verb 








the trouble is that i don't know what function to use as some of the information from help() is vague about arguments.


what i am looking for is a function that looks at one of these random variables and can move the reader 'head' to the exact file line specified by the random variable



say i wanted to load rabbit:

function_name(1) #since rabbit is the first line of foo.txt


is there a function like this?

if not, how do i replicate a function like this to put inside my game? not in terms of parameters etc. just what combination of function calls and loops etc do i need?  



thanks a million


edit: it is python

Edited by game of thought

Share this post

Link to post
Share on other sites

you would need to find or create a file reader/manager.  and then use it to sort out how to read the text file.  I found a simple one going through a tutorial on youtube.  its the platformer mad easy of c#.  its done by the guy from the site codingmadeeasy.com.  just check out the filemanager portion of his platformer tutorial.  


it reads in a file and goes through it till it finds the correct identifier, in your case monster name.  Then you can have multiple monsters on one file and load the correct one based on the programs need.  it is simple to set up and easy to use.  there are others already made for you out there that use other file formats, such as xml.  but they seem a little complicated for what you seem to be looking for.

Share this post

Link to post
Share on other sites

You can store these things in json file ("example.json"):


	"monsters": [
			"name": "Rabbit",
			"hp": 30,
			"attack": 5
			"name": "Rat",
			"hp": 40,
			"attack": 10


And load using python JSON library (included in Python 2.6):


import json

config_file = open('example.json')
data = json.load(config_file)

for monster in data["monsters"]:
    print monster
Edited by Nercury

Share this post

Link to post
Share on other sites

you could also use an xml parser such as tinyXML, this would give you a bit more control/information to someone editing a text file, for example, you might do:


<Monster name="Rabbit">
 <Attribute Name="Hp" Value="10"/>


then all you have to do is go through and look for all monsters tags, then read their child tags to get information about the monster.

Edited by slicer4ever

Share this post

Link to post
Share on other sites

You can also use the ini format and use the python build-in module called config parser.

Here is the Doc: http://docs.python.org/2/library/configparser.html


Your file format would be:



hp = 30
attack = 5
attack_succeeded = "bites %s "
attack_failed = "bite %s"

hp = 40
attack = 10
attack_succeeded = "nibbles %s"
attack_failed = "nibble %s"

Share this post

Link to post
Share on other sites
When I started on my latest RPG, I got away from storing character definitions in a solid binary format that required me to build a separate editor. At first, I went text-base for quick-and-easiness, but eventually, I went with XML to store my enemy definition data. I used this approach to store battle configs for random battles encountered for that map too: assuming the battle config file was loaded and each enemy has an internal name to refer to it, you could create an element like <config>...</config> for each random battle enemy config, and provide an <enemy name="small_goblin"/>, or something like that.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement