|
3 | 3 | <brief_description> |
4 | 4 | </brief_description> |
5 | 5 | <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 |
6 | 106 | </description> |
7 | 107 | <tutorials> |
8 | 108 | </tutorials> |
|
235 | 335 | </method> |
236 | 336 | </methods> |
237 | 337 | <members> |
238 | | - <member name="comparison_float_tolerance" type="float" setter="set_comparison_float_tolerance" getter="get_comparison_float_tolerance" default="0.001"> |
239 | | - </member> |
240 | 338 | <member name="nodes_relevancy_update_time" type="float" setter="set_nodes_relevancy_update_time" getter="get_nodes_relevancy_update_time" default="0.5"> |
241 | 339 | </member> |
242 | 340 | <member name="frame_confirmation_timespan" type="float" setter="set_frame_confirmation_timespan" getter="get_frame_confirmation_timespan" default="1.0"> |
|
0 commit comments