Using a single, repeated input (keypress) to "scroll" through text

Hi All!

This is my first question, so forgive my ignorance.

I am just beginning the Text101 game and using the space key as an input to continue. I want to be able to use the same space key repeatedly to have players scroll through various blocks of story before they get to various options for interaction with the environment (which would use specific key inputs like M, L, T, etc.). But when I press Space the first time, it just jumps down to the second block of text (Text 2) and skips the first block of text (Text 1).

I want to display a block of text, and have them press space. This would then display the next block of text, and prompt them again to press space to continue. How do I make it display one block at a time and use the same input repeatedly?

This is my code currently:

I have looked on the unity forums for awhile seeking a solution, but most of them are too complex for my level of understanding. I tried one code that included something about detecting if the button had been pressed before, and then if so, displaying the next block of text, but I could not get it to work and it used commands I was unfamiliar with. Plus, I want to use Space repeatedly through the whole story to continue when reading blocks of text.

Thanks for any help!

  1. Assign different buttons for text 1 and text 2, since you have used space for two “if statements”, it might confuse the compiler which one to print first.

2)And in the second “if statement” it will again be if (Input.GetKeyDown).

Hi Detham,

How about putting the first block of text into it’s own state and just use a single exit option (SPACE) which leads to the second block of text? One advantage of this approach is you can give the second block of text a key to “Go back” and read the first block again if the player wants to.

Cheers,

Rich

Hey there, @dethham!

Here’s my idea. I may edit later as I progress through this myself and see how Ben has us implementing states, etc:

Create an array of Text objects that will hold each “block” of text you want the user to scroll to. Make sure you keep track in comments how many are needed for each room (in each state). You will also need a single integer field to keep track of the index (position in the array) of the current block of text.

Then, whenever your player hits enter, you will either:

  • Increase that integer field by one
  • Decrease the integer field by a number equal to one less than the number of text blocks for that scene

The action you take will depend on the current index and state. If you are on the final text block in a state and they , you’ll do the latter to reset the blocks of text for that scene (this will loop the text for a scene continuously). Otherwise, you’ll do the former.

As an example, consider the following:

I have an array textBlocks as follows:

textBlocks = [ “Text0”, “Text1”, “Text2”, “Text3”. “Text4”. “Text5”, “Text6” ]
** the number at the end of each string is that string’s index in the array. **

The pseudocode may look like:

textBlocks =  [ "Text0", "Text1", "Text2", "Text3". "Text4". "Text5", "Text6" ] 
/**0-4 are in state 1, 5-6 are state 2**/
int index = 0


Update() 
{
     if (SpaceKeyDown) 
     {
          if ( state == state1 and index == 4) index = index - 4      
               /**there are 5 text blocks in state 1**/
          else if (state == state2 and index == 6) index = index - 1      
               /**there are 2 text blocks in state 2**/
          else index = index + 1
     }

     /** You'll also want your normal state logic to set index if the input
     /** changes the state, and you want to display the next textBlock

     TextBox.Text =  textBlocks[index]

}

Best coding practice would really dictate that you keep most of those “magic numbers” (random integers with no explicit reasoning) in variables instead of writing them out directly (this is for readability of your code). We would also put the last line inside wherever there’s a change in index instead of at the end of update for speed purposes. However, the above pseudocode will get the job done. To elucidate, let’s walk through an example. I’ll give the action the player does and the resulting index and text. Make sure you follow why the text and index are those values.

Action: Starts game
Index: 0
Text Displayed: Text0

Action: Spacebar pressed
Index: 1
Text Displayed: Text1

Action: Spacebar pressed 3x
Index: 4
Text Displayed: Text4

Action: Spacebar pressed
Index: 0
Text Displayed: Text0

Action: State Change (on, say, “M” key pressed)
Index: 5 (this is not handled by the pseudocode above. You’d set the index to the first index in the new state wherever you handled state changes.)
Text Displayed: Text5

Action: Spacebar
Index: 6
Text displayed: Text6

2 Likes