Skip to content

Commit

Permalink
feat: Add option to avoid broadcasting inputs (#230)
Browse files Browse the repository at this point in the history
Co-authored-by: Tamás Gálffy <[email protected]>
  • Loading branch information
nicobatty and elementbound authored Aug 19, 2024
1 parent c971417 commit 5bf94d9
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 6 deletions.
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.7.0"
version="1.8.0"
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.7.0"
version="1.8.0"
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.7.0"
version="1.8.0"
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.7.0"
version="1.8.0"
script="netfox.gd"
15 changes: 14 additions & 1 deletion addons/netfox/rollback/rollback-synchronizer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ class_name RollbackSynchronizer

@export var root: Node = get_parent()
@export var state_properties: Array[String]

@export_subgroup("Inputs")
@export var input_properties: Array[String]

## This will broadcast input to all peers, turning this off will limit to sending it to the server only.
## Turning this off is recommended to save bandwith and reduce cheating risks.
@export var enable_input_broadcast: bool = true

var _record_state_props: Array[PropertyEntry] = []
var _record_input_props: Array[PropertyEntry] = []
var _auth_state_props: Array[PropertyEntry] = []
Expand Down Expand Up @@ -186,7 +192,7 @@ func _after_tick(_delta, _tick):
inputs[property] = []
inputs[property].push_back(tick_input[property])

rpc("_submit_input", inputs, NetworkTime.tick)
_attempt_submit_input(inputs)

while _states.size() > NetworkRollback.history_limit:
_states.erase(_states.keys().min())
Expand All @@ -196,6 +202,13 @@ func _after_tick(_delta, _tick):

_freshness_store.trim()

func _attempt_submit_input(input: Dictionary):
# TODO: Default to input broadcast in mesh network setups
if enable_input_broadcast:
rpc("_submit_input", input, NetworkTime.tick)
elif not multiplayer.is_server():
rpc_id(1, "_submit_input", input, NetworkTime.tick)

func _get_history(buffer: Dictionary, tick: int) -> Dictionary:
if buffer.has(tick):
return buffer[tick]
Expand Down
5 changes: 5 additions & 0 deletions docs/netfox/nodes/rollback-synchronizer.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ their respective players.

See [Property paths] on how to specify properties.

*enable_input_broadcast* toggles whether input properties are broadcast to all
peers, or only to the server. The default is *true* to support legacy
behaviour. It is recommended to turn this off to lower bandwidth and lessen the
attack surface for cheating.

> *Note* that it is not recommended to have both state and input properties on
> the same node. Since nodes with state belong to the server, and nodes with
> input belong to the player, it is difficult to separate ownership on the same
Expand Down
1 change: 1 addition & 0 deletions examples/forest-brawl/scenes/brawler.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ script = ExtResource("7_cmfmx")
root = NodePath("..")
state_properties = Array[String]([":transform", ":velocity", ":speed", "Displaceable:mass", "Displaceable:impulse"])
input_properties = Array[String](["Input:movement", "Input:aim"])
enable_input_broadcast = false

[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
replication_config = SubResource("SceneReplicationConfig_4cjl8")
Expand Down
2 changes: 1 addition & 1 deletion examples/forest-brawl/scripts/brawler-controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func _ready():
func _process(delta):
# Update animation
# Running
var movement = Vector3(input.movement.x, 0, input.movement.z) * speed
var movement = Vector3(velocity.x, 0, velocity.z) * speed
var relative_velocity = quaternion.inverse() * movement
relative_velocity.y = 0
relative_velocity /= speed
Expand Down

0 comments on commit 5bf94d9

Please sign in to comment.