Mapping Blueprint To C++

Finding out the function name

To go from a Blueprint function to C++ function.

  1. Hover over target and we see Scene Component.

  2. In VS Ctrl+, and type in SceneComponent.h

  3. search for GetWorldLocation and we’ll see the below

/** Return location of the component, in world space */
UFUNCTION(BlueprintCallable, meta=(DisplayName = "GetWorldLocation", ScriptName = "GetWorldLocation"), Category="Utilities|Transformation")
FVector K2_GetComponentLocation() const;
  1. F12 on K2_GetComponentLocation and we’ll see the below. Now we’ve mapped BP GetWorldLocation to C++ GetComponentLocation

FVector USceneComponent::K2_GetComponentLocation() const
{
    return GetComponentLocation();
}

Similarly we can do the same with KismetMathLibrary.h and look for GetForwardVector and we’d find:

/** Rotate the world forward vector by the given rotation */
UFUNCTION(BlueprintPure, meta=(ScriptMethod = "GetForwardVector", Keywords="rotation rotate"), Category="Math|Vector")
static FVector GetForwardVector(FRotator InRot);

If we F12 on GetForwardVector we’ll see this.

FVector UKismetMathLibrary::GetForwardVector(FRotator InRot)
{
    return InRot.Vector();
}

Now we realised that we can simplify the code:

FVector UGrabber::GetMaxGrabLocation() const
{
    return GetComponentLocation() + UKismetMathLibrary::GetForwardVector(GetComponentRotation()) * MaxGrabDistance;
}

In to:

FVector UGrabber::GetMaxGrabLocation() const
{
    return GetComponentLocation() + GetComponentRotation().Vector() * MaxGrabDistance;
}


FVector UGrabber::GetMaxGrabLocation() const
{
    return GetComponentLocation() + UKismetMathLibrary::GetForwardVector(GetComponentRotation()) * MaxGrabDistance;
}

Common Includes

Class

#include

UWorld

Engine/World.h

AActor

GameFramework/Actor.h

UActorComponent

Components/ActorComponents.h

UGameplayStatics

Kismet/GameplayStatics.h

UKismetSystemLibrary

Kismet/KismetSystemLibrary.h

FMath

Math/UnrealMathUtility.h

Converting GetPhysicsComponent

  1. Add #include to Grabber.h. Note: it has to go before Grabber.generated.h as that has to be the last #include.

#include "PhysicsEngine/PhysicsHandleComponent.h"
  1. Add function declaration:

UFUNCTION(BlueprintCallable, BlueprintPure)
FVector GetPhysicsComponent() const;
  1. Add #include to Grabber.cpp

#include "GameFramework/Actor.h"
  1. Add function definition:

UPhysicsHandleComponent* UGrabber::GetPhysicsComponent() const
{
    return GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
}
  1. As usual, delete GetPhysicsComponent BP function and recompile