Game getting sluggish for just 1 case in a switch

Hello all.

I am currently working on a game where the player can walk around a small town and talk (in depth) with the people he meets along the way. He can do quests, earn minor upgrades etc. This is just a proof for what might be a larger game.

Anyway, I have the player script which tells the object (character) they are selected, and then the conversation is handled with the chat script each character will have. Each with different dialogue. In order for me to get this system working correctly, I had to use 3 variables. One to record the answer chosen, this is a simple 1,2,3,4 (ABCD). Depending on which question the player chose. The next is a ConversationDepth variable that records how far through the conversation the player is, just adding one for each reply given.The last is a reply depth (really direction) that records which path has been taken through the conversation.

This is all working, but for some reason the game gets sluggish when I enter the case 1: for my convodepth switch. It’s not sluggish before, and not sluggish when it enters cases after that.

Here is the script (some of it anyway) I left out case 3, as it’s just the same as the others, but, as you can imagine exponentially longer!

This leads me to my second worry (two birds one stone, right?). I’m worried that the code will become vastly too large if I wish to incorporate all of the dialogue. Would I be better of leaving some initial dialogue within this script, but then perhaps have other scripts store further dialogue I can call to if a player get’s really chatty.?

Also, if this system is a complete mess, and you know of a better way of achieving this, please let me know :slight_smile:

private GameObject player;
private int conversationDepth; // This will store the number of replies from the player, so we can move through the conversation. 
public GUIText gTextReply;
public bool isSelected = false; // This lets us know if we have been selected by the player.
private string askMe_A; 
private string askMe_B;
private string askMe_C;
private string askMe_D; // This will most likely remain a goodbye/exit conversation option. 
private int actionTaken; // stores the number of the action chosen by the character. (interaction Window)// NOT IN USE YET!
private int replyGiven; // Determines which reply was given 1,2,3,4
private int replyDepth; //  Determines the directions the chat goes as we move through the conversation.
public string charatcerID = ("Soldier"); // This is the name of the character. This can be chnaged at anytime. In or out of code. 
public string GetCharacterID(){return charatcerID;} // Send the player our name when requested. 
public GUIText gTextA;
public GUIText gTextB;
public GUIText gTextC;
public GUIText gTextD;

void Start () {player = GameObject.Find("Player");}

void OnGUI(){ // Temp butons to collect player input.
	if(isSelected){
	if (GUI.Button(new Rect(20,640,20,20),"")){
		replyGiven = 1;
		conversationDepth = conversationDepth +1;}
		ChatLog();
	if (GUI.Button(new Rect(20,660,20,20),"")){
		replyGiven = 2;
		conversationDepth = conversationDepth +1;}
		ChatLog();
	if (GUI.Button(new Rect(20,680,20,20),"")){
		replyGiven = 3;
		conversationDepth = conversationDepth +1;}
		ChatLog();
	if (GUI.Button(new Rect(20,700,20,20),"")){
		replyGiven = 4;
		conversationDepth = conversationDepth +1;}
		ChatLog();}}

void Update () {
	//TextController playerText = player.transform.gameObject.GetComponent();
	if(!isSelected){
		askMe_A = (""); 
		askMe_B = ("");
		askMe_C = ("");
		askMe_D = ("");}
		//playerText.canSelect = true;} // Will be used once chat is refined. 
	
	if(isSelected){  // Display the questions.
	gTextA.text = askMe_A;
	gTextB.text = askMe_B;
	gTextC.text = askMe_C;
	gTextD.text = askMe_D;}}

void ChatLog(){
	switch (conversationDepth){
	case 0:{ // convodepth 0
			gTextReply.text = ("How can I help, young man?"); 
			askMe_A = ("What happened here?"); 
			askMe_B = ("I'm looking for a place to stay");
			askMe_C = ("Do you know where I might find some work?");
			askMe_D = ("I'm fine, thanks.");}break;
		
	case 1:{//convo depth 1
			switch (replyGiven){
		case 1:{
				gTextReply.text = ("The same thing that happens everytime there's an increase in magic usage"); 
				askMe_A = ("Words. Picked A, depth 1"); 
				askMe_B = ("Words. Picked A, depth 1 PICK ME!"); // 1 // Got here fine //should return replygiven 2 and RD 1 conDepth 2
				askMe_C = ("Words. Picked A, depth 1");
				askMe_D = ("Words. Picked A, depth 1");
				replyDepth = 1;
		
				print ("this is the replyDepth" + replyDepth);} break;
				
		case 2:{
				gTextReply.text = ("Response"); 
				askMe_A = ("Words. Picked B, depth 1"); 
				askMe_B = ("Words. Picked B, depth 1");
				askMe_C = ("Words. Picked B, depth 1");
				askMe_D = ("Words. Picked B, depth 1");
				replyDepth = 2;}break;
				
		case 3:{
				gTextReply.text = ("Response"); 
				askMe_A = ("Words. Picked C, depth 1"); 
				askMe_B = ("Words. Picked C, depth 1");
				askMe_C = ("Words. Picked C, depth 1");
				askMe_D = ("Words. Picked C, depth 1");
				replyDepth = 3;}break;
				
		case 4:{
				gTextReply.text = ("Very well."); 
				isSelected = false;
				}break;
		    }
		}break;
		
	case 2:{// convo depth 2
			
			if(replyDepth == 1){
			
			switch (replyGiven){
				
			case 1:{
				gTextReply.text = ("Response"); 
				askMe_A = ("Words. Picked A, depth 2, RD1"); 
				askMe_B = ("Words. Picked A, depth 2, RD1");
				askMe_C = ("Words. Picked A, depth 2, RD1");
				askMe_D = ("Words. Picked A, depth 2, RD1");
				replyDepth = 1;}break;
				
			case 2:{
				gTextReply.text = ("Response to PICKME!"); 
				askMe_A = ("Words. Picked B, depth 2, RD1 PickME!"); //0 //
				askMe_B = ("Words. Picked B, depth 2, RD1");
				askMe_C = ("Words. Picked B, depth 2, RD1 PICK me next");
				askMe_D = ("Words. Picked B, depth 2, RD1");
				print ("I was here, but must have fallen through.");
				replyDepth = 2;}break;
				
			case 3:{
				gTextReply.text = ("Response"); 
				askMe_A = ("Words. Picked C, depth 2, RD1"); 
				askMe_B = ("Words. Picked C, depth 2, RD1");
				askMe_C = ("Words. Picked C, depth 2, RD1");
				askMe_D = ("Words. Picked C, depth 2, RD1");
				replyDepth = 3;}break;
				
			case 4:{
				gTextReply.text = ("Response"); 
				askMe_A = ("Words. Picked D, depth 2, RD1"); 
				askMe_B = ("Words. Picked D, depth 2, RD1");
				askMe_C = ("Words. Picked D, depth 2, RD1");
				askMe_D = ("Words. Picked D, depth 2, RD1");
				replyDepth = 4;}break;
			}
		}
	}break;`

This code is indeed quite complex and not really optimal.
Something I noticed is that you call ChatLog() 4 times every single time OnGUI() is called because you put the call to ChatLog() outside each if(GUI.Button())

if (GUI.Button(new Rect(20,640,20,20),"")){
       replyGiven = 1;
       conversationDepth = conversationDepth +1;}
       ChatLog();

should actually be:

if (GUI.Button(new Rect(20,640,20,20),"")){
     replyGiven = 1;
     conversationDepth = conversationDepth +1;
     ChatLog();
}

And thats probably a large culprit to this. You should not be stingy with line breaks. I see you consistently place your curly braces oddly.
If anything you should do either of the following, since it makes the code a lot easier to read:

if(GUI.Label(new Rect(0,0,16,100), "Hello world")) {
    DoStuff();
}
DoOtherStuff();


or

if(GUI.Label(new Rect(0,0,16,100), "Hello world"))
{
    DoStuff();
}
DoOtherStuff();

but not

if(GUI.Label(new Rect(0,0,16,100), "Hello world"))
    DoStuff();}
    DoOtherStuff();

Because it is not that easy to see that “DoOtherStuff()” is not a part of the if-statement, which is what happens in your code.

So you should probably read up on some coding conventions information (for example here) so you get your code more readable and can avoid issues like this.

As for how your solution is I would probably try to figure out something more object oriented.
For example a Dialogue class which contains what the text should be and then a list with the different options and then setting it up in such a way that you can create a conversation tree in the editor with the inspector, which you then can save to prefabs or however you would want it done.

Would take some work, but if you want to have lots of conversations you probably need to put in that effort to make a general solution which makes it a lot faster and easier in the long run to make conversations.
You should see what information you can find in the forums etc about this. I would be surprised if there aren’t already available conversation engines that do exactly this, or at least tips on how to make one yourself.