Hey, everyone. I’m new to Unreal Engine, but, I wanted a challenge - which was why I decided to start my game from scratch using C++. The game I’m making is an indie survival horror video game, where the player must use their flashlight to defend themselves from creatures in the dark. The only problem is, I’m not sure how to start the flashlight script. I want the flashlight to be able to toggle on and off, in addition to battery life drainage. I’m used to Unity and C#, but, I need some assistance with C++. Can someone please help me get a better idea of how to make this script (at least how to start it). If so, it would be much obliged. Please and thanks in advance!
I didn’t actually compile this so there may be an error but this is a basic idea of where to go when creating a flash light that can be turned on and off, with a battery life:
.h
#pragma once
#include "GameFramework/Actor.h"
#include "Flashlight.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FFlashLightToggled, bool, IsOn);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FFlashLightDrained, float, CurrentBatteryLife);
UCLASS( )
class MY_GAME_API AFlashlight : public AActor
{
GENERATED_BODY( )
public:
AFlashlight( );
virtual void BeginPlay( ) override;
virtual void Tick(float DeltaTime) override;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Flashlight")
UStaticMeshComponent *Mesh;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Flashlight")
USpotLightComponent *Light;
// Total battery life
UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Flashlight")
float MaxBatteryLife;
// Current life of battery
UPROPERTY(BlueprintReadOnly, EditAnywhere, Cateogyr = "Flashlight")
float CurrentBatteryLife;
// Amount of time that needs to pass before the battery is drained
UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Flashlight")
float DrainBatteryLifeTickTime;
// Amount of battery life to drain when the battery is drained
UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "Flashlight")
float BatteryDrainPerTick;
// Turn light on
UFUNCTION(BlueprintCallable, Category = "Flashlight")
void TurnOn( );
// Turn light off
UFUNCTION(BlueprintCallable, Category = "Flashlight")
void TurnOff( );
// Toggle light : off if on or on if off
UFUNCTION(BlueprintCallable, Category = "Flashlight")
void Toggle( );
// Is light on
UPROPERTY(BlueprintReadOnly, Category = "Flashlight")
bool bLightIsOn;
// Event when light is turned on or off, for Blueprint
UPROPERTY(BlueprintAssignable, Category = "Flashlight")
FFlashLightToggled LightToggled;
// Event when battery takes any drain, for Blueprint
UPROPERTY(BlueprintAssignable, Category = "Flashlight")
FFlashLightDrained LightDrained;
private:
// Timer called function to drain battery
void BatteryDrain( );
// Helper to see if light can be turned on
bool CanTurnOn( );
};
.cpp
#include "MyGame.h"
#include "Flashlight.h"
AFlashlight::AFlashlight()
{
PrimaryActorTick.bCanEverTick = true;
Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
SetRootComponent(Mesh);
Light = CreateDefaultSubobject<USpotLightComponent>(TEXT("Light"));
Light->SetupAttachment(Mesh);
// Default values
MaxBatteryLife = 1f;
CurrentBatteryLife = MaxBatteryLife;
DrainBatteryLifeTickTime = 3.5f;
BatteryDrainPerTick = 0.05f;
}
void AFlashlight::BeginPlay( )
{
Super::BeginPlay( );
// Timer every "DrainBatteryLifeTickTime" that loops
GetWorldTimerManager( ).SetTimer(this, & AFlashlight::BatteryDrain, DrainBatteryLifeTickTime, true);
}
void AFlashlight::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AFlashlight::TurnOn( )
{
check(Light);
if(CanTurnOn())
{
// If light can turn on, set light to 10 intensity and call blueprint event
bLightIsOn = true;
Light->SetIntensity(10.f);
LightToggled.Broadcast(bLightIsOn);
}
}
void AFlashlight::TurnOff( )
{
check(Light);
if(!CanTurnOn( ))
{
// If light can turn off, set light to 0 intensity and call blueprint event
bLightIsOn = false;
Light->SetIntensity(0f);
LightToggled.Broadcast(bLightIsOn);
}
}
AFlashlight::Toggle( )
{
// If light can be turned on, do that, otherwise turn off
if(CanTurnOn( ))
{
TurnOn( );
}
else
{
TurnOff( );
}
}
void AFlashlight::BatteryDrain( )
{
// If light is on, drain battery
if(bLightIsOn)
{
if(CurrentBatteryLife > 0f)
{
CurrentBatteryLife -= BatteryDrainPerTick;
LightDrained.Broadcast(CurrentBatteryLife);
}
else
{
CurrentBatteryLife = 0f;
TurnOff( );
}
}
}
bool AFlashlight::CanTurnOn( )
{
// life is more than 0 and light isnt on
return (CurrentBatteryLife > 0f && !bLightIsOn);
}