Skip to content

Instantly share code, notes, and snippets.

@rcmagic
Last active October 19, 2025 20:56
Show Gist options
  • Select an option

  • Save rcmagic/f8d76bca32b5609e85ab156db38387e9 to your computer and use it in GitHub Desktop.

Select an option

Save rcmagic/f8d76bca32b5609e85ab156db38387e9 to your computer and use it in GitHub Desktop.

Revisions

  1. rcmagic revised this gist Jan 19, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions RollbackPseudoCode.txt
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ Start Program:
    Let remote_frame = latest frame received from the remote client
    Let remote_frame_advantage = (local_frame - remote_frame) sent from the remote client

    [Process Rollbacks]
    [Update Synchronization]
    [Determine the sync_frame]
    Let final_frame = remote_frame # We will only check inputs until the remote_frame, since we don't have inputs after.
    if remote_frame > local_frame then final_frame = local_frame # Incase the remote client is ahead of local, don't check past the local frame.
    @@ -54,7 +54,7 @@ Start Program:
    sync_frame = final_frame # All remote inputs matched the predicted inputs since the last synchronized frame.

    if [Rollback Condition] then
    [Process Rollbacks]
    [Execute Rollbacks]
    [Restore Game State]
    select inputs from (sync_frame + 1) through local_frame # Use all the inputs since the last sync frame until the current frame to simulate
    [Rollback Update]
  2. rcmagic revised this gist Oct 28, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RollbackPseudoCode.txt
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,7 @@ Start Program:
    [Process Rollbacks]
    [Determine the sync_frame]
    Let final_frame = remote_frame # We will only check inputs until the remote_frame, since we don't have inputs after.
    if remote_frame > local_frame then final_frame = remote_frame # Incase the remote client is ahead of local, don't check past the local frame.
    if remote_frame > local_frame then final_frame = local_frame # Incase the remote client is ahead of local, don't check past the local frame.
    select frames from (sync_frame + 1) through final_frame and find the first frame where predicted and remote inputs don't match
    if found then
    sync_frame = found frame - 1 # The found frame is the first frame where inputs don't match, so assume the previous frame is synchronized.
  3. rcmagic revised this gist Aug 29, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RollbackPseudoCode.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    Readme: In the following psuedo code, [] indicates a subroutine.
    Readme: In the following pseudo code, [] indicates a subroutine.
    Sometimes I choose to write the subroutine inline under the [] in order to maintain context.
    One important fact about the way rollbacks are handled here is that we are storing state for every frame.
    In any real implementation you only need to store one game state at a time. Storing a game
  4. rcmagic renamed this gist Aug 29, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. rcmagic revised this gist Aug 29, 2020. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions RollbackPsuedoCode.txt
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,10 @@ INITAL_FRAME := Any Integer # Specifies the initial
    [Restore Game State]
    Restores the game state to the frame set in sync_frame

    [Update Input]
    Predict the remote player's input if not available yet.
    Setup the local player and remote player's input for use in [Update Game]

    [Rollback Condition]
    local_frame > sync_frame AND remote_frame > sync_frame # No need to rollback if we don't have a frame after the previous sync frame to synchronize to.

    @@ -62,12 +66,11 @@ Start Program:
    [Normal Update]
    Increment local_frame
    [Get Local Player Input]
    Read the game pad for the local player and store it, associating it with local_frame
    Read the joystick/pad/keyboard for the local player and store it, associating it with local_frame
    [Send input to remote client]
    Send the input and the local_frame to the remote client so it arrives asap.

    [Update Input]
    Predict the remote player's input if not available yet.
    Setup the local player and remote player's input for use in [Update Game]
    [Update Game]
    [Store Game State]
    goto loop
  6. rcmagic revised this gist Aug 29, 2020. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion RollbackPsuedoCode.txt
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ Start Program:
    [Initialize Variables]

    loop:
    [Get Local Player Input]

    [Update Network]
    Let remote_frame = latest frame received from the remote client
    Let remote_frame_advantage = (local_frame - remote_frame) sent from the remote client
    @@ -61,7 +61,13 @@ Start Program:
    if [Time Synced] then
    [Normal Update]
    Increment local_frame
    [Get Local Player Input]
    Read the game pad for the local player and store it, associating it with local_frame
    [Send input to remote client]
    Send the input and the local_frame to the remote client so it arrives asap.
    [Update Input]
    Predict the remote player's input if not available yet.
    Setup the local player and remote player's input for use in [Update Game]
    [Update Game]
    [Store Game State]
    goto loop
  7. rcmagic revised this gist Aug 29, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RollbackPsuedoCode.txt
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ INITAL_FRAME := Any Integer # Specifies the initial
    Stores the game state for the current frame

    [Restore Game State]
    Restores the game state to the frame set in the sync_frame
    Restores the game state to the frame set in sync_frame

    [Rollback Condition]
    local_frame > sync_frame AND remote_frame > sync_frame # No need to rollback if we don't have a frame after the previous sync frame to synchronize to.
  8. rcmagic revised this gist Aug 29, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RollbackPsuedoCode.txt
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    Readme: In the following psuedo code, [] indicates a subroutine.
    Sometimes I choose to write the subroutine inline under the [] in order to maintain context.
    One import fact about the way rollbacks are handled here is that we are storing state for every frame.
    One important fact about the way rollbacks are handled here is that we are storing state for every frame.
    In any real implementation you only need to store one game state at a time. Storing a game
    state for every frame allows us to only rollback to the first frame where the predicted inputs don't match the true ones.

  9. rcmagic created this gist Aug 29, 2020.
    69 changes: 69 additions & 0 deletions RollbackPsuedoCode.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    Readme: In the following psuedo code, [] indicates a subroutine.
    Sometimes I choose to write the subroutine inline under the [] in order to maintain context.
    One import fact about the way rollbacks are handled here is that we are storing state for every frame.
    In any real implementation you only need to store one game state at a time. Storing a game
    state for every frame allows us to only rollback to the first frame where the predicted inputs don't match the true ones.


    ==Constants==
    MAX_ROLLBACK_FRAMES := Any Positive Integer # Specifies the maximum number of frames that can be resimulated
    FRAME_ADVANTAGE_LIMIT := Any Positive Integer # Specifies the number of frames the local client can progress ahead of the remote client before time synchronizing.
    INITAL_FRAME := Any Integer # Specifies the initial frame the game starts in. Cannot rollback before this frame.

    [Initialize Variables]
    local_frame := INITAL_FRAME # Tracks the latest updates frame.
    remote_frame := INITAL_FRAME # Tracks the latest frame received from the remote client
    sync_frame := INITAL_FRAME # Tracks the last frame where we synchronized the game state with the remote client. Never rollback before this frame.
    remote_frame_advantage := 0 # Latest frame advantage received from the remote client.

    [Store Game State]
    Stores the game state for the current frame

    [Restore Game State]
    Restores the game state to the frame set in the sync_frame

    [Rollback Condition]
    local_frame > sync_frame AND remote_frame > sync_frame # No need to rollback if we don't have a frame after the previous sync frame to synchronize to.

    [Time Synced]
    Let local_frame_advantage = local_frame - remote_frame # How far the client is ahead of the last reported frame by the remote client
    Let frame_advantage_difference = local_frame_advantage - remote_frame_advantage # How different is the frame advantage reported by the remote client and this one.
    local_frame_advantage < MAX_ROLLBACK_FRAMES AND frame_advantage_difference <= FRAME_ADVANTAGE_LIMIT # Only allow the local client to get so far ahead of remote.

    Start Program:
    [Initialize Variables]

    loop:
    [Get Local Player Input]
    [Update Network]
    Let remote_frame = latest frame received from the remote client
    Let remote_frame_advantage = (local_frame - remote_frame) sent from the remote client

    [Process Rollbacks]
    [Determine the sync_frame]
    Let final_frame = remote_frame # We will only check inputs until the remote_frame, since we don't have inputs after.
    if remote_frame > local_frame then final_frame = remote_frame # Incase the remote client is ahead of local, don't check past the local frame.
    select frames from (sync_frame + 1) through final_frame and find the first frame where predicted and remote inputs don't match
    if found then
    sync_frame = found frame - 1 # The found frame is the first frame where inputs don't match, so assume the previous frame is synchronized.
    else
    sync_frame = final_frame # All remote inputs matched the predicted inputs since the last synchronized frame.

    if [Rollback Condition] then
    [Process Rollbacks]
    [Restore Game State]
    select inputs from (sync_frame + 1) through local_frame # Use all the inputs since the last sync frame until the current frame to simulate
    [Rollback Update]
    [Update Input]
    [Update Game]
    [Store Game State]

    if [Time Synced] then
    [Normal Update]
    Increment local_frame
    [Update Input]
    [Update Game]
    [Store Game State]
    goto loop

    End Program