[How To] Unbind an action from the input component

For anyone who’s still having trouble with this. Here is my solution:

It’s important to know that for my scenario, the UInputAction is not even in the Control Mapping and I want to basically replace an action in the mapping with this new action. The background for the scenario is that I have weapon pickups, and each weapon has a different firing mode. Some might be a hold weapon, some might be a down weapon, some might be pressed, some might be pressed and released, some might be down and released, etc. So each weapon has an action associated with it and no way am I putting all of those actions and key mappings in my control mapping. Furthermore I think the player would hate me if I made them need to remap all of those keys for every different action if they wanted to change controls. SO, here is my solution:

void APlayerBase::SetFireInputToAction_Implementation(UInputAction* action) {

	
	TArray<FEnhancedActionKeyMapping> mappings = baseControls->GetMappings();

	

	//delete previous weapons action
	for (FEnhancedActionKeyMapping& map : mappings) {

		if (map.Action == fireAction) {

			

			//make mapping to the new action with each key
			baseControls->MapKey(action, map.Key);

		}

	}
	//now unmap the current fire action
	baseControls->UnmapAllKeysFromAction(fireAction);

	//need to rebuild the control mapping
	UEnhancedInputLibrary::RequestRebuildControlMappingsUsingContext(baseControls);

	

	int32 previousAction = -1;
	for (int i = 0; i < playerEnhancedInput->GetNumActionBindings(); i++) {

		if (playerEnhancedInput->GetActionBinding(i).ActionDelegate.IsBoundToObject(fireAction)) {

			previousAction = playerEnhancedInput->GetActionBinding(i).GetHandle();
			break;
		}

	}

	//remove old binding
	playerEnhancedInput->RemoveActionEventBinding(previousAction);

	//update what the current fire action is
	fireAction = action;

	//make new binding
	playerEnhancedInput->BindAction(fireAction, ETriggerEvent::Triggered, this, &APlayerBase::fireInput);

}
1 Like