Branching Text help.

I am trying to write a text driven game just to teach myself a few things. I am wondering if there is a better way to handle a branching style of text. I am currently using “if” statements yo handle everything but I find it very confusing to get everything in order. Is there a better way to handle this? I know the code is necessarily going to function the way it is (most of it does) but I think you can see what I am trying to do with the displayed code.

Thank You!

activeText = a string that’s displayed in the GUI.

public void StateUpdate()
		{

			//The following is the Attack cycle.

			if   (Input.GetKeyDown(KeyCode.A) && attackStatus == 0)
			{
				attackStatus = attackStatus +1;
			}

			if   (Input.GetKeyDown(KeyCode.A)  && attackStatus == 1)
			{
				activeText = attackText;
			}

			if   (attackStatus == 2) // 
									 
			{
				activeText = attackText2;
			}

			//The following is the Look cycle.

			if (Input.GetKeyDown(KeyCode.L)&& lookStatus == 0)
			{
				lookStatus = lookStatus +1;
				activeText = lookText;
			}			

			if (Input.GetKeyDown(KeyCode.L)&& lookStatus == 1 && interactStatus == 0)
			{
				activeText = lookText;
			}
			
			if (Input.GetKeyDown(KeyCode.L)&& lookStatus == 1 && interactStatus == 1)
			{
				activeText = lookText2;

			}

//			if (Input.GetKeyDown(KeyCode.L)&& lookStatus == 2 && interactStatus == 1);
//			{
//				activeText = lookText3;
//			}

			//The following is the Interact cycle.

			if (Input.GetKeyDown(KeyCode.I) && lookStatus == 1)
			{
				interactStatus = interactStatus+1;
				activeText = interactText2;

			}

			if (Input.GetKeyDown(KeyCode.I) && lookStatus == 0)
			{
				activeText = interactText;
			}

1 Answer

1

Option 1

You can try to use Switch statements for some of your code.

if (Input.GetKeyDown(KeyCode.L)&& lookStatus == 0)
{
  switch(lookStatus)
  {
  case 0:
    lookStatus = lookStatus +1;
    activeText = lookText;
    break;         
  case 1:
    switch(interactStatus)
    {
    case 0:
      activeText = lookText;
      break;
    case 1:
      activeText = lookText2;
      break;
    }
    break;
  }
}

You can also make methods to better organize things.

if(Input.GetKeyDown(KeyCode.L))
{
  look();
}

void look()
{
  switch(lookStatus)
  {
  case 0:
    lookStatus = lookStatus +1;
    activeText = lookText;
    break;         
  case 1:
    switch(interactStatus)
    {
    case 0:
      activeText = lookText;
      break;
    case 1:
      activeText = lookText2;
      break;
    }
    break;
  }
}

Lastly, You can use code folding with the “#region” directive.

#region Attack
//Stuff
#endregion

Option 2

Create a class to handle it.

class DialogState : Monobehavior
{
  public String[] dialog;
  private int current = 0;

  String getCurrentDialog()
  {
    return dialog[current];
  }

  Bool next()
  {
    current++;
    if(current >= dialog.Length)
    {
      return false;
    }
    else
    {
      return true;
    }
  }

  void reset()
  {
    current = 0;
  }
}

Then you can create GameObjects for each group of dialog and edit them in Unity.

That is awesome! Thanks so much. I will try this out tomorrow. I think this is exactly what I needed! I will come back and make sure I thumb up or let you know how it went.