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:
parent
cccabe16ef
commit
16275d6abc
1 changed files with 42 additions and 36 deletions
|
|
@ -28,52 +28,58 @@ namespace RacesOfValheim
|
||||||
[HarmonyPatch(typeof(VisEquipment), "UpdateVisuals")]
|
[HarmonyPatch(typeof(VisEquipment), "UpdateVisuals")]
|
||||||
public static class Skadi_MultiplayerVisuals_Patch
|
public static class Skadi_MultiplayerVisuals_Patch
|
||||||
{
|
{
|
||||||
|
private const string FormSuffix = "_multiplayer";
|
||||||
|
|
||||||
public static void Postfix(VisEquipment __instance)
|
public static void Postfix(VisEquipment __instance)
|
||||||
{
|
{
|
||||||
if (!__instance.m_isPlayer) return;
|
if (!__instance.m_isPlayer) return;
|
||||||
Player player = __instance.GetComponent<Player>();
|
Player player = __instance.GetComponent<Player>();
|
||||||
if (player == null || player.IsOwner()) return; // Only process remote players
|
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) {
|
ZNetView nview = player.GetComponent<ZNetView>();
|
||||||
GameObject prefab = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>(formName);
|
if (nview == null || !nview.IsValid()) return;
|
||||||
if (prefab != null) {
|
if (nview.GetZDO().GetString("RacesOfValheim_Race", "Human") != "Skadi") return;
|
||||||
Transform visual = prefab.transform.Find("Visual");
|
|
||||||
if (visual != null) {
|
string formName = nview.GetZDO().GetString("RacesOfValheim_SkadiForm", "");
|
||||||
GameObject formInstance = UnityEngine.Object.Instantiate(visual.gameObject, player.transform);
|
Transform playerVisual = player.transform.Find("Visual");
|
||||||
formInstance.name = formName + "_multiplayer";
|
string wanted = formName + FormSuffix;
|
||||||
if (formName == "Hatchling") formInstance.transform.localPosition = new Vector3(0f, -1.0f, 0f);
|
|
||||||
else formInstance.transform.localPosition = Vector3.zero;
|
// VisEquipment.UpdateVisuals runs every frame off CustomUpdate, so the spawned model
|
||||||
formInstance.transform.localRotation = Quaternion.identity;
|
// 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
|
||||||
if (playerVisual != null) playerVisual.gameObject.SetActive(false);
|
// reverted; the transformed player saw nothing, since owners return above.
|
||||||
} else {
|
// Anything left over from that, or from a previous form, is cleaned up here too.
|
||||||
if (formBody != null) UnityEngine.Object.Destroy(formBody.gameObject);
|
Transform formBody = null;
|
||||||
|
foreach (Transform child in player.transform)
|
||||||
// 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(FormSuffix)) continue;
|
||||||
if (child.name.EndsWith("_multiplayer")) {
|
|
||||||
UnityEngine.Object.Destroy(child.gameObject);
|
if (formBody == null && child.name == wanted) formBody = child;
|
||||||
}
|
else UnityEngine.Object.Destroy(child.gameObject);
|
||||||
}
|
}
|
||||||
if (playerVisual != null) playerVisual.gameObject.SetActive(true);
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue