XML and dialogues.

Hello, I’m trying to implement a dialogue system for a game. I’m going to use xml cause I want the dialogues to be editable/added from outside of Unity. It’s the first time that I work with xml, but I already used serialize to read a xml successfully. Now I got 2 questions.

  • Say we have this class:

    public class Character {
    public string name;
    public float strength;
    public float vitality;
    }

EDIT: And an XML file like this(just a sketch):

<Dialog>
    <Option params = "0" value = "20" operator= "greaterThan">
        <Line text="Hello, #CharacterName"/>
    </Option>
</Dialog>

The attributes in Option means, for the parameter 0, have a value greater than 20. Where parameter 0 is the character’s strength. Is there way I can tell unity(or c# in this case) that 0 = Character.strength without resorting to something like a switch(cause I plan to have many more stats)? I’m looking for the best way to do it, cause probably I ignore it, so far I thought of switch or changing the numbers for words and using a dictionary. ENDOFEDIT.

  • This time I have a dialogue line,where an NPC says “Hello, ‘CharacterName’”. It will be imported from the xml as a string, so, when parsing. How can I replace that’ CharacterName for Character.name? I don’t think searching for a special character on every dialog line to replace the word after it is the best approach.

Sorry for the long question and if it doesn’t belong here just say so. Thanks for reading.

As for telling Unity which XML value is which variable, that would all be handlded directly in your class:

[XmlElement("name")]
public string name;
[XmlElement("strength")]
public float strength;
[XmlElement("vitality")]
public float vitality;

As for replacing the Charactername with the actual name, remember that you the simple string type has some fairly complex and powerful features: You can, for example, check if it contains a word or order of characters via string.Contains, or replace characters at defined positions with string.Remove, and so on. Just check on the MSDN: String Class for what will help you in the best way. If you don’t want to search for a character inside each dialogue string in the game, you could create a copy of the diaolgue XML file when a new campaign is started, and then go through that XML file via script and replace all occurences directly in the file, save it, and then use this file to parse.