Page 1 of 1

Display HUD messages over actors

Posted: Mon Jul 06, 2015 8:42 pm
by darkmessenger84
Greetings.

With a little help from the wiki and wtg62's existing ACS code, I made a simple, fully working function with which to display HUD messages over actors.

Code: Select all

function void hudmessageonactor(int tid, int msgID, int hudX, int hudY, int xOffset, int yOffset, int range, str sprite, str text, int holdTime, str colour)
{
	
	int dist, angle, vang, pitch, x, y;
	
	if (hudX == 0) { hudX = 640; }
	if (hudY == 0) { hudY = 480; }

	if(sprite != -1)
	{
		
		SetFont(sprite);
		text = "A";
		//offset = 0.1;
		
	}

	SetHudSize(hudX, hudY, 1);
	x = GetActorX(tid) - GetActorX(0);
	y = GetActorY(tid) - GetActorY(0);

	vang = VectorAngle(x,y);
	angle = (vang - GetActorAngle(0) + 1.0) % 1.0;

	if(((vang+0.125)%0.5) > 0.25) dist = FixedDiv(y, sin(vang));
	else dist = FixedDiv(x, cos(vang));

	if ((angle < 0.2 || angle > 0.8) && (dist >> 16) < range)
	{
		
		if (GetActorPitch(0) >= -0.25 && GetActorPitch(0) <= 0.25)
		{
			
			pitch = VectorAngle(dist, GetActorZ(tid) - (GetActorZ(0) + 41.0));
			pitch = (pitch + GetActorPitch(0) + 1.0) % 1.0;
			
			if ((hudX/2) * sin(angle) != 0 && cos(angle) != 0 && (hudX/2) * sin(pitch) != 0 && cos(pitch) != 0) //	Fixes divide by zero
			{
				
				x = hudX/2 - ((hudX/2) * sin(angle) / cos(angle));
				y = hudY/2 - ((HUDX/2) * sin(pitch) / cos(pitch));
				
				x+=xOffset;
				y+=yOffset;

				HudMessage(s:text; HUDMSG_PLAIN, msgID, colour, (x << 16), (y << 16), holdTime);
				
			}
			
		}
		
	}
	
}
Using this function, it is possible to display player's names above their heads and suchlike. For example:

Code: Select all

script 1 ENTER
{

	Thing_ChangeTID(0, 1+PlayerNumber());
	
}

script 2 RESPAWN
{
	
	Thing_ChangeTID(0, 1+PlayerNumber());
	
}

script 3 ENTER CLIENTSIDE
{
	
	while(TRUE)
    {   
		
		for (int i = 1; i < PlayerCount(); i++)
		{
				
			if (PlayerInGame(1) && 1+PlayerNumber() != i && GetActorProperty(i, APROP_Health) > 0)
			{
				
				hudmessageonactor(i, i, 640, 480, 0, -120, 512, -1, StrParam(n:i), 0.1, CR_GREEN);	//	Displays nametags
				hudmessageonactor(i, i+64, 640, 480, 0, -112, 512, -1, StrParam(s:"Health: ",d:GetActorProperty(i, APROP_Health),s:"%"), 0.1, CR_WHITE);	//	Displays health

			}
			
		}

        Delay(1);
		
    }
	
}
Permission is granted to use this in your projects. :)

Best called from a 1 tic delay perpetual loop specifying a holdtime of 0.1.

RE: Display HUD messages over actors

Posted: Mon Jul 06, 2015 10:23 pm
by fr blood
Wow thanks I was searching for a function like that, so I've a question, if I put a message above an actor will that message be viewable by all players on the game or only the activator of the script because I see that you are using "HudMessage" instead of "HudMessageBold" and I need that to place some objectives in my project.

RE: Display HUD messages over actors

Posted: Mon Jul 06, 2015 10:28 pm
by Ænima
fr blood wrote: Wow thanks I was searching for a function like that, so I've a question, if I put a message above an actor will that message be viewable by all players on the game or only the activator of the script because I see that you are using "HudMessage" instead of "HudMessageBold" and I need that to place some objectives in my project.
All of the name-drawing is done on each player's end separately, using HudMessageBold would be a very bad idea for this because it would draw text all over the screen for every client instead of just what they're supposed to see.

RE: Display HUD messages over actors

Posted: Mon Jul 06, 2015 10:35 pm
by darkmessenger84
I meant a holdtime of 0.1 not 0. derp

RE: Display HUD messages over actors

Posted: Thu Jul 09, 2015 10:04 am
by Vincent(PDP)
Don't do like this:

Code: Select all

for (int i = 1; i < PlayerCount(); i++)
If PlayerCount() returns 3, and someone has ID 5, his or hers name will not be displayed.
Do like this instead:

Code: Select all

for (int i = 1; i <= 64; i++)
{
    if (PlayerInGame(i-1))
    {
        //Your code goes here
    }
}
And since i starts at 1, you must use the <= operator because the last player WILL have TID 64.
The players' IDs won't change until they disconnect, which means that if there one time were six players or more and everyone except 0, 1, and 5 disconnects, PlayerCount() will only return 3 and the loop will then not apply to the player with ID 5.

RE: Display HUD messages over actors

Posted: Thu Jul 09, 2015 3:09 pm
by darkmessenger84
Thanks for pointing out that oversight, for which I appreciate.