Multiplayer netcode

From nlelouche/Unity-SkillForge · View interactive version

Multiplayer netcode is an AI agent skill from the GitHub repository nlelouche/Unity-SkillForge. Setup for Unity Netcode for GameObjects (NGO). Handles connection, synchronization, RPCs, and NetworkVariables.

How to use this skill

Skill file

Multiplayer Netcode (NGO)

Overview

Implementation of Unity Netcode for GameObjects (NGO). Covers connection management (Host/Client), state synchronization (NetworkVariables), messaging (RPCs), and object spawning.

When to Use

Key Concepts

| Concept | Description |

|---------|-------------|

| NetworkManager | Core component handling connections |

| NetworkBehaviour | MonoBehavior extension for networked scripts |

| NetworkVariable | Variables synced automatically |

| ServerRpc | Client asks Server to do something |

| ClientRpc | Server tells Client to do something |

Architecture

┌─────────────────────────────────────────────────────────────┐
│                  AUTHORITATIVE SERVER                       │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   CLIENT A                SERVER (Host)             CLIENT B│
│  ┌──────────────┐        ┌──────────────┐      ┌──────────┐ │
│  │ Input (Move) │───────▶│ Validate     │◀─────│ Input    │ │
│  │              │ Start  │ Update State │      │          │ │
│  │ Present View │◀───────│ Broadcast    │──────▶ Present  │ │
│  └──────────────┘ Server │ State        │      │ View     │ │
│                   RPC    └──────────────┘      └──────────┘ │
│                             ClientRPC                       │
│                         NetworkVariable                     │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Best Practices

Few-Shot Examples

Example 1: Player Movement (Server Authoritative)

User: "Sync player movement."

Agent:

public class PlayerNet : NetworkBehaviour
{
    public override void OnNetworkSpawn() { ... }

    [ServerRpc]
    public void MoveServerRpc(Vector3 input)
    {
        // Server validates and moves
        transform.position += input * Speed * Time.deltaTime;
    }
    
    void Update() {
        if (IsOwner) {
            MoveServerRpc(Input.GetAxis("Vertical"));
        }
    }
}

Example 2: Synced Variable (Health)

User: "Sync health across clients."

Agent:

public class HealthNet : NetworkBehaviour
{
    public NetworkVariable<int> Health = new NetworkVariable<int>(100);

    public override void OnNetworkSpawn()
    {
        Health.OnValueChanged += (oldVal, newVal) => UpdateHealthBar(newVal);
    }
    
    [ServerRpc]
    public void TakeDamageServerRpc(int amount)
    {
        Health.Value -= amount; // Auto-syncs to all clients
    }
}

Example 3: Spawning Objects

User: "Spawn a bullet."

Agent:

// MUST be run on Server
private void Fire()
{
    var bullet = Instantiate(_bulletPrefab, pos, rot);
    bullet.GetComponent<NetworkObject>().Spawn();
}

TDD Contract

⚠️ Legacy Skill — Refactor Pending

Este skill NO tiene tests automatizados aún. El siguiente boilerplate es un punto de partida.

// Escribe estos tests ANTES de implementar:

// Test 1: should [expected behavior] when [condition]
[Test]
public void MultiplayerNetcode_Should{ExpectedBehavior}_When{Condition}()
{{
    // Arrange
    // TODO: Setup test fixtures
    
    // Act
    // TODO: Execute system under test
    
    // Assert
    Assert.Fail("Not implemented — write test first");
}}

// Test 2: should handle [edge case]
[Test]
public void MultiplayerNetcode_ShouldHandle{EdgeCase}()
{{
    // Arrange
    // TODO: Setup edge case scenario
    
    // Act
    // TODO: Execute
    
    // Assert
    Assert.Fail("Not implemented");
}}

// Test 3: should throw when [invalid input]
[Test]
public void MultiplayerNetcode_ShouldThrow_When{InvalidInput}()
{{
    // Arrange
    var invalidInput = default;
    
    // Act & Assert
    Assert.Throws<Exception>(() => {{ /* execute */ }});
}}

Pasos para completar el TDD:

  1. Descomenta los tests above
  2. Implementa la funcionalidad mínima para que compile
  3. Ejecuta los tests — deben fallar (RED)
  4. Implementa la funcionalidad real
  5. Verifica que los tests pasen (GREEN)
  6. Refactorea manteniendo los tests verdes

Nota: Este skill fue marcado como tdd_first: false durante la auditoría v2.0.1. La sección TDD fue agregada automáticamente pero requiere customización manual para reflejar el comportamiento real del skill.

Related Skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.