Trying to parse external files to drive content

Timbone316Timbone316 Posts: 2Member
edited September 2016 in Programming

So I wanted to make a simple application. I like ot play fighting games (Mortal Kombat, Street Fighter, etc). I have found web pages and printables with movelists of how to do various moves for each character, so I decided to consolidate all of these into an app I could put on a phone or tablet so when i play the games, i can click on a game, click on a character, and be given their moves right before me. I was planning on it being a simple app. I have symbols for each of the inputs (directions, punches, kicks, etc.), and had made a scene that was a horizontal bar with a text label on it. My plan was to populate a scene with these bars, use the label to label the move, and then add sprites for each of the inputs (for example, in mortal kombat, Scorpion's harpoon would have a text label with the text "Harpoon", followed by a "back" sprite, a "back" sprite, and a "punch_low" sprite. I was planning on creating XMl files for each game, which would include inside of them the list of characters, and each character would have a list of moves, and each move would have a list of inputs. I am trying to get the XMLParser to work and canNOT figure it out, at all. Google searches are pointless, and the most help I've gotten is a generic reply that got me to use the .read() command after the .open() command, so now i can get to my root node, but can't get to anything inside. Here is an XML snippet to begin to show what I was thinking of creating to accompany this:

<?xml version="1.0" encoding="UTF-8"?>
<chars>
    <char>
        <name>Liu Kang</name>
        <moves>
                <move>
                        <name>Fireball</name>
                        <inputs>
                                <input>
                                        <type>Sprite</type>
                                        <label>fwd</label>
                                </input
                                <input>
                                        <type>Sprite</type>
                                        <label>fwd</label>
                                </input
                                <input>
                                        <type>Sprite</type>
                                        <label>punch_high</label>
                                </input
                        </inputs>
                </move>
        </moves>
    </char>
</chars>

Using this code, i can get it to read the name "chars"

var data = XMLParser.new()
    data.open("res://assets/xmls/mk1.xml")
    data.read()
    data.skip_section()
    print(data.get_node_name())

and that's all i can do. I have tried reading the node_data, seeing if i could get attributes, etc. and nothing. Is there a better way than XML? I have looked at json parsing, but am not familiar with json so figured it would be even more difficult. Can i do this in a plain text file and read it?

Sigh... this will be such an easy thing to develop once i get this part figured out. Its just a bunch of data entry and loading the right file, then i have to add either a sprite or a label to show what the inputs are. Any help at all is appreciated. Thanks so much guys!

Tagged:

Tags :

Answers

  • vnenvnen Posts: 31Admin Godot Developer
    Answer ✓

    You have to keep calling data.read() to move through the nodes.

    However, if can switch JSON this will a lot easier. Parsing XML in Godot is very low level. With JSON you can just run var dict = {}; dict.parse_json(json_text) and the dictionary will be filled with the data. Saving is also easy since you can do dict.to_json(). You will have to use the File API to save/load the content from file, but it's not much of a burden and certainly easier than using XMLParser.

  • vnenvnen Posts: 31Admin Godot Developer

    BTW, if you want a sample code with XMLParser, you can check my Tiled Importer (link points to proper line where it starts): https://github.com/vnen/godot-tiled-importer/blob/master/addons/vnen.tiled_importer/tiled_map.gd#L398

    Tiled exports JSON so I use that by default, but also convert the XML format to JSON with this function.

  • Timbone316Timbone316 Posts: 2Member

    Thanks! I'm looking into json now.

  • Shin-NiLShin-NiL Posts: 158Moderator

    Yeah, working with json is much easier. I did convert all my XML files to json (using an online tool) to use on my Sokoban game.

Sign In or Register to comment.