Ignore race ability input while a GUI has focus

UnityEngine.Input reads the device directly, so abilities bound to keys
or mouse buttons kept firing with the map, the inventory, a chest, the
chat or a menu open - clicking an item in a container also swung the
transformed attack.

Route that input through Player.TakeInput, which is what Valheim runs
its own controls through and already reports false for those states
(InventoryGui covers the backpack and chests alike). Aswid's transform
keys were the only place already doing this; the shared helper replaces
that one-off and covers Skadi's cold spell, form keys, flight toggle,
form attacks and flight boost, Fenrir's form keys and the attacks of all
four form behaviors, Trol's rock throw and true form, and Aswid's devour
toggle.

Cultist fire is a held button, so it is gated per read rather than
around the block: opening a GUI mid-stream now falls through to
StopFire() instead of leaving the flames running.
This commit is contained in:
kanym 2026-08-01 13:25:02 +02:00
commit bca47beda6
9 changed files with 67 additions and 20 deletions

View file

@ -506,7 +506,7 @@ namespace RacesOfValheim
ZNetView nview = __instance.GetComponent<ZNetView>(); ZNetView nview = __instance.GetComponent<ZNetView>();
if (nview != null && nview.IsValid() && nview.GetZDO().GetString("RacesOfValheim_Race", "Human") == "Aswid") if (nview != null && nview.IsValid() && nview.GetZDO().GetString("RacesOfValheim_Race", "Human") == "Aswid")
{ {
if (__instance == Player.m_localPlayer && Input.GetKeyDown(KeyCode.I)) if (__instance == Player.m_localPlayer && RaceInput.Allowed && Input.GetKeyDown(KeyCode.I))
{ {
AswidRace.DevourEnabled = !AswidRace.DevourEnabled; AswidRace.DevourEnabled = !AswidRace.DevourEnabled;
string msg = AswidRace.DevourEnabled ? "Devour Ability: ENABLED" : "Devour Ability: DISABLED"; string msg = AswidRace.DevourEnabled ? "Devour Ability: ENABLED" : "Devour Ability: DISABLED";

View file

@ -515,8 +515,7 @@ namespace RacesOfValheim
int state = BossProgressionManager.GetCurrentProgressionState(); int state = BossProgressionManager.GetCurrentProgressionState();
bool takeInput = HarmonyLib.Traverse.Create(Player.m_localPlayer).Method("TakeInput").GetValue<bool>(); if (RaceInput.Allowed)
if (takeInput)
{ {
if (Input.GetKeyDown(KeyCode.G)) { TryTransformWithRules(AswidProgression.GetGKeyRules(state)); return; } if (Input.GetKeyDown(KeyCode.G)) { TryTransformWithRules(AswidProgression.GetGKeyRules(state)); return; }
if (Input.GetKeyDown(KeyCode.H)) { TryToggleMistForm(); return; } if (Input.GetKeyDown(KeyCode.H)) { TryToggleMistForm(); return; }

View file

@ -56,7 +56,7 @@ namespace RacesOfValheim
fenrirAnim.SetFloat("sideway_speed", playerAnim.GetFloat("sideway_speed")); fenrirAnim.SetFloat("sideway_speed", playerAnim.GetFloat("sideway_speed"));
} }
} }
if (isGrounded) if (isGrounded && RaceInput.Allowed)
{ {
if (Input.GetKeyDown(KeyCode.O)) if (Input.GetKeyDown(KeyCode.O))
{ {
@ -311,8 +311,12 @@ namespace RacesOfValheim
if (isGrounded) if (isGrounded)
{ {
// Gated per-read rather than around the block, so opening a GUI mid-stream
// still falls through to StopFire() instead of leaving the flames running.
bool canAct = RaceInput.Allowed;
// Fire attack on Right Click // Fire attack on Right Click
if (Input.GetMouseButton(1)) if (canAct && Input.GetMouseButton(1))
{ {
if (Player.m_localPlayer.GetEitr() > 0) if (Player.m_localPlayer.GetEitr() > 0)
{ {
@ -335,18 +339,18 @@ namespace RacesOfValheim
StopFire(); StopFire();
} }
if (Input.GetKeyDown(KeyCode.O)) if (canAct && Input.GetKeyDown(KeyCode.O))
{ {
StartCoroutine(HowlRoutine()); StartCoroutine(HowlRoutine());
} }
// Attacks on Left Click // Attacks on Left Click
if (Input.GetMouseButtonDown(0) && !isShootingFire) if (canAct && Input.GetMouseButtonDown(0) && !isShootingFire)
{ {
StartCoroutine(ClawRoutine()); // Single claw StartCoroutine(ClawRoutine()); // Single claw
} }
// Double claw on Middle Click // Double claw on Middle Click
else if (Input.GetMouseButtonDown(2) && !isShootingFire) else if (canAct && Input.GetMouseButtonDown(2) && !isShootingFire)
{ {
StartCoroutine(DoubleClawRoutine()); StartCoroutine(DoubleClawRoutine());
} }
@ -671,7 +675,7 @@ namespace RacesOfValheim
} }
} }
if (isGrounded) if (isGrounded && RaceInput.Allowed)
{ {
if (Input.GetMouseButtonDown(0)) // Left Click if (Input.GetMouseButtonDown(0)) // Left Click
{ {
@ -844,7 +848,7 @@ namespace RacesOfValheim
} }
} }
if (isGrounded) if (isGrounded && RaceInput.Allowed)
{ {
if (Input.GetMouseButtonDown(0)) // Left Click if (Input.GetMouseButtonDown(0)) // Left Click
{ {

View file

@ -143,6 +143,8 @@ namespace RacesOfValheim {
} }
} }
if (!RaceInput.Allowed) return;
int state = BossProgressionManager.GetCurrentProgressionState(); int state = BossProgressionManager.GetCurrentProgressionState();
if (Input.GetKeyDown(KeyCode.G)) { TryTransformWithRules(FenrirProgression.GetGKeyRules(state)); return; } if (Input.GetKeyDown(KeyCode.G)) { TryTransformWithRules(FenrirProgression.GetGKeyRules(state)); return; }

View file

@ -0,0 +1,36 @@
using System.Reflection;
using HarmonyLib;
namespace RacesOfValheim
{
/// <summary>
/// Gate for race ability input.
///
/// UnityEngine.Input reads the device directly, so abilities bound to keys or mouse buttons
/// kept firing while the map, the inventory, a chest, the chat or a menu had focus - dragging
/// an item inside a container also swung the transformed attack, for instance. Valheim runs
/// its own controls through Player.TakeInput, which reports false for exactly those states
/// (InventoryGui covers the backpack and chests alike), so mirror that instead of maintaining
/// a parallel list of GUI checks that would drift as the game changes.
/// </summary>
public static class RaceInput
{
// Not public on Player, hence the reflection; resolved once rather than per frame.
private static readonly MethodInfo TakeInputMethod = AccessTools.Method(typeof(Player), "TakeInput");
public static bool Allowed
{
get
{
Player player = Player.m_localPlayer;
if (player == null) return false;
// Fail open: if a game update ever renames the method, abilities stay usable
// rather than silently going dead.
if (TakeInputMethod == null) return true;
return (bool)TakeInputMethod.Invoke(player, null);
}
}
}
}

View file

@ -67,7 +67,7 @@ namespace RacesOfValheim
if (Player.m_localPlayer == null || Player.m_localPlayer.IsDead()) return; if (Player.m_localPlayer == null || Player.m_localPlayer.IsDead()) return;
// Handle Flight Toggle (RMB) - Disabled for Fader // Handle Flight Toggle (RMB) - Disabled for Fader
if (Input.GetMouseButtonDown(1) && formRules != null && formRules.Form != SkadiProgression.FormType.SkeletonDragon) if (RaceInput.Allowed && Input.GetMouseButtonDown(1) && formRules != null && formRules.Form != SkadiProgression.FormType.SkeletonDragon)
{ {
if (!isFlying && !isLanding) if (!isFlying && !isLanding)
{ {
@ -122,7 +122,7 @@ namespace RacesOfValheim
} }
// Handle Attack // Handle Attack
if (CanAttackNow()) if (CanAttackNow() && RaceInput.Allowed)
{ {
if (Input.GetMouseButtonDown(0)) if (Input.GetMouseButtonDown(0))
{ {

View file

@ -232,7 +232,7 @@ namespace RacesOfValheim
var traverse = Traverse.Create(__instance); var traverse = Traverse.Create(__instance);
Vector3 lookDir = Player.m_localPlayer.GetLookDir(); Vector3 lookDir = Player.m_localPlayer.GetLookDir();
bool boost = Input.GetKey(KeyCode.W); bool boost = RaceInput.Allowed && Input.GetKey(KeyCode.W);
bool onGround = __instance.IsOnGround(); bool onGround = __instance.IsOnGround();
traverse.Field("m_run").SetValue(boost); traverse.Field("m_run").SetValue(boost);

View file

@ -150,7 +150,7 @@ namespace RacesOfValheim
} }
} }
if (rules.ColdSpell) if (rules.ColdSpell && RaceInput.Allowed)
{ {
HandleColdSpell(rules); HandleColdSpell(rules);
} }
@ -167,6 +167,9 @@ namespace RacesOfValheim
return; return;
} }
// Form keys and the debug dump are ability input: they stay inert while a GUI has focus.
if (!RaceInput.Allowed) return;
int state = BossProgressionManager.GetCurrentProgressionState(); int state = BossProgressionManager.GetCurrentProgressionState();
if (Input.GetKeyDown(KeyCode.G)) { TryTransformWithRules(SkadiProgression.GetGKeyRules(state)); return; } if (Input.GetKeyDown(KeyCode.G)) { TryTransformWithRules(SkadiProgression.GetGKeyRules(state)); return; }
if (Input.GetKeyDown(KeyCode.H)) { TryTransformWithRules(SkadiProgression.GetHKeyRules(state)); return; } if (Input.GetKeyDown(KeyCode.H)) { TryTransformWithRules(SkadiProgression.GetHKeyRules(state)); return; }

View file

@ -119,14 +119,17 @@ namespace RacesOfValheim
{ {
if (!IsTrol()) return; if (!IsTrol()) return;
if (Input.GetKeyDown(KeyCode.G)) if (RaceInput.Allowed)
{ {
TryThrowRock(); if (Input.GetKeyDown(KeyCode.G))
} {
TryThrowRock();
}
if (Input.GetKeyDown(KeyCode.H)) if (Input.GetKeyDown(KeyCode.H))
{ {
TryTrueForm(); TryTrueForm();
}
} }
// Check log durability // Check log durability