How do I disable one type of an Input?

Godot Version

4.2.2

Question

How do I disable one type of an Input?

I am trying to build a place where the player can’t use a specific ability. If the player enters the area, the input connected to the “attack” ability should be disabled. I tried to look for a tutorial, but I couldn’t find anything.

Code

extends Area2D
@onready var player_dungeon = $player_dungeon
@onready var playerinfos = get_node("../player_dungeon")


func _on_body_entered(body):
	if body == player_dungeon:
		if Input.is_action_just_pressed("attack"):
			#disable attack
			
func _on_body_exited(body):
	pass

Best to make a variable on the player if their attack is available or not, then check when pressed if they can use that action.

1 Like

When the player enters the area, set something like “special_attack_disabled” in the player’s class to true. When he leaves, set it to false. And check for the value before performing the attack.

2 Likes

If you can refactor to use _unhandled_input and events, instead of pulling with the Input singleton. You could add a hidden UI element to capture specific inputs and set them as handled. So they will not propagate down into the player and you wouldn’t need to add extra logic.

Although you may forget it’s there and wonder why your character isn’t working. I guess a log is in order

You could put a bool on the player (can_attack) that is checked alongside the input check. When entering the area, just flip the can_attack to false.

If Input.is_action_just_pressed(“attack”) && player.can_attack == true:

1 Like

A simple bool will work,

class_name TestState

var PlayerInput: bool = false

func Enter() -> void:
	# Perform Enter Animations and Sounds	
	PlayerInput = true

func Exit() -> void:
	PlayerInput = false
	# Perform Exit Animations and Sounds

func Update(_delta: float) -> void:
	if Input.is_action_just_pressed("Accept_Button"):
		if PlayerInput == true:
			Accept_Input()

	if Input.is_action_just_pressed("Cancel_Button"):
		if PlayerInput == true:
			Cancel_Input()

func Accept_Input() -> void:
	print("You pressed the A button.")

func Cancel_Input() -> void:
	print("You pressed the B button.")
1 Like

Just write it like that. This way that button does nothing. In that area. Test it.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.