Display HUD messages over actors

Discuss all aspects related to modding Zandronum here.
Post Reply
darkmessenger84
New User
Posts: 13
Joined: Mon Jul 06, 2015 8:32 pm
Location: Grantham, UK

Display HUD messages over actors

#1

Post by darkmessenger84 » Mon Jul 06, 2015 8:42 pm

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.
Last edited by darkmessenger84 on Mon Jul 06, 2015 10:46 pm, edited 1 time in total.

User avatar
fr blood
Frequent Poster Miles card holder
Posts: 995
Joined: Wed Mar 06, 2013 4:04 pm
Location: France

RE: Display HUD messages over actors

#2

Post by fr blood » Mon Jul 06, 2015 10:23 pm

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.

User avatar
Ænima
Addicted to Zandronum
Posts: 3583
Joined: Tue Jun 05, 2012 6:12 pm

RE: Display HUD messages over actors

#3

Post by Ænima » Mon Jul 06, 2015 10:28 pm

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.
Reinforcements: midgame Survival joining/respawning
Doom64: Unabsolved: Doom64 + Diablo II
ZandroSkins: a pack made by our community
AeniPuffs: 3D blood and bullet puff effects, free to use for your own mods
Squad Radio: a WASD-based radio chat menu, add your own custom sounds!
Mercenaries (on hold)
Image

darkmessenger84
New User
Posts: 13
Joined: Mon Jul 06, 2015 8:32 pm
Location: Grantham, UK

RE: Display HUD messages over actors

#4

Post by darkmessenger84 » Mon Jul 06, 2015 10:35 pm

I meant a holdtime of 0.1 not 0. derp

User avatar
Vincent(PDP)
Forum Regular
Posts: 527
Joined: Thu Mar 14, 2013 7:35 pm
Location: Sweden
Clan: My DOOM site
Clan Tag: <MDS>
Contact:

RE: Display HUD messages over actors

#5

Post by Vincent(PDP) » Thu Jul 09, 2015 10:04 am

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.
Last edited by Vincent(PDP) on Thu Jul 09, 2015 10:11 am, edited 1 time in total.
//Visual Vincent ( Vincent (PDP) )
- My DOOM site Team

My projects:
Spoiler: (Open)
Doom Writer
Escape From The Laboratory - Done
Escape From The Laboratory Part 2
Parkskolan Zombie Horde Map (ZM10) - Done
In game Admin Commands for Zandronum.
Achievement Mod for Zandronum
Stats mod

darkmessenger84
New User
Posts: 13
Joined: Mon Jul 06, 2015 8:32 pm
Location: Grantham, UK

RE: Display HUD messages over actors

#6

Post by darkmessenger84 » Thu Jul 09, 2015 3:09 pm

Thanks for pointing out that oversight, for which I appreciate.

Post Reply