https://www.reddit.com/r/unrealengine/comments/19f3ul6/enhanced_input_addmappingcontext/
You have created a system to equip weapons.Each time a weapon is equipped, it adds a MappingContext that the Weapon Actor has.We have a problem with removing the WeaponMappingContext that was previously mapped.
void ABaseRangedWeapon::SetupWeaponInputMappingContext()
{
if(PlayerCharacter) { if(const APlayerController* PC = Cast<APlayerController>(PlayerCharacter->GetController()))
{ if(UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer()))
{ if(Subsystem->HasMappingContext(WeaponMappingContext)) { ClearWeaponMappingContext(); }
Subsystem->AddMappingContext(WeaponMappingContext,1);
if(UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PC->InputComponent))
{
EnhancedInputComponent->BindAction(FireInputAction, ETriggerEvent::Triggered, this, &ThisClass::FirePressed);
EnhancedInputComponent->BindAction(AimInputAction, ETriggerEvent::Triggered, this, &ThisClass::AimPressed);
EnhancedInputComponent->BindAction(ReloadInputAction, ETriggerEvent::Started, this, &ThisClass::ReloadPressed);
InitFiringMode();
}
Subsystem->RequestRebuildControlMappings(FModifyContextOptions(),EInputMappingRebuildType::RebuildWithFlush);
}
}
}
}
void ABaseWeapon::ClearWeaponMappingContext() const
{
if(PlayerCharacter)
{ if(const APlayerController* PC = Cast<APlayerController>(PlayerCharacter->GetController()))
{ if(UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer()))
{ Subsystem->RemoveMappingContext(WeaponMappingContext); } } }
}
The problem is that when I reload, I get a duplicate event to see if there are any existing registered mappings left. How can I fix this?I do the removal with Subsystem->RemoveMappingContext(WeaponMappingContext); but it’s weird.