[Tip] How to add text that doesn't fit on screen

So I wrote out my story and quickly found out it didn’t all fit on the screen for any one state, and I wanted to have it all, so I decided to split the text up into sections and have the player press the space bar to continue to the next part of the text, as dialogue usually goes in video games.

The code is below, a pretty simple change. Feel free to try to figure it out for yourself, it’s not very difficult. :slight_smile:

P.S. I call my states “phases” in my code.

  1. In your State scriptable object, turn you “Game Text” into a string array and return that array.
  2. In the Game Controller create a string currentGameTextArray to hold the string array returned from the State.
  3. Create an endOfPhaseFlag boolean to determine when the end of the State text has been reached and the player can make a decision.
  4. Create an int currentGameTextIndex to keep track of which string index you are currently on.
  5. In the CheckInput() add in:
private void CheckInput()
    {
        if (endOfPhaseFlag == false)
        {
            if (currentGameTextIndex < (currentGameTextArray.Length - 1))
            {
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    currentGameTextIndex++;
                    UpdateGameText(currentGameTextArray[currentGameTextIndex]);
                }
            }
            else
            {
                endOfPhaseFlag = true;
            }
        }
        else if (endOfPhaseFlag == true)
        {
            if (Input.GetKeyDown(KeyCode.Alpha1))
            {
                currentPhase = currentNextPhasesArray[0];
                currentGameTextIndex = 0;
                UpdateCurrentTurnData();
            }
            else if (Input.GetKeyDown(KeyCode.Alpha2))
            {
                currentPhase = currentNextPhasesArray[1];
                currentGameTextIndex = 0;
                UpdateCurrentTurnData();
            }
        }
    }

While you have not reached the last string in your string array, check for player spacebar input. When pressed, the game index will be increased, the game text component will be updated in the UpdateGameText(), and it will check if it has reached the end of the string array.

When it does, it will let the player switch states.

Looking at it now, I can probably get rid of the Flag and just have it check against the array length.

Anyways, hope this helps. :slight_smile:

2 Likes

Privacy & Terms