Index of every page on this site: llms.txt
All skills
jame581 avatar

/multiplayer-sync

@426e8ab
by Jan Mesarčjame581/godotprompter979 stars
63
Run it yourself

Use when synchronizing multiplayer state — MultiplayerSynchronizer, interpolation, prediction, and lag compensation

Use this Skill: https://skilld.dev/gh/jame581/godotprompter/multiplayer-sync

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesinterpolation.md

≈1.2k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Interpolation

Reference for skills/multiplayer-sync/SKILL.md — visual interpolation between snapshots in _process. GDScript + C#.

← Back to SKILL.md


3. Interpolation

Network updates arrive in discrete ticks (e.g. every 50 ms at 20 Hz). Without interpolation, remote players visibly stutter from position to position. Interpolation smooths this by blending between the previous received state and the current received state over time.

Why _process, Not _physics_process

  • _physics_process runs at a fixed physics rate (default 60 Hz) and is coupled to simulation.
  • _process runs every rendered frame, so the blend advances smoothly between network updates.
  • Visual smoothing belongs in _process because it does not affect gameplay state — it only affects what the player sees.
  • Blend by the time elapsed since the last update, not by Engine.get_physics_interpolation_fraction(): that fraction restarts every physics tick (60 Hz by default), while network updates arrive every few ticks.

Interpolation (GDScript)

# remote_player_display.gd — attached to a non-authority instance
extends Node2D

var _prev_pos: Vector2 = Vector2.ZERO
var _curr_pos: Vector2 = Vector2.ZERO
var _prev_health: int = 100
var _curr_health: int = 100
var _since_update: float = 0.0

@onready var _sync_source: Node = $"../SyncedPlayer"  # has synced_position / synced_health
@onready var _sync: MultiplayerSynchronizer = $"../SyncedPlayer/MultiplayerSynchronizer"


func _ready() -> void:
    # Disable physics on remote display nodes — authority drives state.
    set_physics_process(false)
    # The signal belongs to the MultiplayerSynchronizer, not to the node it replicates.
    # "synchronized" covers "Always" properties; "delta_synchronized" covers "On Change" ones.
    _sync.synchronized.connect(_on_synchronized)


func _on_synchronized() -> void:
    # Emitted after a received update has been applied to the properties.
    _prev_pos     = global_position   # start from where we are drawn now
    _curr_pos     = _sync_source.synced_position
    _prev_health  = _curr_health
    _curr_health  = _sync_source.synced_health
    _since_update = 0.0


func _process(delta: float) -> void:
    # 0.0 at the update, 1.0 one sync interval later (replication_interval 0 = every frame).
    _since_update += delta
    var interval: float = maxf(_sync.replication_interval, delta)
    var f: float = clampf(_since_update / interval, 0.0, 1.0)
    global_position = _prev_pos.lerp(_curr_pos, f)
    # Health and other discrete values are not lerped — snap on change.

Match the sender's interval. replication_interval is read only on the sending peer (MultiplayerSynchronizer::sync_interval_usec, modules/multiplayer/multiplayer_synchronizer.cpp:128 at 4.3-stable) and is not replicated. The receiver's copy of the node must be configured with the same value, or _sync.replication_interval here gives the wrong blend window. Set it in the shared scene, not from code on one peer. Properties synced "On Change" follow delta_interval instead, so read that one for them.

Interpolation (C#)

// RemotePlayerDisplay.cs — attached to a non-authority instance
using Godot;

public partial class RemotePlayerDisplay : Node2D
{
    private Vector2 _prevPos = Vector2.Zero;
    private Vector2 _currPos = Vector2.Zero;
    private int _prevHealth;
    private int _currHealth;
    private double _sinceUpdate;

    private SyncedPlayer _syncSource = null!;
    private MultiplayerSynchronizer _sync = null!;

    public override void _Ready()
    {
        SetPhysicsProcess(false);
        _syncSource = GetNode<SyncedPlayer>("../SyncedPlayer");
        // The signal belongs to the MultiplayerSynchronizer, not to the node it replicates.
        _sync = GetNode<MultiplayerSynchronizer>("../SyncedPlayer/MultiplayerSynchronizer");
        _sync.Synchronized += OnSynchronized;
    }

    private void OnSynchronized()
    {
        _prevPos     = GlobalPosition; // start from where we are drawn now
        _currPos     = _syncSource.SyncedPosition;
        _prevHealth  = _currHealth;
        _currHealth  = _syncSource.SyncedHealth;
        _sinceUpdate = 0.0;
    }

    public override void _Process(double delta)
    {
        // 0 at the update, 1 one sync interval later.
        _sinceUpdate += delta;
        double interval = Mathf.Max(_sync.ReplicationInterval, delta);
        float f = (float)Mathf.Clamp(_sinceUpdate / interval, 0.0, 1.0);
        GlobalPosition = _prevPos.Lerp(_currPos, f);
        // Discrete values like health snap immediately — no lerp.
    }
}

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts8d3 checks · Risk SAFE
  • Gen Agent Trust Hub8d

    This skill provides documentation and code templates for implementing multiplayer synchronization in the Godot 4.3+ engine. It covers best practices for property replication, client-side prediction, and server-side validation. No security risks were identified.

  • Socket8d

    No alerts

  • Snyk8d

    Risk: LOW · No issues

Signed by skilld at 426e8ab. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 6 days ago.

Activeupdated last month

README badge

README badge for jame581/godotprompter/multiplayer-sync