Wayback Machine
Jun JUL Aug
Previous capture 25 Next capture
2002 2003 2004
1 captures
25 Jul 03 - 25 Jul 03
sparklines
Close Help
The Place to Start


What Player am I pointing at? 
Masterstroke
Easy


What Player am I pointing at?
In this tutorial I'd like to show how to find out what player you are pointing at with your crosshair. Here are only examples of how it is possible to show the player name. For your mod, you maybe have to change it! There are sure other ways to show the name of a player but I think this is a very simple way and you can certainly get it working. Little bug fixed by BigGuy :)

At first you need to know that the code you have to add is red and the original code of the SDK is lime.

We need to change the CBasePlayer::PreThink, so open the player.cpp and goto PreThink. There should be this:

                 void CBasePlayer::PreThink(void)
                 {
                 int buttonsChanged = (m_afButtonLast ^ pev->button); // These buttons have
                 changed this frame

                 // Debounced button codes for pressed/released
                 // UNDONE: Do we need auto-repeat?
                 m_afButtonPressed = buttonsChanged & pev->button; // The changed ones still down
                 are "pressed"
                 m_afButtonReleased = buttonsChanged & (~pev->button); // The ones not down are
                 "released"

                 g_pGameRules->PlayerThink( this );

                 if ( g_fGameOver )
                 return; // intermission or finale
                 ...


Now you have to add the following:

                 void CBasePlayer::PreThink(void)
                 {
                 //Masterstroke {

                 TraceResult tr;

                 Vector anglesAim = pev->v_angle + pev->punchangle;
                 UTIL_MakeVectors( anglesAim );
                 Vector vecSrc = GetGunPosition( ) - gpGlobals->v_up * 2;
                 Vector vecDir = gpGlobals->v_forward;

                 UTIL_TraceLine(vecSrc, vecSrc + vecDir * 8192, dont_ignore_monsters, edict(),
                 &tr);

                 CBaseEntity *point = CBaseEntity::Instance(tr.pHit);
Ok, this code is to "detect" the player you are pointing at with your crosshair. After that there are different ways to give out the name of the player or even the teamname. Here are some examples:

1. This gives out the name of the player above the health:

                 if (FClassnameIs( tr.pHit, "player" ) )
                 {
                 char PrintOutText[201];
                 hudtextparms_t hText;

                 sprintf(PrintOutText, "Player: %s\n", STRING(tr.pHit->v.netname));

                 // if you also want to give out the teamname do this:
                 // sprintf(PrintOutText, "Player: %s Team: %s \n ",
                 STRING(tr.pHit->v.netname),point->TeamID()); 


                  memset(&hText, 0, sizeof(hText));
                  hText.channel = 1;
                  hText.x = 0.01;
                  hText.y = 0.9;

                  hText.effect = 0;
                  hText.r1 = hText.g1 = hText.b1 = 255;
                  hText.a1 = 255;
                  hText.r2 = hText.g2 = hText.b2 = 255;
                  hText.a2 = 255;
                  hText.fadeinTime = 0.2;
                  hText.fadeoutTime = 1;
                  hText.holdTime = 1.5;
                  hText.fxTime = 0.5;

                  UTIL_HudMessage(this, hText, PrintOutText);
                 };


2.This gives out the name on the center of the screen: if (FClassnameIs( tr.pHit, "player" ) ) { ClientPrint(this->pev, HUD_PRINTCENTER, UTIL_VarArgs("Player: %s\n", STRING(tr.pHit->v.netname))); };

3.There are many other ways to give out the name if you just take the code of the second example and change the HUD_PRINTCENTER to HUD_PRINTNOTIFY, HUD_PRINTCONSOLE or HUD_PRINTTALK. Again it is possible to implement the Teamname if you change the ClientPrint command to this: ClientPrint(this->pev, HUD_PRINTCENTER, UTIL_VarArgs("Player: %s Team: %s\n", STRING(tr.pHit->v.netname), point->TeamID()));

Ok, I think that's it. I hope you can understand this tutorial. I think it is quite easy. If you would like to use this code in your mod please give me some credit. Ok, have a lot of fun !!!

Any questions or suggestions should be sent to me: no.names@altavista.net