UE4 Notes
UE4 Notes
#include “Engine/TriggerVolume.h”
class ATriggerVolume* Name;
UPROPERTY(EditAnywhere)
ATriggerVolume* PressurePlate; // to have a trigger action on actors
UPROPERTY(EditAnywhere)
AActor* ActorThatOpen;
DELTA TIME
UPROPERTY(EditAnywhere)
#include "Engine/World.h"
#include "GameFramework/PlayerController.h"
## To be able to use own specific pawn -- Edit - Project Setting - Default GameMode
https://docs.unrealengine.com/en-US/InteractiveExperiences/Framework/GameMode/index.html
115. MODIFYING DEFAULT PAWN ACTOR & BLUEPRINT
#include “GameFramework/PlayeController.h”
APlayerController::GetPlayerViewPoint
#include “DrawDebugHelpers.h”
Calculating LineTraceEnd
Self approach..
FVector LineTraceDirection = PlayerViewPointRotation.Vector();
//Convert a rotation into a unit vector facing in its direction.
FVector b = LineTraceDirection * Reach;
FVector LineTraceEnd = PlayerViewPointLocation + b; // measured in cm
Guided approach…
FVector LineTraceEnd = PlayerViewPointLocation +
PlayerViewPointRotation.Vector() * Reach;
DrawDebugLine
(
GetWorld(),
PlayerViewPointLocation,
LineTraceEnd,
FColor(0, 255, 0),
false,
0.f,
0U,
5.f
);
Object -> Collision presets - Object Type - PhysicsBody is the one we can pick up
Quiz
A reusable component accesses the transform (position, rotation, scale) of the object it is
attached to = GetOwner()->GetTransform().
120. LineTraceSingleByObjectType()
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
(To access the object’s physics body)
#include “CollisionQueryParams.h”
FCollisionQueryParams
(
FName(TEXT()), // are we using tag? No.. we will use physics channel
Bool bInTraceComplex, // visibility collision using?
Const AActor * InIgnoreActor // which actor to ignore? Yes,self. GetOwner()
)
FHitResult Hit;
// Ray-cast ( Line Trace ) out to a certain distance (reach)
FCollisionQueryParams TraceParam(FName(TEXT("")), false, GetOwner());
// (blank cuz we aren't using, false, ignore us)
GetWorld()->LineTraceSingleByObjectType
(
OUT Hit,
PlayerViewPointLocation,
LineTraceEnd,
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
// To access object from enum, use ::
TraceParam
);
Use it to find attached components. <> for generics. Nullptr to initialize pointers.
#include "PhysicsEngine/PhysicsHandleComponent.h"
private:
float Reach = 150.f;
UPhysicsHandleComponent* PhysicsHandle = nullptr;
In BeginPlay() --
PhysicsHandle = GetOwner()-
>FindComponentByClass<UPhysicsHandleComponent>();
** LOG out if physics is attached to default pawn or not. GetOwner()->GetName()
122. Input Binding
InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
if (InputComponent)
{
UE_LOG(LogTemp, Warning, TEXT("Input component found!"));
InputComponent->BindAction("Grab", IE_Pressed, this,
&UGrabber::Grab);
}
https://docs.unrealengine.com/en-US/API/Runtime/Engine/PhysicsEngine/
UPhysicsHandleComponent/index.html
#include "Components/PrimitiveComponent.h"
UAudioComponent
#include “Components/AudioComponent.h”
Extension** C++ Helper -> press Ctrl + Shift + P -> type create implementation
void AShooterCharacter::SetupPlayerInputComponent(UInputComponent*
PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis(TEXT("MoveForward"), this,
&AShooterCharacter::MoveForward);
PlayerInputComponent->BindAxis(TEXT("LookUp"), this,
&APawn::AddControllerPitchInput);
PlayerInputComponent->BindAxis(TEXT("MoveRight"), this,
&AShooterCharacter::MoveRight);
PlayerInputComponent->BindAxis(TEXT("LookRight"), this,
&APawn::AddControllerYawInput);
PlayerInputComponent->BindAction(TEXT("Jump"),
EInputEvent::IE_Pressed, this, &ACharacter::Jump);
}
Controller Movement
Add Camera component, Add Spring Arm component, make the camera comp be the child of
spring arm, Use Pawn Control Rotation option in spring arm (for rotating 360),
175. SPEED
private:
UPROPERTY(VisableAnywhere)
USceneComponent* Root;
UPROPERTY(VisableAnywhere)
UStaticMeshComponent* Mesh;
OR
USkeletalMeshComponent* Mesh; (depends on the weapon mesh type)
Header --
#include "Components\StaticMeshComponent.h"
#include "Components\SkeletalMeshComponent.h"
.cpp
AGun::AGun()
{
// Set this actor to call Tick() every frame. You can turn this off
to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
Mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Mesh"));
Mesh->SetupAttachment(Root);
}
GetMesh()->HideBoneByName(TEXT("weapon_r"), EPhysBodyOp::PBO_None);
GamePlayStatics.h - SpawnEmitterAttached(...)
UPROPERTY(EditAnywhere)
UParticleSystem* MuzzleFlash;
void AGun::PullTrigger()
{
//UE_LOG(LogTemp, Warning, TEXT("You've been shot!"));
UGameplayStatics::SpawnEmitterAttached(MuzzleFlash, Mesh,
TEXT("MuzzleFlashSocket"));
}
LineTraceTestByChannel()
In DrawDebugHelpers.h
FHitResult Hit;
bool bSuccess = GetWorld()->LineTraceSingleByChannel(Hit, Location,
EndPoint, ECollisionChannel::ECC_GameTraceChannel1);
if (bSuccess)
{
DrawDebugPoint(GetWorld(), Hit.Location, 20, FColor::Green, true);
}
GamePlayStatics.h
SpawnEmitterAtLocation(..)
186. Damage
Actor.h - TakeDamage()
FRadialDamageParams = Generade
FPointDamageEvent = Single target
188. Overriding TakeDamage
virtual float TakeDamage(float DamageAmount, struct FDamageEvent
const& DamageEvent, class AController* EventInstigator, AActor*
DamageCauser) override;
192. AI Aiming
AIController.h
SetFocus(PlayerPawn)
#include “Kismet/GameplayStatics.h”
GameplayStatics.h for UGameplayStatics::GetPlayerPawn()
APawn* PlayerPawn = UGameplayStatics::GetPlayerPawn(GetWorld(), 0);
SetFocus(PlayerPawn);
AIController.h - MoveTo()
AIController.h - LineOfSightTo()
To stop chasing AI
virtual void ClearFocus(EAIFocusPriority::Type InPriority);
if (LineOfSightTo(PlayerPawn))
{
SetFocus(PlayerPawn);
MoveToActor(PlayerPawn, ChaseRadius);
}
RunBehaviorTree()
if (AIBehavior != nullptr)
{
RunBehaviorTree(AIBehavior);
}
BlackboardComponent.h - setValueAs … ()
GetBlackboardComponent()->SetValueAsVector(TEXT("PlayerLocation"),
PlayerPawn->GetActorLocation());
Controller.h - GetPawn()
GetBlackboardComponent()->SetValueAsVector(TEXT("StartLocation"),
GetPawn()->GetActorLocation());
GetBlackboardComponent()->ClearValue(TEXT("PlayerLocation"));
BTTask_BlackboardBase.h
BTTaskNode.h
BehaviorTreeTypes.h
EBTNodeResult::Type
UBTTask_ClearBlackboardValue::ExecuteTask(UBehaviorTreeComponent
&OwnerComp, uint8* NodeMemory)
{
Super::ExecuteTask(OwnerComp, NodeMemory);
OwnerComp.GetBlackboardComponent()-
>ClearValue(GetSelectedBlackboardKey());
return EBTNodeResult::Succeeded;
}
DetachFromControllerPendingDestroy();
GetCapsuleComponent()-
>SetCollisionEnabled(ECollisionEnabled::NoCollision);
AFirstShooterGameModeBase* GameMode = GetWorld()-
>GetAuthGameMode<AFirstShooterGameModeBase>();
if(GameMode != nullptr)
{
GameMode->PawnKilled(this);
}
Blueprint Class
Structures (Struct)
Transform, Hit Result,