Hey everyone hope someone out there can help, I have a flashlight mechanic set up and it works but my problem is that the light doesn’t toggle on/off with a key press (F) I have to hold F for the light to turn on and once I let go the light will turn off.
In my SleepCharacter.h
protected:
/** Toggle Flashlight on*/
void FlashlightOn();
/** Toggle Flashlight off*/
void FlashlightOff();
public:
ASleepCharacter();
/** See if flashlight is currently on */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Character)
bool IsFlashlightOn;
/** Get the current amount of Flashlight battery available */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Character)
float FlashlightPower;
In my SleepCharacter.cpp
// Set up Flashlight variables
IsFlashlightOn = false;
FlashlightPower = 100;
// Create a FlashLightComponent
FlashLightComponent = CreateDefaultSubobject<USpotLightComponent>(TEXT("Flashlight"));
FlashLightComponent->SetupAttachment(FirstPersonCameraComponent);
FlashLightComponent->RelativeLocation = FVector(10, 0, 4.f);
FlashLightComponent->AttenuationRadius = 1000.f;
FlashLightComponent->bAffectsWorld = true;
FlashLightComponent->SetMobility(EComponentMobility::Movable);
void ASleepCharacter::BeginPlay()
{
// Call the base class
Super::BeginPlay();
FlashlightOff(); // Start the game with the flashlight off
}
void ASleepCharacter::Tick(float DeltaTime)
{
if (IsFlashlightOn)
{
if (FlashlightPower > 0)
{
FlashlightPower -= 0.3f;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("ON"));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::White, FString::FromInt(FlashlightPower)); // NEED TO CHANGE TO A TOGGLE PRESS
}
}
else if (!IsFlashlightOn)
{
if (FlashlightPower < 100)
{
FlashlightPower += 0.5;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("OFF"));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Black, FString::FromInt(FlashlightPower));
}
}
if (FlashlightPower <= 0)
{
FlashlightOff();
}
}
void ASleepCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
// set up gameplay key bindings
check(InputComponent);
InputComponent->BindAction("Flashlight", IE_Pressed, this, &ASleepCharacter::FlashlightOn);
InputComponent->BindAction("Flashlight", IE_Released, this, &ASleepCharacter::FlashlightOff);
}
void ASleepCharacter::FlashlightOn()
{
IsFlashlightOn = true;
FlashLightComponent->ToggleVisibility("Flashlight");
}
void ASleepCharacter::FlashlightOff()
{
IsFlashlightOn = false;
FlashLightComponent->ToggleVisibility("Flashlight");
}
That is all the code relating to the Flashlight, I have been stumped for a few days now, when I hold F the flashlight will come on and when I release it will turn off. But like I said before I want it to toggle so when you press F once the flashlight will come on and the FlashlightPower will decrease, if you press F again then the light will turn off and recharge.
I hope the question is clear and the code is easy to read, I hope someone can help with this too
Thanks for taking a look.