Getting a parametric L-System formula into code

Beginner here. I need help understanding how i introduce parameters into an lsystem. I know how to code it when its only strings and characters with a ruleset like
‘X’, “[FX[-FX]+FX]”
‘F’, “F”

But when its parameters i get confused. I understand the formula and all but have no clue how to plot it into C#.
Like, id : pred : cond → succ
With an example.
Axiom : B(2)A(4, 4)
p1 : A(x, y) : y < 3 → A(x * 2; x + y)
p2 : A(x, y) : y >= 3 → B(x)A(x/y, 0)
p3 : B(x) : x < 1 → C
p4 : B(x) : x >= 1 → B(x - 1)

private void Generate()
    {
        currentString = axiom;

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < iterations; i++)
        {
            foreach (char c in currentString)
            {
                sb.Append(rules.ContainsKey(c) ? rules[c] : c.ToString());

            }

            currentString = sb.ToString();
            sb = new StringBuilder();
        }



        foreach (char c in currentString)
        {
            switch (c)
            {
                case 'F':
                    Vector3 initialPosition = transform.position;
                    transform.Translate(Vector3.up * length);

                    treeSegment = Instantiate(Branch);
                    treeSegment.GetComponent<LineRenderer>().SetPosition(0, initialPosition);
                    treeSegment.GetComponent<LineRenderer>().SetPosition(1, transform.position);
                    treeSegment.transform.parent = gameObject.transform;
                    break;

                case 'X':
                    break;

                case '+':
                    transform.Rotate(Vector3.back * angle);
                    break;

                case '-':
                    transform.Rotate(Vector3.forward * angle);
                    break;

                case '[':
                    transformStack.Push(new TransformInfo()
                    {
                        position = transform.position,
                        rotation = transform.rotation

                    });
                    break;

                case ']':
                    TransformInfo ti = transformStack.Pop();
                    transform.position = ti.position;
                    transform.rotation = ti.rotation;
                    break;

                default:
                    throw new InvalidOperationException("Invalid L-Tree operation");

            }
        }
    }

I would normaly use this to read the string for every character and then draw the lines. But how do you get the numbers from the string and read it like ‘‘B(2)’’ and not just ‘‘B’’,‘’(‘’,‘‘2’’ and so on?

I don’t see how Unity comes into the picture here… isn’t this just a C# string processing question?

StringBuilder and String have different ways of accessing their innards… are you sure you’re using them properly?

Here’s how to find out!

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on Android

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

You must find a way to get the information you need in order to reason about what the problem is.

You may want to have a look at my implementation I wrote for this question over here ^^. For it I rewrote my expression parser to include logic expressions as well. I’ve put it on my github if you’re interested. I haven’t done much with it yet. The example is a non parametric system to draw a hilbert curve.