Advertisement
Guest User

ItemGrabber.cpp

a guest
Jun 3rd, 2021
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. #include "GameFramework/PlayerController.h"
  2. #include "DrawDebugHelpers.h"
  3. #include "ItemGrabber.h"
  4. // To give myself a heads up that something is an out parameter.
  5. #define OUT
  6.  
  7.  
  8. // ----------------------------------------------------------------------
  9. /// Sets default values for this component's properties.
  10. UItemGrabber::UItemGrabber()
  11. {
  12.     // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
  13.     // off to improve performance if you don't need them.
  14.     PrimaryComponentTick.bCanEverTick = true;
  15.  
  16.     // ...
  17. }
  18.  
  19.  
  20. // ----------------------------------------------------------------------
  21. /// Called when the game starts.
  22. void UItemGrabber::BeginPlay()
  23. {
  24.     Super::BeginPlay();
  25.     NotifyLoading();
  26.     Player = GetWorld()->GetFirstPlayerController();
  27.  
  28.     // Get the PhysicsHandle component we added to the DefaultPawnBP.
  29.     // UItemGrabber is also attached to DefaultPawnBP, so GetOwner will work for this too.
  30.     // Since FindComponentByClass is a function template, we construct by <type>.
  31.     // This will return the first UPhysicsHandleComponent found in the Owner (DefaultPawnBP).
  32.     PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
  33.  
  34.     if (!PhysicsHandle) {
  35.         UE_LOG(LogTemp, Error,
  36.             TEXT("%s has no Physics Handle Component (needed by ItemGrabber)!"),
  37.             *GetOwner()->GetName()
  38.         );
  39.     }
  40.     // ...
  41. }
  42.  
  43.  
  44. // ----------------------------------------------------------------------
  45. /// Called every frame.
  46. void UItemGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
  47. {
  48.     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
  49.  
  50.     // Get player's viewpoint and set it to our PlayerView data structure.
  51.     Player->GetPlayerViewPoint(
  52.         OUT PlayerView.Location,
  53.         OUT PlayerView.Rotation);
  54.  
  55.     GetGrabbableObject();
  56.     DebugLineStuff();
  57.     // If key is pressed, grab object and move to set short distance in front of player.
  58.     // If key is released, release object.
  59.     // ...
  60. }
  61.  
  62.  
  63. // ----------------------------------------------------------------------
  64. /// Do a line trace and see what object we can grab. Update GrabbableHit.
  65. void UItemGrabber::GetGrabbableObject()
  66. {
  67.     // The max length point of the ray cast we want.
  68.     // Player location + rotation * the length of grab.
  69.     LineTraceEnd = PlayerView.Location + PlayerView.Rotation.Vector() * Reach;
  70.  
  71.     // Define what should be considered a collision for what can be grabbed.
  72.     const FCollisionQueryParams TraceParameters(
  73.         FName(TEXT("")),    // The tag name.
  74.         false,              // Use complex collisions?
  75.         GetOwner()          // Which objects to ignore (make sure the ray hitting ourselves doesn't count).
  76.     );
  77.  
  78.     // LineTrace (raycast) from view to certain distance, see what the cast hits.
  79.     GetWorld()->LineTraceSingleByObjectType(
  80.         OUT GrabbableHit,
  81.         PlayerView.Location,
  82.         LineTraceEnd,
  83.         FCollisionObjectQueryParams(ECC_PhysicsBody),
  84.         TraceParameters
  85.     );
  86. }
  87.  
  88. // --------------------------------------------------------------------
  89. // Debug helper functions.
  90. // --------------------------------------------------------------------
  91.  
  92. /// Draw debug line for grabby hands, log grabbable object hit.
  93. void UItemGrabber::DebugLineStuff()
  94. {
  95.     FColor Color;
  96.  
  97.     DrawDebugLine(
  98.         GetWorld(),              // InWorld.
  99.         PlayerView.Location,     // LineStart.
  100.         LineTraceEnd,            // LineEnd.
  101.         Color.FromHex("00FF15"), // Color.
  102.         false,                   // PersistentLines.
  103.         1,                       // LifeTime.
  104.         0,                       // DepthPriority
  105.         2                        // Thickness.
  106.     );
  107.  
  108.     // Checking if GrabbableHit has an actor yet,
  109.     // should stop us from referencing a null pointer and crashing.
  110.     if (GrabbableHit.GetActor()) {
  111.         UE_LOG(LogTemp, Warning,
  112.             TEXT("Grabbable object: %s"),
  113.             *GrabbableHit.GetActor()->GetName()
  114.         );
  115.     }
  116. }
  117.  
  118. /// Notify this component was loaded successfully.
  119. void UItemGrabber::NotifyLoading()
  120. {
  121.     UE_LOG(LogTemp, Warning,
  122.     TEXT("UItemGrabber loaded successfully on %s."),
  123.     *GetOwner()->GetName()
  124. );
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement