Writing text out

Started by Darius Poyer, Fri 13/08/2010 13:00:07

Previous topic - Next topic

Darius Poyer

I've found it difficult to find an answer to this problem but hopefully it's simple.

I want text that appears in game to be written out, like... any game handles text. I simply want each character to appear in sequence at variable speeds.
I know it's possible and perhaps ridiculously easy, so how could I implement such a function?

Khris

#1
It depends...
You could do this:

Code: ags
void WriteOut(this Label*, String message, int delay) {
  int i = 1;

  int d1 = delay/2;
  int d2 = d1;
  if (d1+d2 < delay) d2++;

  while (i <= message.Length) {
    this.Text = message.Substring(0, i);
    Wait(d1);
    i++;
    if (IsKeyPressed(eKeySpace) || Mouse.IsButtonDown(eMouseLeft)) i = message.Length;
    Wait(d2);
  }
}


  lblMessage.WriteOut("Please press space to continue.", 5);
will write out the line blocking on the label, at eight characters per second, skipping to the full line after a left click or space is pressed.

The downside is that this will obviously only work for one line of text.
To display multiple lines, one could use DrawingSurface.DrawStringWrapped(), the thing is that if a word won't fit at the end of a line, it still starts getting written out there and only wraps to the next line after it becomes too long.

The visually pleasing alternative is breaking down the message into single words, then reassembling them into lines that fit the available width.
After that, line by line is written out.

I did this when I started experimenting with RPG stuff, here's a very short demo: RPG

The code that invokes the messages:
Code: ags
    ShowBox(true, "Hello! ] This a first test of my new message box function!");
    ShowBox(false, "It's actually working, I can't believe it! ] Nice!");


Space to bring up, N to skip, Esc to quit.

As you can see, all depends heavily on how exactly you want the text to be displayed.

SMF spam blocked by CleanTalk