the XML file is named data_file.xml in this example:
Code: Select all
<?xml version="1.0"?>
<data>
<junk>
<name>Apple</name>
<weight>1</weight>
<value>2</value>
<chancetodrop>25</chancetodrop>
</junk>
<junk>
<name>Orange</name>
<weight>1</weight>
<value>2</value>
<chancetodrop>25</chancetodrop>
</junk>
<armor>
<name>Burlap Robe</name>
<weight>2</weight>
<value>10</value>
<chanceToDrop>4</chanceToDrop>
<defense>5</defense>
</armor>
<armor>
<name>Leather Cuirass</name>
<weight>6</weight>
<value>15</value>
<chanceToDrop>2</chanceToDrop>
<defense>7</defense>
</armor>
<weapon>
<name>Dagger</name>
<value>11</value>
<weight>5</weight>
<chanceToDrop>2</chanceToDrop>
<attack>5</attack>
<block>2</block>
<speed>10</speed>
</weapon>
<weapon>
<name>Training Sword</name>
<value>1</value>
<weight>7</weight>
<chanceToDrop>4</chanceToDrop>
<attack>2</attack>
<block>5</block>
<speed>6</speed>
</weapon>
</data>
Code: Select all
init python:
#import os module and use it to open the file
import os
from xml.etree import ElementTree
f_name=(renpy.file('data/game_data.xml'))
#parse the file into DOM
dom=ElementTree.parse(f_name)
#pull out the information from dom into categories
junk = dom.findall('junk')
armor = dom.findall('armor')
weapons = dom.findall('weapon')
#define the base loot class
class loot(object):
def __init__(self, name, price, weight, rarity):
self.name = name
self.price = price
self.weight = weight
self.rarity = rarity
def __repr__(self):
return "Loot: '{}', {}, {}, {}".format(self.name, self.price, self.weight, self.rarity)
#create the loot list and append each Loot item from the file into the list
Loot = [ ]
for j in junk:
name = j.find('name').text
weight = j.find('weight').text
val = j.find('value').text
chanceToDrop = j.find('chancetodrop').text
weight = int(weight)
val = int(val)
chanceToDrop = int(chanceToDrop)
Loot.append(loot(name, val, weight, chanceToDrop))
#define the weapon class which inherits properties from the Loot class
class weapon(loot):
def __init__(self, name, price, weight, rarity, attackPower, blocking, attackspeed):
super(weapon, self).__init__(name, price, weight, rarity)
self.attackPower = attackPower
self.blocking = blocking
self.attackspeed = attackspeed
def __repr__(self):
return "Weapon: '{}', {}, {}, {}, {}, {}, {}".format(self.name, self.price, self.weight, self.rarity, self.attackPower, self.blocking, self.attackspeed)
#create the Weapons list and populate it with the items from the xml file
Weapons = [ ]
for j in weapons:
name = j.find('name').text
weight = j.find('weight').text
val = j.find('value').text
chanceToDrop = j.find('chanceToDrop').text
attack = j.find('attack').text
blocking = j.find('block').text
speed = j.find('speed').text
weight = int(weight)
val = int(val)
chanceToDrop = int(chanceToDrop)
attack = int(attack)
blocking = int(blocking)
speed = int(speed)
Weapons.append(weapon(name, val, weight, chanceToDrop, attack, blocking, speed))
#define the armors class which also inherits from the Loot class
class armors(loot):
def __init__(self, name, price, weight, rarity, defense):
super(armors, self).__init__(name, price, weight, rarity)
self.defense = defense
def __repr__(self):
return "Armor: '{}', {}, {}, {}, {}, {}".format(self.name, self.price, self.weight, self.rarity, self.defense)
#create the Armors list and populate it from the xml file
Armors = [ ]
for j in armor:
name = j.find('name').text
weight = j.find('weight').text
val = j.find('value').text
defense = j.find('defense').text
chanceToDrop = j.find('chanceToDrop').text
weight = int(weight)
val = int(val)
chanceToDrop = int(chanceToDrop)
defense = int(defense)
Armors.append(armors(name, val, weight, chanceToDrop, defense))