Using XML to populate lists using classes.

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
Thundy
Regular
Posts: 88
Joined: Tue Dec 05, 2017 9:08 am
Contact:

Using XML to populate lists using classes.

#1 Post by Thundy »

This little code snippet is for anyone wanting to use XML in conjunction with renpy. Is tested and working at the time of posting

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>
The code for creating our lists is as follows:

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))
Now all you need to do is add more items to the XML file in the same format as the examples

Post Reply

Who is online

Users browsing this forum: No registered users