Multiline text output (text with line breaks)

Discussions about Coding and Scripting
dot
Average
Posts: 53
Joined: Tue Oct 30, 2018 3:41 am

Multiline text output (text with line breaks)

Post by 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);
}

JackGriffin
Godlike
Posts: 3766
Joined: Fri Jan 14, 2011 1:53 pm
Personal rank: -Retired-

Re: Multiline text output (text with line breaks)

Post by 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)?
So long, and thanks for all the fish

dot
Average
Posts: 53
Joined: Tue Oct 30, 2018 3:41 am

Re: Multiline text output (text with line breaks)

Post by 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