Download

Inputs don't work for me

Hello everyone!

I’ve created a new action mapping in my project called TurnOnFlashLight and assigned the F key for it.

In my character class I have this in my class header:



protected:
//Turn on/off flashlight
	UFUNCTION()
		void TurnOnFlashlight();
	UFUNCTION()
		void TurnOffFlashlight();

// Flashlight
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = FlashLight)
		TSubobjectPtr<USpotLightComponent> FlashlightComponent;


And this in my .cpp:



//Create Flashlight in constructor
	FlashlightComponent = PCIP.CreateDefaultSubobject<USpotLightComponent>(this, TEXT("Flashlight"));
	FlashlightComponent->AttachParent = FirstPersonCameraComponent;
	FlashlightComponent->Intensity = 0.0f; //Turned off by default
	FlashlightComponent->InnerConeAngle = 16.0f;
	FlashlightComponent->OuterConeAngle = 25.0f;
	FlashlightComponent->RelativeRotation = FRotator(0, 0, 0);

// INPUT
void ATTSCharacter::TurnOnFlashlight()
{
	FlashlightComponent->Intensity = 5000.0f;
}

void ATTSCharacter::TurnOffFlashlight()
{
	FlashlightComponent->Intensity = 0.0f;
}

//This is in my SetupPlayerInputComponent function
       InputComponent->BindAction("TurnOnFlashLight", IE_Pressed, this, &ATTSCharacter::TurnOnFlashlight);
       InputComponent->BindAction("TurnOnFlashLight", IE_Released, this, &ATTSCharacter::TurnOffFlashlight);


The code compiles, no problems, but when I get to the game the flashlight is set to 5000.0 and if I try to hit the “F” key nothing really happens. Any idea how to fix this? I’m surely doing something wrong.

Just to make sure.


virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent)

Is this public: in your class?

No, it’s protected. Should it be public?
Also I fixed the blueprint and now when I start the game the intensity is 0, but still doesn’t want to turn on when I press F

Yes it should ACharacter::SetupPlayerInputComponent
API Refrence ACharacter::
Make it public and i think that will solve your problem.

Nope it’s nothing has changed. I forgot to mention that the other action mappings actually work. Only the TurnOnFlashLight doesnt…

136cb395f2fa0785361dc02e9346d8d8ffb4d3f5.png

Ok if the other ones work things get more tricky.
So if you are sure the method gets called i think you have to look into the flashlight component.
And check if the light is correctly setup, sorry i can`t be of more help atm.

You set your intensity to 0 as soon as you release F button, if you wanted to make it switch button, you should check state of the flashlight and proceed accordingly.
If you wanted it to be “hold-to-keep-on”, then make sure that the light is set up properly, simply unpossess pawn while playing and fly around the character to see where it is pointing and everything else.

Hope this helps, regards.

I want it to be press “F” to turn on the flashlight, and press “F” to turn it off. I tried adding just IE_Pressed, and wrote a code like this:



void ATTSCharacter::TurnOnFlashlight() {
if(isTurnedOn == false)
{
Flashlight->Intensity = 5000.0f;
isTurnedOn = true;
}
else if (isTurnedOn == true)
{
Flashlight->Intensity = 0.0f;
isTurnedOn = false;
}
}


It still compiled correctly, but it didn’t want to change it’s intensity…

Edit: Here’s a photo when I debugged the code to see what’s wrong…:
Debuging.png

Can’t tell you anything as i don’t see how your flash light is being set up. Once again, make sure you actually could see light from the component and it’s not looking backwards.
And, it would be better to toggle visibility instead of changing intensity. Just like USpotlightComponent::ToggleVisibility().

There is no problem with the spotlight setup as if I set the intensity to 5000.0f in the constructor, it is working. But I cannot manage to make it turn on/off. Let me try with toggle visibility

Works… I’m really stupid… I just had to reset the variables to default in the blueprint, and compile it… Works just fine! Thanks for the help, this Toggle visiblity is a lot easier and better :slight_smile: