manager/Manager.Preloading.cs

Partial Manager class that preloads combat prefabs (blood effects, gibs, coin, chest) during the loading screen. Clones each prefab disabled, warms up particle emitters, disables Thing/BloodSprayer/BloodSplatDecal/GibFader components so no logic runs, then destroys the clones and preloads two death sounds.

using System.Threading.Tasks;

public partial class Manager
{
	private static readonly string[] CombatPrefabsToPreload =
	{
		"prefabs/effects/blood_explosion.prefab",
		"prefabs/effects/blood_explosion_miniboss.prefab",
		"prefabs/effects/blood_sprayer.prefab",
		"prefabs/effects/decal_blood_splat.prefab",
		"prefabs/gibs/zombie/left_hand.prefab",
		"prefabs/gibs/zombie/left_foot.prefab",
		"prefabs/gibs/zombie/head.prefab",
		"prefabs/gibs/organ.prefab",
		"prefabs/items/coin.prefab",
		"prefabs/enemies/chest.prefab"
	};

	protected override Task OnLoad( LoadingContext context )
	{
		context.Title = "Preparing combat effects";

		// Prepare the templates and their first particle/physics activation while
		// the loading screen is up, rather than doing that work on the first kill.
		// This runs on every client; blood and gibs are created locally by DieRpc.
		foreach ( var path in CombatPrefabsToPreload )
		{
			var prefab = GameObject.GetPrefab( path );
			if ( !prefab.IsValid() )
			{
				Log.Warning( $"Could not preload combat prefab: {path}" );
				continue;
			}

			var warmup = prefab.Clone( new CloneConfig { StartEnabled = false } );
			try
			{
				// Warm engine components without spawning loot, registering decals/gibs,
				// or running enemy/item logic. No warmup object survives into a frame.
				foreach ( var component in warmup.GetComponentsInChildren<Component>( includeDisabled: true, includeSelf: true ) )
				{
					if ( component is Thing or BloodSprayer or BloodSplatDecal or GibFader )
						component.Enabled = false;
				}

				warmup.Enabled = true;
				foreach ( var effect in warmup.GetComponentsInChildren<ParticleEffect>( includeSelf: true ) )
				{
					foreach ( var emitter in effect.GetComponents<ParticleEmitter>() )
						if ( emitter.Enabled ) emitter.Emit( effect );
				}
			}
			finally
			{
				warmup.DestroyImmediate();
			}
		}

		Sound.Preload( "enemy.die" );
		Sound.Preload( "miniboss_die" );

		return base.OnLoad( context );
	}
}