how do i reverse a button input with the same button?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Witcher64

I am making a first person shooter where your primary weapon is throwing knives.
I am having trouble with Input.is_action_just_pressed().
I added a feature where right click flips the knife, causing it to be thrown by the blade.
Problem is I want the right click to reverse the process (flipping the knife back to the hilt) in the event of another right click and I have no idea how!
Where it stands now, I right click once, and the knife is stuck in the held by the blade position.

:bust_in_silhouette: Reply From: Help me please

For this you need to declare a bool variable which will be true for knife and false for blade and hence it will go like this

var knife = true
func _process(delta):
	if  Input.is_action_just_pressed("attack"):
		if knife == true:
			#knife attack goes here
		elif knife == false:
			#blade attack goes here
	if input.is_action_just_pressed("right_click")
		knife = !knife

in the last we equated knife with its reverse. And hence we need not to write multiple if statements.

Thanks a bunch!

Witcher64 | 2021-06-24 11:47