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

Asked

Viewed 72 times

0

I created a dialogue creation language for a game, but at the stage where I analyze recursion, it is always generated incorrectly.

The syntax looks like this:

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

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

In my code, I can capture the number of '>' before each snippet, format the string and process it to have a dialog item, but when checking the recursion, it is erroneous.

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 someone tell me some way to make it functional?

  • How does the expression work for you? It could indicate a step-by-step of how it is executed and also indicate where the problem is in the step-by-step created?

  • Add a sample txt and the processLine() and compileItem methods()

  • Fortunately, after several hours, I discovered the solution through the old thinking and deduction, but I will provide the method I used to achieve, since I can not close the topic.

1 answer

0


Yes, after several attempts, I managed to solve the problem myself, 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
        }
  • Thanks anyway to all those who tried to help me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.