- Home /
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;
}
Answer by Spinnernicholas · Nov 27, 2013 at 11:42 PM
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.
with option 2, you could make a dialog database like so:
class DialogDatabase : $$anonymous$$onoBehavior, IDictionary<String, DialogState>
{
Dictionary<String,DialogState> dictionary;
//wire IDictionary to dictionary....
}
Then add a
public String name;
field to the DialogState class.
Create a gameobject and add DialogDatabase to it.
Create separate gameobjects and add a DialogState to each of them.
$$anonymous$$ake all the DialogStates children of the DialogDatabase.
Create an entry in DialogDatabase for each DialogStates using it's name as a key.
Tada, you can move the whole database of dialog around. You can store it as an asset and swap out entire databases on a whim.
I made this a comment because it is complex. $$anonymous$$ake sure you understand how generics and dictionaries work in C# before you rely too heavily on this code.
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.
Your answer
Follow this Question
Related Questions
Is there a reason GetKeyDown doesn't work with this code but GetKey does? 1 Answer
if statement error 1 Answer
Unity GUI Style with if Statements 1 Answer
imeCompositionmode 0 Answers
Can't call GetKey inside OnTriggerEnter? 2 Answers