Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Because I am rather new to parsing XML I can't think of a way to break the logic out of the XML and use it in code dynamically. I looked over http://stackoverflow.com/questions/372915/game-logic-in-xml-fileshttps://stackoverflow.com/questions/372915/game-logic-in-xml-files and really did not seem to answer my question on how to solve it in C#.

Because I am rather new to parsing XML I can't think of a way to break the logic out of the XML and use it in code dynamically. I looked over http://stackoverflow.com/questions/372915/game-logic-in-xml-files and really did not seem to answer my question on how to solve it in C#.

Because I am rather new to parsing XML I can't think of a way to break the logic out of the XML and use it in code dynamically. I looked over https://stackoverflow.com/questions/372915/game-logic-in-xml-files and really did not seem to answer my question on how to solve it in C#.

Added a bit more about the enum
Source Link
MarcusM
  • 33
  • 1
  • 5

Hopefully this helps anyone who happens to stumble over this problem like I did. You might not need the enum but it could be handy for later.

Hopefully this helps anyone who happens to stumble over this problem like I did.

Hopefully this helps anyone who happens to stumble over this problem like I did. You might not need the enum but it could be handy for later.

Added my solution based on accepted answer
Source Link
MarcusM
  • 33
  • 1
  • 5

Edit: Spent more time on this and with Blue's answer I was able to come up with a viable solution.

Sample XML

 <scene>
  <description>You enter a dark room </description>
  <condition type="itemEquip" itemType="Flashlight">
   <message>and do you do not have a light source.</message>
  </condition>
  <button text="Use flashlight" sceneIndex="2">
   <condition type="itemHave" itemType="Flashlight">
    <message> and you forgot your flashlight at home.</message>
   </condition>
  </button>
  <button text="Go back" sceneIndex="0"/>
 </scene>

Made it so that you can have a condition for a button and for the scene as separate checks, that way buttons can be also dynamic based on the condition class.

The condition class:

public enum ConditionType
{
    itemLevelReq,
    itemEquip,
    itemHas,
    playerLevel,
    playerAttribute,
    baseDefault
}

abstract class Condition
{
    public ConditionType type;
    public string failMessage = "Condition not met";

    public abstract bool IsThisConditionMet();
}

class BaseCondition : Condition
{
    ConditionType type = ConditionType.baseDefault;
    string failMessage = "Condition not met";

    override public bool IsThisConditionMet()
    {
        return false;
    }
}

//example concrete condition
class ItemHasCondition : Condition
{
    ConditionType type = ConditionType.itemHas;
    string itemType;
    int levelReq;

    public ItemHasCondition(String item, string fail)
    {
        this.itemType = item;
        this.failMessage = fail;
    }

    override public bool IsThisConditionMet()
    {
        if(Program.testPlayer.items.Contains(this.itemType)){
            return true;
        } else {
            return false;
        }
    }
}

Then here is the parser for the XML(Simplified)

private static void readThisXML()
{
    XDocument doc = XDocument.Load("Scenes.xml");

    foreach (XElement root in doc.Root.Elements("scene"))
    {
        SceneStruct scene = new SceneStruct();
        List<ButtonStruct> buttons = new List<ButtonStruct>();
        List<Condition> conditions = new List<Condition>();

        foreach (XElement element in root.Elements("button"))
        {
            ButtonStruct btn = new ButtonStruct();
            List<int> btnIndex = new List<int>();
            List<Condition> btnConditions = new List<Condition>();
            btn.text = (string)element.Attribute("text");
            //Maybe different indexes based on conditions?
            btnIndex.Add((int)element.Attribute("sceneIndex"));
            btn.btnIndex = btnIndex;

            foreach (XElement elementC in element.Elements("condition"))
            {
                if (elementC.Attribute("type").Value.Equals("itemEquip"))
                {
                    Condition con = new ItemHasCondition((string)elementC.Attribute("itemType"), (string)elementC.Element("message").Value);
                    btnConditions.Add(con);
                }
            }
            btn.conditions = btnConditions;

            buttons.Add(btn);
        }

        dwStruct.buttons = buttons;

        foreach (XElement element in root.Elements("description"))
        {
            scene.description = (string)element.Value;
        }

        foreach (XElement element in root.Elements("condition"))
        {
            if (element.Attribute("type").Value.Equals("itemEquip"))
            {
                Condition con = new ItemHasCondition((string)element.Attribute("itemType"), (string)element.Element("message").Value);
                conditions.Add(con);
            }
        }

        scene.conditions = conditions;

        sceneList.Add(scene);
    }
}

Hopefully this helps anyone who happens to stumble over this problem like I did.

Edit: Spent more time on this and with Blue's answer I was able to come up with a viable solution.

Sample XML

 <scene>
  <description>You enter a dark room </description>
  <condition type="itemEquip" itemType="Flashlight">
   <message>and do you do not have a light source.</message>
  </condition>
  <button text="Use flashlight" sceneIndex="2">
   <condition type="itemHave" itemType="Flashlight">
    <message> and you forgot your flashlight at home.</message>
   </condition>
  </button>
  <button text="Go back" sceneIndex="0"/>
 </scene>

Made it so that you can have a condition for a button and for the scene as separate checks, that way buttons can be also dynamic based on the condition class.

The condition class:

public enum ConditionType
{
    itemLevelReq,
    itemEquip,
    itemHas,
    playerLevel,
    playerAttribute,
    baseDefault
}

abstract class Condition
{
    public ConditionType type;
    public string failMessage = "Condition not met";

    public abstract bool IsThisConditionMet();
}

class BaseCondition : Condition
{
    ConditionType type = ConditionType.baseDefault;
    string failMessage = "Condition not met";

    override public bool IsThisConditionMet()
    {
        return false;
    }
}

//example concrete condition
class ItemHasCondition : Condition
{
    ConditionType type = ConditionType.itemHas;
    string itemType;
    int levelReq;

    public ItemHasCondition(String item, string fail)
    {
        this.itemType = item;
        this.failMessage = fail;
    }

    override public bool IsThisConditionMet()
    {
        if(Program.testPlayer.items.Contains(this.itemType)){
            return true;
        } else {
            return false;
        }
    }
}

Then here is the parser for the XML(Simplified)

private static void readThisXML()
{
    XDocument doc = XDocument.Load("Scenes.xml");

    foreach (XElement root in doc.Root.Elements("scene"))
    {
        SceneStruct scene = new SceneStruct();
        List<ButtonStruct> buttons = new List<ButtonStruct>();
        List<Condition> conditions = new List<Condition>();

        foreach (XElement element in root.Elements("button"))
        {
            ButtonStruct btn = new ButtonStruct();
            List<int> btnIndex = new List<int>();
            List<Condition> btnConditions = new List<Condition>();
            btn.text = (string)element.Attribute("text");
            //Maybe different indexes based on conditions?
            btnIndex.Add((int)element.Attribute("sceneIndex"));
            btn.btnIndex = btnIndex;

            foreach (XElement elementC in element.Elements("condition"))
            {
                if (elementC.Attribute("type").Value.Equals("itemEquip"))
                {
                    Condition con = new ItemHasCondition((string)elementC.Attribute("itemType"), (string)elementC.Element("message").Value);
                    btnConditions.Add(con);
                }
            }
            btn.conditions = btnConditions;

            buttons.Add(btn);
        }

        dwStruct.buttons = buttons;

        foreach (XElement element in root.Elements("description"))
        {
            scene.description = (string)element.Value;
        }

        foreach (XElement element in root.Elements("condition"))
        {
            if (element.Attribute("type").Value.Equals("itemEquip"))
            {
                Condition con = new ItemHasCondition((string)element.Attribute("itemType"), (string)element.Element("message").Value);
                conditions.Add(con);
            }
        }

        scene.conditions = conditions;

        sceneList.Add(scene);
    }
}

Hopefully this helps anyone who happens to stumble over this problem like I did.

Tweeted twitter.com/#!/StackGameDev/status/419865181662220288
deleted 2 characters in body; edited title
Source Link
House
  • 73.5k
  • 17
  • 188
  • 276
Loading
Source Link
MarcusM
  • 33
  • 1
  • 5
Loading