Useful helper functions

Discussions about Coding and Scripting
Buggie
Adept
Posts: 420
Joined: Sat Mar 21, 2020 5:32 am

Useful helper functions

Post by Buggie » Thu Jul 02, 2020 10:56 pm

1. Get screen X Y coordinates for specified Location:

Code: Select all

simulated function getXY(Canvas C, vector location, out int screenX, out int screenY) {
	local vector X, Y, Z, CamLoc, TargetDir, Dir, XY;
	local rotator CamRot;
	local Actor Camera;
	local float TanFOVx, TanFOVy;

	C.ViewPort.Actor.PlayerCalcView(Camera, CamLoc, CamRot);

	TanFOVx = Tan(C.ViewPort.Actor.FOVAngle / 114.591559); // 360/Pi = 114.5915590...
	TanFOVy = (C.ClipY / C.ClipX) * TanFOVx;
	GetAxes(CamRot, X, Y, Z);

	TargetDir = Location - CamLoc;

	Dir = X * (X dot TargetDir);
	XY = TargetDir - Dir;

	screenX = C.ClipX * 0.5 * (1.0 + (XY dot Y) / (VSize(Dir) * TanFOVx));
	screenY = C.ClipY * 0.5 * (1.0 - (XY dot Z) / (VSize(Dir) * TanFOVy));
}
2. Draw text with outline around it:

Code: Select all

simulated function DrawTextClipped(Canvas C, int X, int Y, string text, Color outline) {
	local Color old;

	old = C.DrawColor;
	C.DrawColor = outline;

	C.SetPos(X - 1, Y - 1);
	C.DrawTextClipped(text, False);
	C.SetPos(X + 1, Y + 1);
	C.DrawTextClipped(text, False);
	C.SetPos(X - 1, Y + 1);
	C.DrawTextClipped(text, False);
	C.SetPos(X + 1, Y - 1);
	C.DrawTextClipped(text, False);

	C.DrawColor = old;
	C.SetPos(X, Y);
	C.DrawTextClipped(text, False);
}
3. Draw line between two points on screen with one pixel texture:
Slow! Don't use often. Better use another approach.

Code: Select all

simulated function DrawLine(Canvas Canvas, int x1, int y1, int x2, int y2) {
	local int i, j, n, dx, dy;
	if (x1 == 0 && y1 == 0) return;
	if (x2 == 0 && y2 == 0) return;
	dx = x2 - x1;
	dy = y2 - y1;
	if (Abs(dx) > Abs(dy)) {		
		if (dx == 0) return;
		if (dx > 0) j = 1; else j = -1;
		n = Min(Canvas.ClipX, Max(0, x2));
		for (i = Min(Canvas.ClipX, Max(0, x1)); i != n; i += j) {
			canvas.SetPos(i, y1 + dy*(i - x1)/dx);
			canvas.DrawRect(Texture'Botpack.AmmoCountJunk', 1, 1);
		}
	} else {
		if (dy == 0) return;
		if (dy > 0) j = 1; else j = -1;
		n = Min(Canvas.ClipY, Max(0, y2));
		for (i = Min(Canvas.ClipY, Max(0, y1)); i != n; i += j) {
			canvas.SetPos(x1 + dx*(i - y1)/dy, i);
			canvas.DrawRect(Texture'Botpack.AmmoCountJunk', 1, 1);
		}
	}
}
4. Get value from Actor (work with network play):

Code: Select all

simulated function string getVal(Actor actor, string prop) {
	local string ret;

	Root.Console.ViewPort.Actor.MyHUD.Role = actor.Role; // hack for avoid Enum set issues, here always ROLE_Authority before that
	actor.Role = actor.ENetRole.ROLE_Authority;
	ret = actor.GetPropertyText(prop);
	actor.Role = Root.Console.ViewPort.Actor.MyHUD.Role;
	Root.Console.ViewPort.Actor.MyHUD.Role = actor.ENetRole.ROLE_Authority;
	return ret;
}

Buggie
Adept
Posts: 420
Joined: Sat Mar 21, 2020 5:32 am

Re: Useful helper functions

Post by Buggie » Sat Dec 26, 2020 7:51 am

5. Replace first occurrence of substring with another substring. Useful for make placeholders in messages.

Code: Select all

static function string Replace(coerce string source, coerce string search, coerce string replace) {
	var int pos;
	
	pos = InStr(source, search);
	if (pos >= 0) {
		source = Left(source, pos) $ replace $ Mid(source, pos + Len(search));
	}
	
	return source;
}

User avatar
Barbie
Godlike
Posts: 2069
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: Useful helper functions

Post by Barbie » Sat Dec 26, 2020 2:27 pm

ad 5) It is also available in Engine.PlayerPawn (but replaces ALL occurrences):

Code: Select all

final function ReplaceText(out string Text, string Replace, string With)
Spoiler
Show

Code: Select all

{
	local int i;
	local string Input;

	Input = Text;
	Text = "";
	i = InStr(Input, Replace);
	while(i != -1)
	{
		Text = Text $ Left(Input, i) $ With;
		Input = Mid(Input, i + Len(Replace));
		i = InStr(Input, Replace);
	}
	Text = Text $ Input;
}
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett

Buggie
Adept
Posts: 420
Joined: Sat Mar 21, 2020 5:32 am

Re: Useful helper functions

Post by Buggie » Sat Dec 26, 2020 2:57 pm

I didn’t find this function (although I didn’t really look for it - I looked only at the static functions of the object).
In addition, I can say that there is a difference between the first replacement and the replacement of everything.
For placeholders, you don't need to replace all occurrences.
It depends on the placeholders used though. If they are all different, then there is no difference. If they are the same, then there is a difference, but the order should be fixed, which is not good.