RacesOfValheim/JotunnModStub/Fenrir/FormAnimEventSound.cs
kanym 0674329592 fix(fenrir): spawn sounds the way vanilla spawns effects, without ghost init
Ghost init was meant to keep a sound's ZNetView from writing a ZDO. It
does not: ZNetView.Awake still creates the ZDO and only skips registering
the instance, so ZNetScene sees an unbuilt ZDO and builds a second copy.
Vanilla's EffectList.Create uses a plain Instantiate, and TimedDestruction
deletes the ZDO through ZNetScene.Destroy when the sound ends. The spawn
now does the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 18:50:51 +02:00

88 lines
4.1 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Plays the sounds Valheim wires into a creature's own animation clips, for a player wearing
/// that creature.
///
/// THE HOWL WAS NEVER A PREFAB TO LOOK UP. It is an AnimationEvent. The wolf's animator carries a
/// clip called 'C_Hawl' whose single event, at 0.39s, calls a method named Effect and hands it the
/// sound as an object reference:
///
/// 'C_Hawl' 3.13s loop=True events=1
/// @0.39s Effect() ref='sfx_wolf_haul' (GameObject)
///
/// Two things follow from that, and both are better than what the mod was doing. First, the clip
/// already plays on its own: a player in wolf form idles through 'C_Hawl' like any other idle, so
/// the howl animation costs nothing to get. Second, the sound belongs at 0.39s into that clip and
/// nowhere else, which is something no amount of instantiating a prefab on a keypress can match.
///
/// Three names, three spellings, and that is the whole reason this took so long to find: the clip
/// is 'C_Hawl', the sound is 'sfx_wolf_haul', and the mod was looking for 'sfx_wolf_howl' - the
/// only correct spelling of the three, and the only one that does not exist.
///
/// Unity calls Effect BY NAME on the components of the GameObject that owns the Animator, so this
/// has to sit there and the method must keep that name.
/// </summary>
internal class FormAnimEventSound : MonoBehaviour
{
private static readonly HashSet<string> logged = new HashSet<string>();
/// <summary>
/// Off whenever the game's own receiver is on this object, which in practice is always.
///
/// A player in wolf form idles through 'C_Hawl' and howls, out loud, with no help from this
/// class - confirmed in game. AnimationEffect, which sits beside the Animator on the form
/// along with CharacterAnimEvent, is doing that already. Two receivers for one event means two
/// howls, so this stands down and stays only as cover for a form that turns out not to carry
/// one. The log says which of the two happened, once per form, so nobody has to wonder.
/// </summary>
private bool standDown;
private void Awake()
{
standDown = GetComponent("AnimationEffect") != null;
if (logged.Add("attach/" + name))
{
Jotunn.Logger.LogInfo(
standDown
? $"[FenrirForm] '{name}' already carries AnimationEffect; leaving animation sounds to it."
: $"[FenrirForm] '{name}' has no AnimationEffect; handling animation sounds here.");
}
}
/// <summary>
/// CALLED BY UNITY from an AnimationEvent. Do not rename, and do not make it private.
///
/// Only object references are taken, and only when they carry audio. The clips also fire
/// Effect with a string and no reference - 'Jump' does, with string='jump' - and that form
/// means "look something up", which is a different job and somebody else's. The audio test is
/// there because an animation can reference anything at all, and a form is not the place to
/// find out what spawning an arbitrary vanilla object does.
/// </summary>
public void Effect(AnimationEvent e)
{
if (standDown) return;
GameObject prefab = e.objectReferenceParameter as GameObject;
if (prefab == null) return;
bool audible = prefab.GetComponentInChildren<AudioSource>(true) != null;
if (logged.Add(prefab.name))
{
Jotunn.Logger.LogInfo(
$"[FenrirForm] animation event Effect on '{name}' -> '{prefab.name}' " +
$"audio={audible}{(audible ? "" : ", skipped")}.");
}
if (!audible) return;
// Through the same spawn as every other Fenrir sound, so a change there reaches this too.
FenrirCreatureSound.Spawn(prefab, transform.position);
}
}
}