-
Notifications
You must be signed in to change notification settings - Fork 18
/
GdSceneSynchronizer.xml
493 lines (463 loc) · 22.4 KB
/
GdSceneSynchronizer.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GdSceneSynchronizer" inherits="Node" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
</brief_description>
<description>
The GdSceneSynchronizer synchronizes the scene.
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 complementary to the other, offering the best networking method for each situation.
- [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.
- [b]METHOD 2. The trickling:[/b] This is the best networking method to synchronize background objects or elements that doesn't immediately (or directly) affect the player's gameplay. This method is cheap on the bandwidth and CPU usage, at the cost of latency and input lag.
[b]Prerequisites[/b]
The prerequisite to use this plugin is to have the game peers connection, map loading, and the character spawning. Follow this to know how to implement it: [https://docs.godotengine.org/en/stable/tutorials/networking/high_level_multiplayer.html]
Then, you need to override the GdSceneSynchronizer and make that class a singleton. From now on we will refer to it as [i]GameSceneSync[/i] In this way, you will be able to add functionalities, specific to your game, on top of the GdSceneSynchronizer (like the object spawning).
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 character not reacting to the player's input yet.
[b]Setup the synchronization[/b]
The really first thing to do is to implement the character. Assuming your character is a scene whose first node is a [RigidBody] in kinematic mode. Open the script and search the [code]_ready()[/code] function or add it. Then add the following code:
[codeblock]
func _ready() -> void:
# Register the node as synchronized. This will create a NetId and call `_setup_synchronizer`.
GameSceneSync.register_node(self)
func _setup_synchronizer(local_id) -> void:
# This function is called by the NetworkSynchronizer to setup the node synchronization model.
# The function `setup_controller` is used to specify the functions that will control the character.
GameSceneSync.setup_controller(self, get_multiplayer_authority(), _collect_inputs, _count_input_size, _are_inputs_different, _controller_process)
# Register all the variables to keep in sync that the function `_controller_process` modifies.
GameSceneSync.register_variable(self, "velocity")
GameSceneSync.register_variable(self, "transform")
# ------------------------------------------------------------------- Networking
func _collect_inputs(delta: float, buffer: DataBuffer) -> void:
# This function is called by the NetworkSynchronizer each frame, only on the client side, to collect the player's input into the buffer.
var input_direction := Vector3()
var wants_to_jump: bool = false
if Input.is_action_pressed("forward"):
input_direction.x = 1
if Input.is_action_pressed("backward"):
input_direction.x = -1
if Input.is_action_pressed("left"):
input_direction.z = 1
if Input.is_action_pressed("right"):
input_direction.z = -1
if Input.is_action_pressed("jump"):
wants_to_jump = true
buffer.add_vector3(input_direction)
buffer.add_bool(wants_to_jump)
func _count_input_size(inputs: DataBuffer) -> int:
# This function is called by the NetworkSynchronizer to read the buffer size.
# To keep the buffer as small as possible, to save bandwidth, the buffer size is never stored.
var size = 0
size += inputs.get_vector3_size()
size += inputs.get_bool_size()
return size
func _are_inputs_different(inputs_A: DataBuffer, inputs_B: DataBuffer) -> bool:
# The NetworkSynchronizer will call this function to compare two input buffers.
if inputs_A.size != inputs_B.size:
return true
var input_direction_A = inputs_A.read_vector3()
var input_direction_B = inputs_B.read_vector3()
if input_direction_A != input_direction_B:
return true
var jump_A: bool = inputs_A.read_bool()
var jump_B: bool = inputs_B.read_bool()
if jump_A!= jump_B:
return true
return false
func _controller_process(delta: float, buffer: DataBuffer) -> void:
# This function is executed by the NetworkSynchronizer each frame, on the clients and the server, to advance the simulation.
# The buffer contains the player's input that you can read as follows.
var input_direction: Vector3 = buffer.read_vector3()
var wants_to_jump: bool = buffer.read_bool()
move_the_character(delta, input_direction, wants_to_jump)
[/codeblock]
The above code is an example of how to implement a controllable character and now the project is ready to play.
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 a few functions to collect and process the player's input.
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.
**Networking under the hood**
In this section, we will focus on what the NetworkSynchronizer does to network the simulated nodes.
The networking model used by this plugin is based on the idea of "prediction and reconciliation". You can read more about it here:
- Client side prediction server reconciliation: https://www.gabrielgambetta.com/client-side-prediction-server-reconciliation.html
- Rocket League networking: https://www.youtube.com/watch?v=ueEmiDM94IE
- Overwatch networking architecture: https://www.youtube.com/watch?v=W3aieHjyNvw
In short; The client sends all the player's input to the server, but instead of waiting for the server to process it, the client processes the input right away. This phase is called prediction, because the client is trying to predict the server replay. In this way the client will see its character moving immediately and without any input lag. Once the server's response is received, the client will validate its predicted state, and if it's wrong (a desync occurred) it will rewind the scene to the server's state and then reprocess all the frames that are still pending. This phase is called reconciliation.
Thanks to the prediction, the player's input is processed right away and that's how we can get rid of the input lag. Furthermore, thanks to the reconciliation, the client can re-sync with the server while still keeping predicting the newer frames.
This loop (prediction-validation-reconciliation) works WELL only under the strict condition that the executed code is deterministic.
**Deterministic**
With the term "deterministic code" I intend a piece of software which output guaranteed to be always the same, execution after execution, assuming that the provided input is consistent. In other words, when the character state (which may be composed by the transform, velocity, ...) is the same on both the client and the server the output frame IS guaranteed to be exactly the same on both sides.
This aspect is quite important to ensure playable gameplay. Indeed, a non deterministic code would introduce way too much desync and that would result in a game in which characters would randomly teleport back and forth, in the best case.
NOTE that the full recipe to get a deterministic game, is made of three ingredients.
1. Deterministic code.
2. Known initial state.
3. All the player's input.
The NetworkSynchronizer's code is deterministic, but that's not enough, and your code must be deterministic as well. Do not panic though, because this plugin is built in a way to encourage writing deterministic code - and however it provides some tools to debug it.
To give an example, the character controller (check the above example code) provides two distinct functions; one to read the player's input (_collect_inputs) and the other to process them (_controller_process). This setup makes it more difficult to make mistakes like fetching the inputs from the singleton [code]Input.[/code] directly from the processing function.
The second element to have a deterministic game is to ensure the initial state is known and you can do that by using the function [code]GameSceneSync.register_variable(self, "transform")[/code] to mark the variables as synchronized. The NetworkSynchronizer will track the changes to these variables and will make sure to synchronize them. All the variables modified by the process function (_controller_process) MUST be synchronized (the local variables don't need to be sync). Generally the process function modifies things like the [code]velocity[/code], the [code]transform[/code], the [code]is_on_ground[/code], the [code]ability_timer[/code], the [code]mana[/code], ecc... ANY variable modified by the process function MUST be synchronized.
The third and final element, to get a deterministic gameplay, is to send ALL the player's input to the server. To do that, it's enough to write (and also read) the player's input in to the buffer; the NetworkSynchronizer will take care to send the inputs to the server in an optimized but reliable manner.
The NetworkSynchronizer abstracts the networking so that the user can fully focus on implementing the gameplay (even the most complex one where the physics is fully synchronized).
**Trickled**
The prediction-and-reconciliation method is quite good at providing no-input-lag-synchronized gameplay, however it's heavy to process on the CPU (due to the validation and reconciliation logic) and uses quite a lot of bandwidth (due to the fact that it needs ALL the players' inputs at full precision) so network a big number of complex characters with this technology is not going to be easy - but most importantly not even required!
The NetworkSynchronizer provides an alternative way of synchronizing the objects, which is based on the concept of sending the objects' state to the users and letting the users interpolate those.
This mechanism is much slower compared to the simulation approach, it will add some delay between the time the player presses the forward button and the character moves on the other players screen, however it's much cheaper to process and it can be drastically smaller bandwidth wise (especially considering that precision and sync rate can be dynamically lowered).
This sync mechanism is easy and works in this way: The server sends to all the peers, with a fixed rate (can be changed from the settings), a packet containing the trickled objects update. The client will interpolate over the received state updates.
It's important to notice that since it's possible to change the sync rate for each object (as a way to scale the precision at cost of responsiveness) the packet may not contain the updates for all the trickled objects.
This mechanism is the best to network faraway characters, or background objects, which doesn't need great precision or real time update. You are free to choose which method to use for each object.
</description>
<tutorials>
</tutorials>
<methods>
<method name="_update_nodes_relevancy" qualifiers="virtual">
<return type="void" />
<description>
</description>
</method>
<method name="clear">
<return type="void" />
<description>
</description>
</method>
<method name="force_state_notify">
<return type="void" />
<param index="0" name="group_id" type="int" />
<description>
</description>
</method>
<method name="force_state_notify_all">
<return type="void" />
<description>
</description>
</method>
<method name="get_frames_per_seconds" qualifiers="const">
<return type="int" />
<description>
</description>
</method>
<method name="get_max_trickled_nodes_per_update" qualifiers="const">
<return type="int" />
<description>
</description>
</method>
<method name="get_node_from_id">
<return type="Node" />
<param index="0" name="id" type="int" />
<param index="1" name="expected" type="bool" default="true" />
<description>
</description>
</method>
<method name="get_node_id">
<return type="int" />
<param index="0" name="node" type="Node" />
<description>
</description>
</method>
<method name="get_peer_latency" qualifiers="const">
<return type="int" />
<param index="0" name="peer" type="int" />
<description>
</description>
</method>
<method name="get_peer_latency_jitter_ms" qualifiers="const">
<return type="int" />
<param index="0" name="peer" type="int" />
<description>
</description>
</method>
<method name="get_peer_latency_ms" qualifiers="const">
<return type="int" />
<param index="0" name="peer" type="int" />
<description>
</description>
</method>
<method name="get_peer_networking_enabled" qualifiers="const">
<return type="bool" />
<param index="0" name="peer" type="int" />
<description>
</description>
</method>
<method name="get_peer_packet_loss_percentage" qualifiers="const">
<return type="float" />
<param index="0" name="peer" type="int" />
<description>
</description>
</method>
<method name="get_variable_id">
<return type="int" />
<param index="0" name="node" type="Node" />
<param index="1" name="variable" type="StringName" />
<description>
</description>
</method>
<method name="is_client" qualifiers="const">
<return type="bool" />
<description>
</description>
</method>
<method name="is_end_sync" qualifiers="const">
<return type="bool" />
<description>
</description>
</method>
<method name="is_networked" qualifiers="const">
<return type="bool" />
<description>
</description>
</method>
<method name="is_recovered" qualifiers="const">
<return type="bool" />
<description>
</description>
</method>
<method name="is_resetted" qualifiers="const">
<return type="bool" />
<description>
</description>
</method>
<method name="is_rewinding" qualifiers="const">
<return type="bool" />
<description>
</description>
</method>
<method name="is_server" qualifiers="const">
<return type="bool" />
<description>
</description>
</method>
<method name="local_controller_get_controlled_nodes" qualifiers="const">
<return type="Array" />
<description>
</description>
</method>
<method name="register_node">
<return type="int" />
<param index="0" name="node" type="Node" />
<description>
</description>
</method>
<method name="register_process">
<return type="int" />
<param index="0" name="node" type="Node" />
<param index="1" name="phase" type="int" enum="ProcessPhase" />
<param index="2" name="function" type="Callable" />
<description>
</description>
</method>
<method name="register_variable">
<return type="void" />
<param index="0" name="node" type="Node" />
<param index="1" name="variable" type="StringName" />
<description>
</description>
</method>
<method name="reset_synchronizer_mode">
<return type="void" />
<description>
</description>
</method>
<method name="set_enabled">
<return type="void" />
<param index="0" name="enabled" type="bool" />
<description>
</description>
</method>
<method name="set_frames_per_seconds">
<return type="void" />
<param index="0" name="fps" type="int" />
<description>
</description>
</method>
<method name="set_max_trickled_nodes_per_update">
<return type="void" />
<param index="0" name="rate" type="int" />
<description>
</description>
</method>
<method name="set_peer_networking_enable">
<return type="void" />
<param index="0" name="peer" type="int" />
<param index="1" name="enabled" type="bool" />
<description>
</description>
</method>
<method name="set_skip_rewinding">
<return type="void" />
<param index="0" name="node" type="Node" />
<param index="1" name="variable" type="StringName" />
<param index="2" name="skip_rewinding" type="bool" />
<description>
</description>
</method>
<method name="setup_controller">
<return type="void" />
<param index="0" name="node" type="Node" />
<param index="1" name="peer" type="int" />
<param index="2" name="collect_input_func" type="Callable" />
<param index="3" name="count_input_size_func" type="Callable" />
<param index="4" name="are_inputs_different_func" type="Callable" />
<param index="5" name="proces_func" type="Callable" />
<description>
</description>
</method>
<method name="setup_trickled_sync">
<return type="void" />
<param index="0" name="node" type="Node" />
<param index="1" name="collect_epoch_func" type="Callable" />
<param index="2" name="apply_epoch_func" type="Callable" />
<description>
</description>
</method>
<method name="sync_group_add_node">
<return type="void" />
<param index="0" name="node_id" type="int" />
<param index="1" name="group_id" type="int" />
<param index="2" name="realtime" type="bool" />
<description>
</description>
</method>
<method name="sync_group_create">
<return type="int" />
<description>
</description>
</method>
<method name="sync_group_get_trickled_update_rate" qualifiers="const">
<return type="float" />
<param index="0" name="node_id" type="int" />
<param index="1" name="group_id" type="int" />
<description>
</description>
</method>
<method name="sync_group_move_peer_to">
<return type="void" />
<param index="0" name="peer_id" type="int" />
<param index="1" name="group_id" type="int" />
<description>
</description>
</method>
<method name="sync_group_remove_node">
<return type="void" />
<param index="0" name="node_id" type="int" />
<param index="1" name="group_id" type="int" />
<description>
</description>
</method>
<method name="sync_group_set_trickled_update_rate">
<return type="void" />
<param index="0" name="node_id" type="int" />
<param index="1" name="group_id" type="int" />
<param index="2" name="update_rate" type="float" />
<description>
</description>
</method>
<method name="track_variable_changes">
<return type="int" />
<param index="0" name="nodes" type="Array" />
<param index="1" name="variables" type="Array" />
<param index="2" name="callable" type="Callable" />
<param index="3" name="flags" type="int" enum="NetEventFlag" default="17" />
<description>
</description>
</method>
<method name="unregister_node">
<return type="void" />
<param index="0" name="node" type="Node" />
<description>
</description>
</method>
<method name="unregister_process">
<return type="void" />
<param index="0" name="node" type="Node" />
<param index="1" name="phase" type="int" enum="ProcessPhase" />
<param index="2" name="function" type="int" />
<description>
</description>
</method>
<method name="unregister_variable">
<return type="void" />
<param index="0" name="node" type="Node" />
<param index="1" name="variable" type="StringName" />
<description>
</description>
</method>
<method name="untrack_variable_changes">
<return type="void" />
<param index="0" name="handle" type="int" />
<description>
</description>
</method>
</methods>
<members>
<member name="frame_confirmation_timespan" type="float" setter="set_frame_confirmation_timespan" getter="get_frame_confirmation_timespan" default="1.0">
</member>
<member name="max_fps_acceleration_percentage" type="float" setter="set_max_fps_acceleration_percentage" getter="get_max_fps_acceleration_percentage" default="0.2">
</member>
<member name="netstats_update_interval_sec" type="float" setter="set_netstats_update_interval_sec" getter="get_netstats_update_interval_sec" default="0.6">
</member>
<member name="nodes_relevancy_update_time" type="float" setter="set_nodes_relevancy_update_time" getter="get_nodes_relevancy_update_time" default="0.5">
</member>
</members>
<signals>
<signal name="desync_detected">
<param index="0" name="input_id" type="int" />
<param index="1" name="node" type="Object" />
<param index="2" name="var_names" type="Array" />
<param index="3" name="client_values" type="Array" />
<param index="4" name="server_values" type="Array" />
<description>
</description>
</signal>
<signal name="peer_status_updated">
<param index="0" name="peer" type="int" />
<param index="1" name="connected" type="bool" />
<param index="2" name="enabled" type="bool" />
<description>
</description>
</signal>
<signal name="rewind_frame_begin">
<param index="0" name="input_id" type="int" />
<param index="1" name="index" type="int" />
<param index="2" name="count" type="int" />
<description>
</description>
</signal>
<signal name="state_validated">
<param index="0" name="input_id" type="int" />
<param index="1" name="desync_detected" type="bool" />
<description>
</description>
</signal>
<signal name="sync_paused">
<description>
</description>
</signal>
<signal name="sync_started">
<description>
</description>
</signal>
</signals>
<constants>
<constant name="GLOBAL_SYNC_GROUP_ID" value="0">
</constant>
<constant name="CHANGE" value="1" enum="NetEventFlag">
</constant>
<constant name="SYNC_RECOVER" value="2" enum="NetEventFlag">
</constant>
<constant name="SYNC_RESET" value="4" enum="NetEventFlag">
</constant>
<constant name="SYNC_REWIND" value="8" enum="NetEventFlag">
</constant>
<constant name="END_SYNC" value="16" enum="NetEventFlag">
</constant>
<constant name="DEFAULT" value="17" enum="NetEventFlag">
</constant>
<constant name="SYNC" value="14" enum="NetEventFlag">
</constant>
<constant name="ALWAYS" value="31" enum="NetEventFlag">
</constant>
<constant name="PROCESS_PHASE_EARLY" value="0" enum="ProcessPhase">
</constant>
<constant name="PROCESS_PHASE_PRE" value="1" enum="ProcessPhase">
</constant>
<constant name="PROCESS_PHASE_PROCESS" value="2" enum="ProcessPhase">
</constant>
<constant name="PROCESS_PHASE_POST" value="3" enum="ProcessPhase">
</constant>
<constant name="PROCESS_PHASE_LATE" value="4" enum="ProcessPhase">
</constant>
</constants>
</class>