Announcement

Collapse
No announcement yet.

Get the name of pawn by trace function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Get the name of pawn by trace function

    Hi.
    I want get the name of pawn that I'm aiming. What function I should use for this?

    Thanks.

    #2
    Try 'pawn.GetHumanReadableName' function (if it still exists).

    Comment


      #3
      Assuming you also need to find out which pawn you're aiming at before getting its name, this should hopefully work. (untested)

      Code:
      function string GetAimTargetPlayerName()
      {
      	local Pawn P, PawnOwner;
      	local actor HitActor;
      	local vector HitLocation, HitNormal, StartTrace, EndTrace, PawnToPawn;
      
      	PawnOwner = PlayerOwner.Pawn;
      	if (PawnOwner == None)
      		return "";
      
      	StartTrace = PawnOwner.Location;
      
      	foreach WorldInfo.AllPawns(class'Pawn', P)
      	{
      		if (P != PawnOwner)
      		{
      			PawnToPawn = P.Location - StartTrace;			
      
      			// fast check to see if aim is within 60 degrees
      			if (0.5 < (Normal(PawnToPawn) dot vector(PlayerOwner.Rotation)))
      			{
      				// perform actual trace to make sure pawn is exactly on crosshair
      				EndTrace = StartTrace + (VSize(PawnToPawn) * 1.1) * vector(PlayerOwner.Rotation);
      				HitActor = MyPawnOwner.Trace(HitLocation, HitNormal, EndTrace, StartTrace, true);
      
      				if ( Pawn(HitActor) == None )
      				{
      					// might still have hit pawn's weapon or such
      					HitActor = (HitActor == None) ? None : Pawn(HitActor.Base);
      				}
      
      				if (Pawn(HitActor) != None && Pawn(HitActor).PlayerReplicationInfo != None)
      				{
      					return Pawn(HitActor).PlayerReplicationInfo.PlayerName;
      				}
      			}
      		}
      	}
      	return "";
      }
      Since you probably want to do this every time you draw the HUD, I tried to have it perform traces only when a pawn is considered to be pretty close to your crosshair (within 60 degrees). This way you won't spam tons of traces every tick. This function would go into your HUD class.

      Comment


        #4
        Why even bother with the 'foreach' check? Back in the original UT all the HUD Player ID code ('TraceIdentify' and 'DrawIdentityInfo' functions) did was a single trace check 1000 UUs in front of the player along its view rotation; if a pawn was hit then its name was displayed, if not, nothing was shown. (see the ChallengeHUD class from the BotPack package in the UTScript source).

        Comment


          #5
          I just felt it's completely unnecessary to perform a trace when there's no pawn around, and also I was a bit bored. A trace every frame is probably fine, though.

          Comment


            #6
            A much simpler aproach

            On your hud, on the drawliving function, get the pawn from the playerowner, then get the weapon, then call a function and pass it the canvas. That way on every frame you can have your weapon trace and get the object, if its a pawn then look for its name on the playerreplication info.

            Comment


              #7
              Originally posted by evr View Post
              Assuming you also need to find out which pawn you're aiming at before getting its name, this should hopefully work. (untested)

              Code:
              function string GetAimTargetPlayerName()
              {
              	local Pawn P, PawnOwner;
              	local actor HitActor;
              	local vector HitLocation, HitNormal, StartTrace, EndTrace, PawnToPawn;
              
              	PawnOwner = PlayerOwner.Pawn;
              	if (PawnOwner == None)
              		return "";
              
              	StartTrace = PawnOwner.Location;
              
              	foreach WorldInfo.AllPawns(class'Pawn', P)
              	{
              		if (P != PawnOwner)
              		{
              			PawnToPawn = P.Location - StartTrace;			
              
              			// fast check to see if aim is within 60 degrees
              			if (0.5 < (Normal(PawnToPawn) dot vector(PlayerOwner.Rotation)))
              			{
              				// perform actual trace to make sure pawn is exactly on crosshair
              				EndTrace = StartTrace + (VSize(PawnToPawn) * 1.1) * vector(PlayerOwner.Rotation);
              				HitActor = MyPawnOwner.Trace(HitLocation, HitNormal, EndTrace, StartTrace, true);
              
              				if ( Pawn(HitActor) == None )
              				{
              					// might still have hit pawn's weapon or such
              					HitActor = (HitActor == None) ? None : Pawn(HitActor.Base);
              				}
              
              				if (Pawn(HitActor) != None && Pawn(HitActor).PlayerReplicationInfo != None)
              				{
              					return Pawn(HitActor).PlayerReplicationInfo.PlayerName;
              				}
              			}
              		}
              	}
              	return "";
              }
              Since you probably want to do this every time you draw the HUD, I tried to have it perform traces only when a pawn is considered to be pretty close to your crosshair (within 60 degrees). This way you won't spam tons of traces every tick. This function would go into your HUD class.
              evr,
              This works fine, thank you very, very much.

              Thanks.

              Comment


                #8
                take into account iterating functions are costly and should be used only when necesary. Even if this is just a learning project, this can become a real bad habbit, best to start learning good habbits

                Comment


                  #9
                  For reference here is the code from UT (99)'s ChallengeHUD.uc for performing the identity trace (called once per tick from the HUD's PostRender). Obviously to use this in UDK you'd need to update a few of the parameters a bit or just update it for your purposes:
                  Code:
                  class ChallengeHUD extends HUD
                  	config;
                  var float IdentifyFadeTime;
                  var PlayerReplicationInfo IdentifyTarget;
                  var Pawn PawnOwner;	// pawn currently managing this HUD (may be the viewtarget of the owner rather than the owner) 
                  ....
                  simulated function bool TraceIdentify(canvas Canvas) // you could modify this here to return the PlayerReplicationInfo instead...
                  {
                  	local actor Other;
                  	local vector HitLocation, HitNormal, StartTrace, EndTrace;
                  
                  	StartTrace = PawnOwner.Location;
                  	StartTrace.Z += PawnOwner.BaseEyeHeight;
                  	EndTrace = StartTrace + vector(PawnOwner.ViewRotation) * 1000.0;
                  	Other = Trace(HitLocation, HitNormal, EndTrace, StartTrace, true);
                  
                  	if ( Pawn(Other) != None )
                  	{
                  		if ( Pawn(Other).bIsPlayer && !Other.bHidden )
                  		{
                  			IdentifyTarget = Pawn(Other).PlayerReplicationInfo;
                  			IdentifyFadeTime = 3.0;
                  		}
                  	}
                  	else if ( (Other != None) && SpecialIdentify(Canvas, Other) )
                  		return false;
                  
                  	if ( (IdentifyFadeTime == 0.0) || (IdentifyTarget == None) || IdentifyTarget.bFeigningDeath )
                  		return false;
                  
                  	return true;
                  }
                  If you wanted a simplified version it could be something like this:
                  Code:
                  simulated function string TracePawnIdentify(canvas Canvas)
                  {
                  	local actor Other;
                  	local vector HitLocation, HitNormal, StartTrace, EndTrace;
                  
                  	StartTrace = PawnOwner.GetPawnViewLocation();
                  	EndTrace = StartTrace + vector(PawnOwner.GetViewRotation()) * 1000.0;
                  	Other = Trace(HitLocation, HitNormal, EndTrace, StartTrace, true,,,TRACEFLAG_Blocking);
                  
                  	if ( Pawn(Other) != None )
                  	{
                  		return Other.GetHumanReadableName();
                  	}
                          return "";
                  }

                  Comment


                    #10
                    evr,
                    How do I to your code works in a multiplayer game?

                    Thanks.

                    Comment


                      #11
                      Originally posted by meowcat View Post
                      For reference here is the code from UT (99)'s ChallengeHUD.uc for performing the identity trace (called once per tick from the HUD's PostRender). Obviously to use this in UDK you'd need to update a few of the parameters a bit or just update it for your purposes:
                      Code:
                      class ChallengeHUD extends HUD
                      	config;
                      var float IdentifyFadeTime;
                      var PlayerReplicationInfo IdentifyTarget;
                      var Pawn PawnOwner;	// pawn currently managing this HUD (may be the viewtarget of the owner rather than the owner) 
                      ....
                      simulated function bool TraceIdentify(canvas Canvas) // you could modify this here to return the PlayerReplicationInfo instead...
                      {
                      	local actor Other;
                      	local vector HitLocation, HitNormal, StartTrace, EndTrace;
                      
                      	StartTrace = PawnOwner.Location;
                      	StartTrace.Z += PawnOwner.BaseEyeHeight;
                      	EndTrace = StartTrace + vector(PawnOwner.ViewRotation) * 1000.0;
                      	Other = Trace(HitLocation, HitNormal, EndTrace, StartTrace, true);
                      
                      	if ( Pawn(Other) != None )
                      	{
                      		if ( Pawn(Other).bIsPlayer && !Other.bHidden )
                      		{
                      			IdentifyTarget = Pawn(Other).PlayerReplicationInfo;
                      			IdentifyFadeTime = 3.0;
                      		}
                      	}
                      	else if ( (Other != None) && SpecialIdentify(Canvas, Other) )
                      		return false;
                      
                      	if ( (IdentifyFadeTime == 0.0) || (IdentifyTarget == None) || IdentifyTarget.bFeigningDeath )
                      		return false;
                      
                      	return true;
                      }
                      If you wanted a simplified version it could be something like this:
                      Code:
                      simulated function string TracePawnIdentify(canvas Canvas)
                      {
                      	local actor Other;
                      	local vector HitLocation, HitNormal, StartTrace, EndTrace;
                      
                      	StartTrace = PawnOwner.GetPawnViewLocation();
                      	EndTrace = StartTrace + vector(PawnOwner.GetViewRotation()) * 1000.0;
                      	Other = Trace(HitLocation, HitNormal, EndTrace, StartTrace, true,,,TRACEFLAG_Blocking);
                      
                      	if ( Pawn(Other) != None )
                      	{
                      		return Other.GetHumanReadableName();
                      	}
                              return "";
                      }
                      Your codes no works for me.

                      Comment


                        #12
                        Originally posted by Humberto Milanez View Post
                        evr,
                        How do I to your code works in a multiplayer game?

                        Thanks.
                        It was already intended to work in multiplayer. It doesn't?

                        Comment


                          #13
                          @Humberto: The code needs tweaking to work (it is not tested,but should not require much adjustment). The pawnowner variable could be changed out for some other reference to the pawn belonging to the controller who owns the hud (or perhaps the Controller's viewtarget...)

                          Comment


                            #14
                            Originally posted by evr View Post
                            It was already intended to work in multiplayer. It doesn't?
                            Oh, sorry! Works. I had modifyied it, but works.

                            Comment


                              #15
                              Originally posted by meowcat View Post
                              @Humberto: The code needs tweaking to work (it is not tested,but should not require much adjustment). The pawnowner variable could be changed out for some other reference to the pawn belonging to the controller who owns the hud (or perhaps the Controller's viewtarget...)
                              Ok, Meowcat, I will test it when I get in home.
                              Thank you.

                              Comment

                              Working...
                              X