Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix starting order for RollbackSynchronizer #284

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion addons/netfox.extras/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="netfox.extras"
description="Game-specific utilities for Netfox"
author="Tamas Galffy"
version="1.8.3"
version="1.8.4"
script="netfox-extras.gd"
2 changes: 1 addition & 1 deletion addons/netfox.internals/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="netfox.internals"
description="Shared internals for netfox addons"
author="Tamas Galffy"
version="1.8.3"
version="1.8.4"
script="plugin.gd"
2 changes: 1 addition & 1 deletion addons/netfox.noray/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="netfox.noray"
description="Bulletproof your connectivity with noray integration for netfox"
author="Tamas Galffy"
version="1.8.3"
version="1.8.4"
script="netfox-noray.gd"
2 changes: 1 addition & 1 deletion addons/netfox/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="netfox"
description="Shared internals for netfox addons"
author="Tamas Galffy"
version="1.8.3"
version="1.8.4"
script="netfox.gd"
26 changes: 20 additions & 6 deletions addons/netfox/rollback/rollback-synchronizer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class_name RollbackSynchronizer
## synchronizing data between players, but with support for rollback.

@export var root: Node = get_parent()
## This is to give time for Input authority to be set.
@export var await_first_frame: bool = true
@export var state_properties: Array[String]

@export_subgroup("Inputs")
Expand All @@ -24,6 +26,7 @@ var _states: Dictionary = {}
var _inputs: Dictionary = {}
var _latest_state: int = -1
var _earliest_input: int
var _started_at_tick: int = -1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable ultimately ended up being unused, but its useful to keep so as to know when an avatar joined/started.


var _property_cache: PropertyCache
var _freshness_store: RollbackFreshnessStore
Expand All @@ -43,16 +46,12 @@ func process_settings():

_states.clear()
_inputs.clear()
_latest_state = NetworkTime.tick - 1
_earliest_input = NetworkTime.tick

# Gather state props - all state props are recorded
for property in state_properties:
var pe = _property_cache.get_entry(property)
_record_state_props.push_back(pe)

process_authority()

# Gather all rollback-aware nodes to simulate during rollbacks
_nodes = root.find_children("*")
_nodes.push_front(root)
Expand Down Expand Up @@ -84,13 +83,24 @@ func process_authority():
_record_input_props.push_back(pe)
_auth_input_props.push_back(pe)

func process_time_flags():
_latest_state = NetworkTime.tick - 1
_earliest_input = NetworkTime.tick
if (_started_at_tick == -1):
_started_at_tick = NetworkTime.tick

func _ready():
process_settings()

if (await_first_frame):
await get_tree().process_frame
process_authority()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After await, it is basically guaranteed the input authority is set, so process_authority can be safely invoked


if not NetworkTime.is_initial_sync_done():
# Wait for time sync to complete
await NetworkTime.after_sync
_latest_state = NetworkTime.tick - 1
process_time_flags()


NetworkTime.before_tick.connect(_before_tick)
NetworkTime.after_tick.connect(_after_tick)
Expand Down Expand Up @@ -160,7 +170,9 @@ func _record_tick(tick: int):
# Broadcast as new state
_latest_state = max(_latest_state, tick)
_states[tick] = PropertySnapshot.merge(_states.get(tick, {}), broadcast)
_submit_state.rpc(broadcast, tick)
for picked_peer_id in multiplayer.get_peers():
if (NetworkTime.is_client_synced(picked_peer_id)): #This assumes the server is the authority -_-
_submit_state.rpc_id(picked_peer_id, broadcast, tick)

# Record state for specified tick ( current + 1 )
if not _record_state_props.is_empty() and tick > _latest_state:
Expand Down Expand Up @@ -220,6 +232,8 @@ func _get_history(buffer: Dictionary, tick: int) -> Dictionary:
var latest = buffer.keys().max()

if tick < earliest:
_logger.warning("Tried to load tick %s which is earlier than the earliest we have recorded (%s)
Try increasing the history limit." % [tick, earliest])
Copy link
Contributor Author

@TheYellowArchitect TheYellowArchitect Sep 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug comment, to confirm #260 is fixed. Place this message on main branch, and you will see it goes from 0 to current. I will remove this message once this PR is reviewed/approved

return buffer[earliest]

if tick > latest:
Expand Down
110 changes: 55 additions & 55 deletions examples/forest-brawl/scenes/brawler.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -104,71 +104,71 @@ respawn_time = 8.0
transform = Transform3D(0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0, -0.5, 0)

[node name="Skeleton3D" parent="bomber-guy/rig" index="0"]
bones/0/position = Vector3(3.20746e-15, 0.583857, -0.0893679)
bones/0/position = Vector3(3.20746e-15, 0.561946, -0.0791427)
bones/0/rotation = Quaternion(0.255292, -7.97688e-15, 1.00226e-14, 0.966864)
bones/1/rotation = Quaternion(0.0205548, 1.19184e-07, 2.45031e-09, 0.999789)
bones/1/scale = Vector3(1.01931, 0.96247, 1.01931)
bones/2/rotation = Quaternion(0.096395, -4.59033e-12, 2.30305e-08, 0.995343)
bones/2/scale = Vector3(1.04897, 0.907002, 1.05131)
bones/3/rotation = Quaternion(-0.181044, -1.17239e-07, -2.15821e-08, 0.983475)
bones/3/scale = Vector3(0.935252, 1.1196, 0.963441)
bones/4/rotation = Quaternion(0.376178, 1.10453e-07, 4.48439e-08, 0.926548)
bones/4/scale = Vector3(0.922825, 1.17435, 0.922825)
bones/5/rotation = Quaternion(-0.0765415, -3.93158e-08, -1.58714e-08, 0.997066)
bones/5/scale = Vector3(1.00009, 1.00661, 0.995893)
bones/6/rotation = Quaternion(-0.43687, -4.72473e-08, -2.26897e-08, 0.899525)
bones/6/scale = Vector3(1.08356, 1.00264, 0.945663)
bones/7/position = Vector3(-3.6176e-08, 0.583857, -0.0893679)
bones/1/rotation = Quaternion(0.0037054, 1.19208e-07, 4.417e-10, 0.999993)
bones/1/scale = Vector3(1.01777, 0.965393, 1.01777)
bones/2/rotation = Quaternion(0.0570746, -1.28964e-12, 1.36195e-08, 0.99837)
bones/2/scale = Vector3(1.05125, 0.904271, 1.052)
bones/3/rotation = Quaternion(-0.149728, -1.17865e-07, -1.78488e-08, 0.988727)
bones/3/scale = Vector3(0.934626, 1.12843, 0.954347)
bones/4/rotation = Quaternion(0.359603, 1.11235e-07, 4.2868e-08, 0.933105)
bones/4/scale = Vector3(0.946208, 1.11695, 0.946208)
bones/5/rotation = Quaternion(-0.0813336, -3.0754e-08, -1.72157e-08, 0.996687)
bones/5/scale = Vector3(1.00007, 1.00492, 0.996523)
bones/6/rotation = Quaternion(-0.395121, -8.0111e-08, -3.45525e-08, 0.918629)
bones/6/scale = Vector3(1.05678, 0.984066, 0.974799)
bones/7/position = Vector3(-3.6176e-08, 0.561946, -0.0791428)
bones/7/rotation = Quaternion(-0.135651, -0.571304, -0.59224, 0.551781)
bones/8/position = Vector3(3.6176e-08, 0.583857, -0.0893679)
bones/8/position = Vector3(3.6176e-08, 0.561946, -0.0791428)
bones/8/rotation = Quaternion(-0.135651, 0.571304, 0.59224, 0.551781)
bones/9/position = Vector3(0.185305, 0.611893, -0.0339381)
bones/9/rotation = Quaternion(0.905251, -2.21189e-05, -0.000117811, 0.424877)
bones/9/scale = Vector3(1.00629, 0.987535, 1.00629)
bones/9/position = Vector3(0.185305, 0.589982, -0.023713)
bones/9/rotation = Quaternion(0.890042, -1.60252e-05, -0.000114532, 0.455879)
bones/9/scale = Vector3(1.00801, 0.984174, 1.00801)
bones/10/rotation = Quaternion(1.23085e-07, -5.52346e-06, -2.98159e-11, 1)
bones/11/rotation = Quaternion(0.385459, 6.31284e-06, -3.32111e-05, 0.922725)
bones/11/scale = Vector3(1.0012, 0.988229, 1.01087)
bones/11/rotation = Quaternion(0.452257, 3.33109e-06, -3.33071e-05, 0.891888)
bones/11/scale = Vector3(1.00155, 0.981624, 1.01741)
bones/12/rotation = Quaternion(-9.67563e-08, 8.29257e-06, -2.16274e-09, 1)
bones/13/rotation = Quaternion(-0.505198, 0.000109451, 2.25149e-05, 0.863003)
bones/13/scale = Vector3(0.992628, 0.997842, 1.00979)
bones/13/rotation = Quaternion(-0.538477, 0.000110231, 1.65799e-05, 0.84264)
bones/13/scale = Vector3(0.990557, 0.995594, 1.01424)
bones/14/rotation = Quaternion(5.02433e-08, 0.977548, -0.210714, 2.33145e-07)
bones/14/scale = Vector3(0.99948, 1.00078, 0.999746)
bones/15/position = Vector3(-0.185305, 0.611893, -0.0339381)
bones/15/rotation = Quaternion(0.99633, 4.19078e-05, -0.000130651, -0.08559)
bones/15/scale = Vector3(1.00638, 0.987357, 1.00638)
bones/17/rotation = Quaternion(0.389683, 1.45563e-05, -3.68506e-05, 0.920949)
bones/17/scale = Vector3(1.00122, 0.987896, 1.0112)
bones/15/position = Vector3(-0.185305, 0.589982, -0.023713)
bones/15/rotation = Quaternion(0.99649, 4.37977e-05, -0.000130417, -0.0837092)
bones/15/scale = Vector3(1.00726, 0.985638, 1.00726)
bones/17/rotation = Quaternion(0.427841, 1.44422e-05, -3.8107e-05, 0.903854)
bones/17/scale = Vector3(1.0014, 0.984456, 1.0146)
bones/18/rotation = Quaternion(-4.84353e-08, 4.04099e-06, 1.10813e-08, 1)
bones/19/rotation = Quaternion(-0.871762, 8.53352e-05, 7.82363e-05, 0.48993)
bones/19/scale = Vector3(0.992445, 0.99873, 1.0091)
bones/19/rotation = Quaternion(-0.889997, 8.56441e-05, 7.61367e-05, 0.455967)
bones/19/scale = Vector3(0.991415, 1.0004, 1.00856)
bones/20/rotation = Quaternion(1.02643e-08, 0.977516, -0.210861, -2.46051e-07)
bones/20/scale = Vector3(1.00003, 0.999961, 1.00001)
bones/21/position = Vector3(0.0507796, 1.02886, 0.287226)
bones/21/rotation = Quaternion(-0.472842, -0.270157, -0.416495, 0.727989)
bones/22/position = Vector3(0.242455, 1.04749, 0.191412)
bones/22/rotation = Quaternion(-0.280276, 0.143785, -0.88237, 0.349562)
bones/22/scale = Vector3(1.07211, 0.87008, 1.07211)
bones/23/rotation = Quaternion(3.18531e-08, 0.0853649, 8.32473e-08, 0.99635)
bones/24/rotation = Quaternion(0.421944, 0.147999, -0.308633, 0.839527)
bones/24/scale = Vector3(0.960023, 0.997541, 1.06918)
bones/25/rotation = Quaternion(2.97979e-08, -0.026165, -1.54692e-07, 0.999658)
bones/26/rotation = Quaternion(0.143976, -0.0275267, 0.0637975, 0.987139)
bones/26/scale = Vector3(1.00575, 0.989855, 1.00455)
bones/27/position = Vector3(-0.0507796, 1.02886, 0.287226)
bones/27/rotation = Quaternion(-0.472842, 0.270157, 0.416495, 0.727989)
bones/28/position = Vector3(-0.242455, 1.04749, 0.191412)
bones/28/rotation = Quaternion(-0.425562, -0.363618, 0.693798, 0.453127)
bones/28/scale = Vector3(0.983063, 1.03476, 0.983063)
bones/29/rotation = Quaternion(3.98409e-08, -0.0370571, -2.15209e-07, 0.999313)
bones/30/rotation = Quaternion(0.232389, -0.0893491, 0.253375, 0.93478)
bones/30/scale = Vector3(1.06864, 0.878868, 1.06637)
bones/31/rotation = Quaternion(-3.48398e-08, -0.117463, 1.57868e-07, 0.993077)
bones/32/rotation = Quaternion(0.41303, -0.11386, 0.189505, 0.883476)
bones/32/scale = Vector3(0.954337, 1.01045, 1.05417)
bones/33/position = Vector3(0.1184, 1.00881, 0.15031)
bones/33/rotation = Quaternion(-1.44276e-14, 0.544482, 0.838773, -2.01673e-14)
bones/34/position = Vector3(-0.1184, 1.00881, 0.15031)
bones/34/rotation = Quaternion(-1.44276e-14, 0.544482, 0.838773, -2.01673e-14)
bones/21/position = Vector3(0.0507796, 1.02492, 0.278989)
bones/21/rotation = Quaternion(-0.490535, -0.28028, -0.409751, 0.716186)
bones/22/position = Vector3(0.242455, 1.03883, 0.182378)
bones/22/rotation = Quaternion(-0.313024, 0.189079, -0.843138, 0.394187)
bones/22/scale = Vector3(1.10679, 0.816361, 1.10679)
bones/23/rotation = Quaternion(7.605e-08, 0.0737019, 2.07023e-07, 0.99728)
bones/24/rotation = Quaternion(0.498155, 0.158158, -0.368934, 0.76858)
bones/24/scale = Vector3(0.92719, 0.990345, 1.1277)
bones/25/rotation = Quaternion(3.37927e-08, -0.026504, -1.38135e-07, 0.999649)
bones/26/rotation = Quaternion(0.103732, -0.0287162, 0.0539946, 0.992723)
bones/26/scale = Vector3(1.02971, 0.946527, 1.02689)
bones/27/position = Vector3(-0.0507796, 1.02492, 0.278989)
bones/27/rotation = Quaternion(-0.490535, 0.28028, 0.409751, 0.716186)
bones/28/position = Vector3(-0.242455, 1.03883, 0.182378)
bones/28/rotation = Quaternion(-0.442671, -0.375887, 0.652601, 0.486686)
bones/28/scale = Vector3(0.976904, 1.04785, 0.976904)
bones/29/rotation = Quaternion(-3.46401e-08, -0.0245002, -5.38719e-08, 0.9997)
bones/30/rotation = Quaternion(0.246764, -0.0944549, 0.286493, 0.920928)
bones/30/scale = Vector3(1.10423, 0.824063, 1.1023)
bones/31/rotation = Quaternion(-1.17522e-07, -0.125216, 1.96651e-07, 0.99213)
bones/32/rotation = Quaternion(0.407144, -0.120311, 0.226357, 0.876653)
bones/32/scale = Vector3(0.940913, 1.01816, 1.08918)
bones/33/position = Vector3(0.1184, 0.998194, 0.143219)
bones/33/rotation = Quaternion(-1.24159e-14, 0.564869, 0.825181, -1.93569e-14)
bones/34/position = Vector3(-0.1184, 0.998194, 0.143219)
bones/34/rotation = Quaternion(-1.24159e-14, 0.564869, 0.825181, -1.93569e-14)

[node name="AnimationTree" type="AnimationTree" parent="."]
tree_root = SubResource("AnimationNodeBlendTree_8to0y")
Expand Down
4 changes: 0 additions & 4 deletions examples/forest-brawl/scripts/brawler-controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ func _ready():

_snap_to_spawn()

# TODO: What if the RollbackSynchronizer had a flag for this?
# Wait a frame so Input has time to get its authority set
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And that's what I did :P

await get_tree().process_frame
rollback_synchronizer.process_settings()
GameEvents.on_brawler_spawn.emit(self)
NetworkTime.on_tick.connect(_tick)

Expand Down
4 changes: 0 additions & 4 deletions examples/multiplayer-netfox/scripts/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ func _ready():

if input == null:
input = $Input

# Wait a single frame, so player spawner has time to set input owner
await get_tree().process_frame
$RollbackSynchronizer.process_settings()

func _rollback_tick(delta, _tick, _is_fresh):
# Add the gravity.
Expand Down