Multiline text output (text with line breaks)

Discussions about Coding and Scripting

Multiline text output (text with line breaks)

Postby dot » Sun Nov 04, 2018 5:38 pm

This is a fairly obvious task, but I did not find information about it here.

For example, you need to send multiline text to the console, or draw it on the screen. DrawText and ClientMessage do not work correctly for line breaks.

Therefore, I have written several methods for this. You can use them in your code.

Code: Select all
function DrawText(Canvas C, coerce string Text, float X, float Y, optional bool CR) {
   local float _, DY;
   local int Pos;
   local string cCR, LF, Line;
   cCR = chr(13);
   LF = chr(10);
   C.StrLen("X", _, DY);
   while (true) {
      Pos = InStr(Text, LF);
      if (Pos == -1) break;
      Line = Left(Text, Pos);
      Text = Mid(Text, Pos + 1);
      if (Pos > 0 && Right(Line, 1) == cCR) Line = Left(Line, Pos - 1);
      C.SetPos(X, Y);
      C.DrawText(Line, CR);
      Y = Y + DY;
   }
   if (Text != "" && Right(Text, 1) == cCR) Text = Left(Text, Len(Text) - 1);
   C.SetPos(X, Y);
   C.DrawText(Text, CR);
}

function ClientMessage(Pawn Pawn, coerce string Text, optional name Type, optional bool bBeep) {
   local int Pos;
   local string CR, LF, Line;
   CR = chr(13);
   LF = chr(10);
   while (true) {
      Pos = InStr(Text, LF);
      if (Pos == -1) break;
      Line = Left(Text, Pos);
      Text = Mid(Text, Pos + 1);
      if (Pos > 0 && Right(Line, 1) == CR) Line = Left(Line, Pos - 1);
      Pawn.ClientMessage(Line, Type);
   }
   if (Text != "" && Right(Text, 1) == CR) Text = Left(Text, Len(Text) - 1);
   Pawn.ClientMessage(Text, Type, bBeep);
}
dot
Average
 
Posts: 53
Joined: Tue Oct 30, 2018 3:41 am

Re: Multiline text output (text with line breaks)

Postby JackGriffin » Sun Nov 04, 2018 8:36 pm

I ran into this very problem this morning. Mind If I use this (credited to you OFC)?
"You damn kids, back in my time we made the items, maps and games ourselves with an unwieldy engine using counter-intuitive crash-prone tools and we liked it so much we built communities around this which nowadays look like cults because they're quasi-parallel societies based on the same old games." -Hellkeeper
User avatar
JackGriffin
Godlike
 
Posts: 3750
Joined: Fri Jan 14, 2011 1:53 pm
Personal rank: Hack coder

Re: Multiline text output (text with line breaks)

Postby dot » Sun Nov 04, 2018 10:16 pm

I place this code for use by anyone. Credits do not need.
Also you can modify this code as you want.

Also I place this code here: https://wiki.beyondunreal.com/Legacy:Us ... _Functions
dot
Average
 
Posts: 53
Joined: Tue Oct 30, 2018 3:41 am


Return to Coding, Scripting

Who is online

Users browsing this forum: No registered users and 4 guests