Skip to content

Instantly share code, notes, and snippets.

@Chaosed0
Last active May 16, 2020 04:11
Show Gist options
  • Save Chaosed0/4a378922ede069bb20d6 to your computer and use it in GitHub Desktop.
Save Chaosed0/4a378922ede069bb20d6 to your computer and use it in GitHub Desktop.
Example AActor-derived class for Unreal Engine 4
/* An example UE4 class derived from AActor. It plays a
* sound when an actor enters the bounding box, and lets
* the player turn a PointLight on and off using the "Use"
* key. It originally existed as a blueprint. */
/* WallSconceCpp.h */
#pragma once
#include "GameFramework/Actor.h"
#include "WallSconceCpp.generated.h"
UCLASS()
class WTF_API AWallSconceCpp : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AWallSconceCpp(const FObjectInitializer &ObjectInitializer);
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
// Called when another actor overlaps with this one
UFUNCTION()
void OnBeginOverlap(AActor *otherActor);
// Called when another actor stops overlapping with this one
UFUNCTION()
void OnEndOverlap(AActor *otherActor);
UFUNCTION()
void Use();
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly)
class UStaticMeshComponent *mesh;
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly)
class UPointLightComponent *light;
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly)
class UAudioComponent *audio;
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly)
class UBoxComponent *useArea;
};
/* WallSconceCpp.cpp */
#include "wtf.h"
#include "WallSconceCpp.h"
// Sets default values
AWallSconceCpp::AWallSconceCpp(const FObjectInitializer &ObjectInitializer)
{
// 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 = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("LampMesh"));
light = ObjectInitializer.CreateDefaultSubobject<UPointLightComponent>(this, TEXT("PointLight"));
audio = ObjectInitializer.CreateDefaultSubobject<UAudioComponent>(this, TEXT("Audio"));
useArea = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, TEXT("Box"));
/* Initialize the component hierarchy */
SetRootComponent(useArea);
mesh->AttachTo(useArea);
light->AttachTo(useArea);
audio->AttachTo(useArea);
/* Make the box collider generate events, so we know the player has entered the area */
useArea->bGenerateOverlapEvents = true;
/* Make the box collider default a little larger */
useArea->InitBoxExtent(FVector(100, 100, 100));
/* Make the light float a little above the static mesh */
light->SetRelativeLocation(FVector(0, 0, 50));
/* Don't play the audio by default */
audio->SetActive(false);
/* Don't light up by default */
light->SetVisibility(false);
/* Bind events */
OnActorBeginOverlap.AddDynamic(this, &AWallSconceCpp::OnBeginOverlap);
OnActorEndOverlap.AddDynamic(this, &AWallSconceCpp::OnEndOverlap);
}
// Called when the game starts or when spawned
void AWallSconceCpp::BeginPlay()
{
Super::BeginPlay();
/* I'm not totally sure whether this is proper or not, but it seems like
* the PlayerController was invalid in the constructor above, so setup
* the toggle light event here */
InputComponent = GetWorld()->GetFirstPlayerController()->InputComponent;
InputComponent->BindAction("Use", IE_Pressed, this, &AWallSconceCpp::Use);
}
// Called every frame
void AWallSconceCpp::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
void AWallSconceCpp::OnBeginOverlap(AActor *otherActor)
{
/* Toggle audio and allow the player to toggle the light
* when they enter the box collider */
audio->SetActive(true);
EnableInput(GetWorld()->GetFirstPlayerController());
}
void AWallSconceCpp::OnEndOverlap(AActor *otherActor)
{
/* Toggle audio and don't allow the player to toggle the
* light when they exit the box collider */
audio->SetActive(false);
DisableInput(GetWorld()->GetFirstPlayerController());
}
void AWallSconceCpp::Use()
{
/* Toggle the light when the player presses "Use"*/
light->ToggleVisibility();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment