Skip to content

Commit 9a7cd3f

Browse files
committed
Add some documentation on how to use the NetSync.
1 parent 4bfc9b4 commit 9a7cd3f

4 files changed

Lines changed: 100 additions & 117 deletions

File tree

doc_classes/GdNetworkedController.xml

Lines changed: 0 additions & 98 deletions
This file was deleted.

doc_classes/GdSceneSynchronizer.xml

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,106 @@
33
<brief_description>
44
</brief_description>
55
<description>
6+
The GdSceneSynchronizer synchronizes the scene.
7+
This plugin uses a synchronization model that works best with realtime games and allows to network complex scenes without introducing any input lag. This class supports two networking methods, where each is the complementary to the other, offering the best networking method for each situation.
8+
- [b]METHOD 1. The simulation:[/b] This is the best networking method to synchronize a character (or any node which affects the gameplay directly) without introducing any input lag. This method is the most expensive in terms of bandwidth and CPU usage.
9+
- [b]METHOD 2. The trickling:[/b] This is the best networking method to synchronize background objects or elements that doesn't immediatelly (or directly) affects the player's gameplay. This method is cheap on the bandwidth and CPU usage, at the cost of latency and input lag.
10+
11+
[b]Prerequisites[/b]
12+
The prerequisite to use this plugin is to have the game peers connection, map loading, and the charcter spawning. Follow this to know how to implement it: [https://docs.godotengine.org/en/stable/tutorials/networking/high_level_multiplayer.html]
13+
Then, you need to override the GdSceneSynchronizer and make that class a singelton. From now on we will refer to it as [i]GameSceneSync[/i] In this way, you will be able to add functionalities on top of the GdSceneSynchronizer (like the object spawning), tied to your game. Now you are ready to use it!
14+
At this point, you should be able to start a [b]server[/b] and [b]2 clients[/b] connected to that server. Each client should have its own character, despite the characters doesn't react to the player's input yet.
15+
16+
[b]Setup the synchronization[/b]
17+
The really first thing to do is to implement the character. Assuming your character is a scene which first node is a [RigidBody] in kinematic mode. Open the script and search the [code]_ready()[/code] function. Then add the following code:
18+
[codeblock]
19+
func _ready() -> void:
20+
# Register the node as synchronized. This will create a NetId and call `_setup_synchronizer`.
21+
GameSceneSync.register_node(self)
22+
23+
func _setup_synchronizer(local_id) -> void:
24+
# This function is called by the NetworkSynchronizer to setup the node synchronization model.
25+
# The function `setup_controller` is used to specify the functions that will controll the character.
26+
GameSceneSync.setup_controller(self, get_multiplayer_authority(), _collect_inputs, _count_input_size, _are_inputs_different, _controller_process)
27+
# Register all the variables to keep in sync that the function `_controller_process` modifies.
28+
GameSceneSync.register_variable(self, "velocity")
29+
GameSceneSync.register_variable(self, "transform")
30+
31+
# ------------------------------------------------------------------- Networking
32+
func _collect_inputs(delta: float, buffer: DataBuffer) -> void:
33+
# This function is called by the NetworkSynchronizer each frame, only on the client side, to collect the player's input into the buffer.
34+
var input_direction := Vector3()
35+
var wants_to_jump: bool = false
36+
37+
if Input.is_action_pressed("forward"):
38+
input_direction.x = 1
39+
if Input.is_action_pressed("backward"):
40+
input_direction.x = -1
41+
if Input.is_action_pressed("left"):
42+
input_direction.z = 1
43+
if Input.is_action_pressed("right"):
44+
input_direction.z = -1
45+
if Input.is_action_pressed("jump"):
46+
wants_to_jump = true
47+
48+
buffer.add_vector3(input_direction)
49+
buffer.add_bool(wants_to_jump)
50+
51+
52+
func _count_input_size(inputs: DataBuffer) -> int:
53+
# This function is called by the NetworkSynchronizer to read the buffer size.
54+
# To keep the buffer as small as possible, to save bandwidth, the buffer size is never stored.
55+
var size = 0
56+
size += inputs.get_vector3_size()
57+
size += inputs.get_bool_size()
58+
return size
59+
60+
61+
func _are_inputs_different(inputs_A: DataBuffer, inputs_B: DataBuffer) -> bool:
62+
# The NetworkSynchronizer will call this function to compare two input buffers.
63+
if inputs_A.size != inputs_B.size:
64+
return true
65+
66+
var input_direction_A = inputs_A.read_vector3()
67+
var input_direction_B = inputs_B.read_vector3()
68+
if input_direction_A != input_direction_B:
69+
return true
70+
71+
var jump_A: bool = inputs_A.read_bool()
72+
var jump_B: bool = inputs_B.read_bool()
73+
if jump_A!= jump_B:
74+
return true
75+
76+
return false
77+
78+
79+
func _controller_process(delta: float, buffer: DataBuffer) -> void:
80+
# This function is executed by the NetworkSynchronizer each frame, on the clients and the server, to advance the simulation.
81+
# The buffer contains the player's input that you can read as follow.
82+
var input_direction: Vector3 = buffer.read_vector3()
83+
var wants_to_jump: bool = buffer.read_bool()
84+
85+
move_the_character(delta, input_direction, wants_to_jump)
86+
[/codeblock]
87+
88+
The above code is an example of how to implement a controllable character and now the project is ready to play.
89+
90+
As you can notice from the above code, the synchronization code is abstracted, and all you have to do is specify what to synchronize and few functions to collect and process the player's input.
91+
The NetworkSynchronizer will take care of the networking side of it. The default synchronization method is the [b]simulated[/b] one. To know more about what the NetworkSynchronizer does, read the next section.
92+
93+
**Networking under the hood**
94+
In this section, we will focus on what the NetworkSynchronizer does to network the simulated nodes.
95+
96+
The networking model used by this plugin is based on the idea of "prediction and reconciliation". You can read more about it here:
97+
- Client side prediction server reconciliation: https://www.gabrielgambetta.com/client-side-prediction-server-reconciliation.html
98+
- Rocket League networking: https://www.youtube.com/watch?v=ueEmiDM94IE
99+
In short; The client send all the player's input to the server, but instead of waiting for the server to process it, the client processes it on its own. This phase is called prediction. Once the server's response is received, the client will validate its predicted state, and if it's wrong (it desync) it will rewind the scene to the server state and then reprocess all the frames that are still pending. This phase is called reconciliation.
100+
101+
TODO variable sync
102+
103+
TODO sync process
104+
105+
TODO trickled
6106
</description>
7107
<tutorials>
8108
</tutorials>
@@ -235,8 +335,6 @@
235335
</method>
236336
</methods>
237337
<members>
238-
<member name="comparison_float_tolerance" type="float" setter="set_comparison_float_tolerance" getter="get_comparison_float_tolerance" default="0.001">
239-
</member>
240338
<member name="nodes_relevancy_update_time" type="float" setter="set_nodes_relevancy_update_time" getter="get_nodes_relevancy_update_time" default="0.5">
241339
</member>
242340
<member name="frame_confirmation_timespan" type="float" setter="set_frame_confirmation_timespan" getter="get_frame_confirmation_timespan" default="1.0">

godot4/gd_scene_synchronizer.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ void GdSceneSynchronizer::_bind_methods() {
5555
ClassDB::bind_method(D_METHOD("set_frame_confirmation_timespan", "interval"), &GdSceneSynchronizer::set_frame_confirmation_timespan);
5656
ClassDB::bind_method(D_METHOD("get_frame_confirmation_timespan"), &GdSceneSynchronizer::get_frame_confirmation_timespan);
5757

58-
ClassDB::bind_method(D_METHOD("set_comparison_float_tolerance", "tolerance"), &GdSceneSynchronizer::set_comparison_float_tolerance);
59-
ClassDB::bind_method(D_METHOD("get_comparison_float_tolerance"), &GdSceneSynchronizer::get_comparison_float_tolerance);
60-
6158
ClassDB::bind_method(D_METHOD("set_nodes_relevancy_update_time", "time"), &GdSceneSynchronizer::set_nodes_relevancy_update_time);
6259
ClassDB::bind_method(D_METHOD("get_nodes_relevancy_update_time"), &GdSceneSynchronizer::get_nodes_relevancy_update_time);
6360

@@ -120,7 +117,6 @@ void GdSceneSynchronizer::_bind_methods() {
120117
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "tick_acceleration", PROPERTY_HINT_RANGE, "0.1,20.0,0.01"), "set_tick_acceleration", "get_tick_acceleration");
121118

122119
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "frame_confirmation_timespan", PROPERTY_HINT_RANGE, "0.001,10.0,0.0001"), "set_frame_confirmation_timespan", "get_frame_confirmation_timespan");
123-
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "comparison_float_tolerance", PROPERTY_HINT_RANGE, "0.000001,0.01,0.000001"), "set_comparison_float_tolerance", "get_comparison_float_tolerance");
124120
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "nodes_relevancy_update_time", PROPERTY_HINT_RANGE, "0.0,2.0,0.01"), "set_nodes_relevancy_update_time", "get_nodes_relevancy_update_time");
125121

126122
ADD_SIGNAL(MethodInfo("sync_started"));
@@ -409,14 +405,6 @@ real_t GdSceneSynchronizer::get_frame_confirmation_timespan() const {
409405
return scene_synchronizer.get_frame_confirmation_timespan();
410406
}
411407

412-
void GdSceneSynchronizer::set_comparison_float_tolerance(real_t p_tolerance) {
413-
comparison_float_tolerance = p_tolerance;
414-
}
415-
416-
real_t GdSceneSynchronizer::get_comparison_float_tolerance() const {
417-
return comparison_float_tolerance;
418-
}
419-
420408
void GdSceneSynchronizer::set_nodes_relevancy_update_time(real_t p_time) {
421409
scene_synchronizer.set_objects_relevancy_update_time(p_time);
422410
}

godot4/gd_scene_synchronizer.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class GdSceneSynchronizer : public Node, public NS::SynchronizerManager {
2525
typedef NS::SceneSynchronizer<Node, GdNetworkInterface> SyncClass;
2626
SyncClass scene_synchronizer;
2727

28-
real_t comparison_float_tolerance = 0.001;
29-
3028
// Just used to detect when the low level peer change.
3129
void *low_level_peer = nullptr;
3230

@@ -56,9 +54,6 @@ class GdSceneSynchronizer : public Node, public NS::SynchronizerManager {
5654
void set_frame_confirmation_timespan(real_t p_interval);
5755
real_t get_frame_confirmation_timespan() const;
5856

59-
void set_comparison_float_tolerance(real_t p_tolerance);
60-
real_t get_comparison_float_tolerance() const;
61-
6257
void set_nodes_relevancy_update_time(real_t p_time);
6358
real_t get_nodes_relevancy_update_time() const;
6459

0 commit comments

Comments
 (0)