How to create a dialog tree with a custom language in C #

0

I created a dialog language for a game, but in the step where I analyze the recursion, it is always generated incorrectly.

The syntax looks like this:

Chance(Num);Relation(Num);Reputation(Num):Dialog>Option0[RelationInc,ReputationInc],...

And to add recursion, the character '>' before any other, such as >Chance;Relation;Reputation:Dialog>Option[bla,bla]... , and so on.

In my code, I can pick up the number of '>' before each section, format the string and process it to have a dialog item, but when checking the recursion, it gets wrong.

Current Code:

public Dialog Compile(string source,string id)
{
   Dialog handler = new Dialog(id);
   List<DialogItem> controls = new List<DialogItem>();
   source = source.Replace("\r","");
   string[] lines = source.Split('\n');
   int curDepth = -1;
   foreach(var ln in lines)
   {
      string line = ln;
      int depth;
      bool global = processLine(ref line,out depth);
      var dialog = compileItem(line);
      dialog.Depth = depth;
      dialog.IsGlobal = global;
      //Begin
      if(global)
      {
         handler.Items.Add(dialog);
      }
      bool isParent = global || depth > curDepth;
      bool isDown = depth < curDepth;
      bool changed = false;
      if(isParent && !isDown)
      {
         controls.Add(dialog);
         changed = true;
      }
      if(isDown)
      {
         if(curDepth > -1 && curDepth < controls.Count)
         {
            controls.RemoveAt(curDepth);
         }
         curDepth = depth;
      }
      if(curDepth > -1 && curDepth != depth)
      {
         controls[curDepth].Childs.Add(dialog);
      }
      if(changed)curDepth = depth;
      //End
   }
   return handler;
}

Could anyone tell me some way to make it functional?

    
asked by anonymous 22.12.2017 / 23:23

1 answer

0

Yes, after several attempts, I myself have been able to solve the problem with the following code:

        string[] lines = source.Split('\n');

        Dialog holder = new Dialog(id);
        List<DialogItem> help = new List<DialogItem>();
        int oldDepth = -1;
        foreach(var ln in lines)
        {
            string line = ln;
            int depth;
            bool global = processLine(ref line,out depth);
            var dialog = compileItem(line);
            dialog.Depth = depth;
            dialog.IsGlobal = global;
            //Begin
            bool isParent = depth > oldDepth;
            bool isDown = depth < oldDepth;
            bool isEqual = depth == oldDepth;

            if(global)
            {
                holder.Items.Add(dialog);
            }

            if(isParent)
            {
                help.Add(dialog);
            }
            if(isDown)
            {
                help.RemoveAt(help.Count-1);
                for(int i=0;i<help.Count;i++)
                {
                    if(help[i].Depth == dialog.Depth)help[i] = dialog;
                }
            }
            if(isEqual)
            {
                help[help.Count-1] = dialog;
            }

            if((depth-1) > -1 && (depth-1) < help.Count)
            {
                help[depth-1].Childs.Add(dialog);
            }
            oldDepth = depth;
            //End
        }
    
26.12.2017 / 22:57