Stop remote clients spawning a dragon model every frame

The multiplayer visual sync named each spawned model "<form>_multiplayer"
but searched for an existing one called "SkadiBody_Form", a name nothing
is ever assigned. The lookup therefore missed every time, and since
VisEquipment.UpdateVisuals runs each frame off CustomUpdate, observers
instantiated a fresh dragon roughly sixty times a second for as long as
the Skadi player stayed transformed.

The transformed player saw nothing wrong, because owners return early,
and the pile only got cleaned up in the reverted-to-human branch, so the
lag never let up on its own.

Look the model up by the name actually assigned. The scan also destroys
any surplus "_multiplayer" children it walks past, so clients that have
already accumulated a pile clear it on the first frame after updating
instead of needing to rejoin.
This commit is contained in:
kanym 2026-08-01 13:24:44 +02:00
commit 16275d6abc

View file

@ -28,52 +28,58 @@ namespace RacesOfValheim
[HarmonyPatch(typeof(VisEquipment), "UpdateVisuals")]
public static class Skadi_MultiplayerVisuals_Patch
{
private const string FormSuffix = "_multiplayer";
public static void Postfix(VisEquipment __instance)
{
if (!__instance.m_isPlayer) return;
Player player = __instance.GetComponent<Player>();
if (player == null || player.IsOwner()) return; // Only process remote players
ZNetView nview = player.GetComponent<ZNetView>();
if (nview != null && nview.IsValid() && nview.GetZDO().GetString("RacesOfValheim_Race", "Human") == "Skadi")
{
string formName = nview.GetZDO().GetString("RacesOfValheim_SkadiForm", "");
Transform playerVisual = player.transform.Find("Visual");
Transform formBody = player.transform.Find("SkadiBody_Form");
if (!string.IsNullOrEmpty(formName)) {
if (formBody != null && formBody.gameObject.name != formName + "_multiplayer") {
UnityEngine.Object.Destroy(formBody.gameObject);
formBody = null;
}
if (formBody == null) {
GameObject prefab = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>(formName);
if (prefab != null) {
Transform visual = prefab.transform.Find("Visual");
if (visual != null) {
GameObject formInstance = UnityEngine.Object.Instantiate(visual.gameObject, player.transform);
formInstance.name = formName + "_multiplayer";
if (formName == "Hatchling") formInstance.transform.localPosition = new Vector3(0f, -1.0f, 0f);
else formInstance.transform.localPosition = Vector3.zero;
formInstance.transform.localRotation = Quaternion.identity;
}
}
}
if (playerVisual != null) playerVisual.gameObject.SetActive(false);
} else {
if (formBody != null) UnityEngine.Object.Destroy(formBody.gameObject);
// Fader form has a different name? "SkadiBody_Form" was used earlier, we just find all children and destroy them if they have "_multiplayer"
foreach (Transform child in player.transform) {
if (child.name.EndsWith("_multiplayer")) {
UnityEngine.Object.Destroy(child.gameObject);
}
}
if (playerVisual != null) playerVisual.gameObject.SetActive(true);
ZNetView nview = player.GetComponent<ZNetView>();
if (nview == null || !nview.IsValid()) return;
if (nview.GetZDO().GetString("RacesOfValheim_Race", "Human") != "Skadi") return;
string formName = nview.GetZDO().GetString("RacesOfValheim_SkadiForm", "");
Transform playerVisual = player.transform.Find("Visual");
string wanted = formName + FormSuffix;
// VisEquipment.UpdateVisuals runs every frame off CustomUpdate, so the spawned model
// has to be found again by the exact name it was given. This used to search for
// "SkadiBody_Form" - a name nothing is ever assigned - so the lookup missed every
// time and a fresh dragon was instantiated each frame. Remote players watched the
// models pile up on the spot and their client degraded until the Skadi player
// reverted; the transformed player saw nothing, since owners return above.
// Anything left over from that, or from a previous form, is cleaned up here too.
Transform formBody = null;
foreach (Transform child in player.transform)
{
if (!child.name.EndsWith(FormSuffix)) continue;
if (formBody == null && child.name == wanted) formBody = child;
else UnityEngine.Object.Destroy(child.gameObject);
}
if (string.IsNullOrEmpty(formName))
{
if (playerVisual != null) playerVisual.gameObject.SetActive(true);
return;
}
if (formBody == null)
{
GameObject prefab = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>(formName);
Transform visual = prefab != null ? prefab.transform.Find("Visual") : null;
if (visual != null)
{
GameObject formInstance = UnityEngine.Object.Instantiate(visual.gameObject, player.transform);
formInstance.name = wanted;
formInstance.transform.localPosition = formName == "Hatchling" ? new Vector3(0f, -1.0f, 0f) : Vector3.zero;
formInstance.transform.localRotation = Quaternion.identity;
}
}
if (playerVisual != null) playerVisual.gameObject.SetActive(false);
}
}