Components/MenuBootstrap.cs

A Component that lazily starts a network session when the player first opens the in-game menu at /play. On update it checks the main menu URL, avoids repeating the request, verifies a NetworkSession exists, and calls CreateSession on it.

Networking
using Machines.UI;

namespace Machines.Components;

/// <summary>
/// Creates the network session lazily when the player first opens /play; skips if already in a session.
/// </summary>
public sealed class MenuBootstrap : Component
{
	private bool _requested;

	protected override void OnUpdate()
	{
		if ( _requested || Sandbox.Networking.IsActive )
			return;

		// Host the lobby on first visit to /play.
		if ( MainMenuPanel.Instance?.CurrentUrl == "/play" )
		{
			_requested = true;

			var session = Scene.Get<NetworkSession>();
			if ( !session.IsValid() )
			{
				Log.Warning( "MenuBootstrap: NetworkSession missing." );
				return;
			}

			session.CreateSession();
		}
	}
}