Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am planing to Write a Text adventure and I don't know how to make the text draw letter by letter in any other way than changing the variable from h to he to hel to hell to hello That would be a terrible amount of work since there are tons of dialogue.

Here is the source code so far

protected override void Update(GameTime gameTime)
    //input code removed for clarity

    if (Dialogue == 1)
        Row1 = "Input Text 1 Here.";
        Row2 = "Input Text 2 Here.";
        Row3 = "Input Text 3 Here.";
        Row4 = "Input Text 4 Here.";
    if (Dialogue == 2)
        Row1 = "Text 1";
        Row2 = "Text 2";
        Row3 = "Text 3";
        Row4 = "Text 4";


protected override void Draw(GameTime gameTime)

    spriteBatch.Draw(sampleBG, new Rectangle(0, 0, 800, 600), Color.White);
    spriteBatch.Draw(TextBG, new Rectangle(0, 400, 800, 200), Color.White);
    spriteBatch.DrawString(defaultfont, Row1, new Vector2(10, (textheight + (rowspace * 0))), Color.Black);
    spriteBatch.DrawString(defaultfont, Row2, new Vector2(10, (textheight + (rowspace * 1))), Color.Black);
    spriteBatch.DrawString(defaultfont, Row3, new Vector2(10, (textheight + (rowspace * 2))), Color.Black);
    spriteBatch.DrawString(defaultfont, Row4, new Vector2(10, (textheight + (rowspace * 3))), Color.Black);
share|improve this question
"Write this code for me," type questions don't do well on this forum. What specifically is not happening that you want to have happen? – michael.bartnett Oct 11 '12 at 20:29
I agree with Michael. Additionally I disagree that it would be a terrible amount of work. You just have to write a function that takes a string, and amount of time it should take to print. You only have to write that once. Not for every line of dialog. Think a little more about algorithms and how you might accomplish your goals with simple code. – Byte56 Oct 11 '12 at 20:42
Oh im sorry That was not meant to be one of this Questions, Just like How that would be possible, but Byte`s Answer actually helped me and I figuered out how i could do it Thanks – Evi Oct 11 '12 at 20:52
@Evi I'm glad it was helpful. In the future, do try to only post code that's relevant to the question (see the edits I made). Also make sure to tell us what you've tried already and what about it didn't work. When you've solved this you can come back and answer your own question. Good luck with your game. – Byte56 Oct 11 '12 at 22:03
Type writer effect =) – Sidar Oct 12 '12 at 16:51

I won't write the code for you, but I can put forth a conceptual path forward.

Assuming your text is displayed in a string, it is possible to iterate through the character array that makes up that string. The goal here is to print each character individually (or however many at a time you want) separated by a delta time.

When you think about it, this isn't really any different than animating a sprite. Instead of moving the index into a spritesheet, you're moving the index into a string and appending that character to the displayed string.

So, for example:

string fullText = "Hello World";
string currentText = "";
int currentTextIndex = 0;
float timeSinceLastIncrement = 0;

    timeSinceLastIncrement += dt;

    if(timeSinceLastIncrement >= .5)
         currentText += fullText[currentTextIndex];
         timeSinceLastIncrement = 0;


Basically, you're maintaining the full text that will eventually be displayed, the current text that is being rendered, and the current index into the full text. dt is the delta time since the last update and timeSinceLastIncrement is the time that has passed since the last time you moved to the next character in the string. When that time (here in seconds) has gone past half a second, append the character to the current text, increment to the next character, and reset the time.

@ time 0, currentText = "";
@ time .5, currentText = "H";
@ time 1.0, currentText = "He";

And so on...

share|improve this answer
+1 Just what I was thinking. – Byte56 Oct 11 '12 at 21:55
It's probably a good idea to use StringBuilder instead of string for the partial string, to avoid unnecessary allocations. SpriteBatch.DrawString is overloaded to take either. – Andrew Russell Oct 12 '12 at 11:15
@AndrewRussell My answer is entirely pseudocode, but good note for him when he implements. – Justin Skiles Oct 12 '12 at 11:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.