having multiple monsters in one text file

Started by
5 comments, last by Vincent_M 11 years, 1 month ago

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

Rabbit

30#hp

5#attack

bites #success verb

bite #fail verb

rat

40

10

nibbles

nibble

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

e.g.

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

Advertisement

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.

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

for monster in data["monsters"]:
    print monster

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"/>
 ...
</Monster>
...

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.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

thanks for the info, i will try it

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:


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

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

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

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.

This topic is closed to new replies.

Advertisement