Skip to content

Commit 0cff177

Browse files
Updated Multiplayer Pong C# to Godot 4.2 (#1045)
1 parent 05aeb11 commit 0cff177

File tree

8 files changed

+85
-80
lines changed

8 files changed

+85
-80
lines changed
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
<Project Sdk="Godot.NET.Sdk/4.0.0-dev5">
1+
<Project Sdk="Godot.NET.Sdk/4.2.0">
22
<PropertyGroup>
3-
<TargetFramework>net472</TargetFramework>
4-
<RootNamespace>PongMultiplayer</RootNamespace>
3+
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net7.0</TargetFramework>
5+
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'ios' ">net8.0</TargetFramework>
6+
<EnableDynamicLoading>true</EnableDynamicLoading>
7+
<RootNamespace>PongMultiplayerwithC</RootNamespace>
58
</PropertyGroup>
69
</Project>
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio 2012
3-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pong Multiplayer with C#", "Pong Multiplayer with C#.csproj", "{4BB6C2D0-FC11-466E-8C73-8DAD689F135A}"
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pong Multiplayer with C#", "Pong Multiplayer with C#.csproj", "{58FECEC2-0618-4042-81F2-4EDD7982C229}"
44
EndProject
55
Global
66
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -9,11 +9,11 @@ Global
99
ExportRelease|Any CPU = ExportRelease|Any CPU
1010
EndGlobalSection
1111
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12-
{4BB6C2D0-FC11-466E-8C73-8DAD689F135A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13-
{4BB6C2D0-FC11-466E-8C73-8DAD689F135A}.Debug|Any CPU.Build.0 = Debug|Any CPU
14-
{4BB6C2D0-FC11-466E-8C73-8DAD689F135A}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU
15-
{4BB6C2D0-FC11-466E-8C73-8DAD689F135A}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU
16-
{4BB6C2D0-FC11-466E-8C73-8DAD689F135A}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU
17-
{4BB6C2D0-FC11-466E-8C73-8DAD689F135A}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU
12+
{58FECEC2-0618-4042-81F2-4EDD7982C229}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13+
{58FECEC2-0618-4042-81F2-4EDD7982C229}.Debug|Any CPU.Build.0 = Debug|Any CPU
14+
{58FECEC2-0618-4042-81F2-4EDD7982C229}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU
15+
{58FECEC2-0618-4042-81F2-4EDD7982C229}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU
16+
{58FECEC2-0618-4042-81F2-4EDD7982C229}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU
17+
{58FECEC2-0618-4042-81F2-4EDD7982C229}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU
1818
EndGlobalSection
1919
EndGlobal

mono/multiplayer_pong/lobby.tscn

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
[gd_scene load_steps=2 format=2]
1+
[gd_scene load_steps=2 format=3 uid="uid://bxj0km4t28u17"]
22

3-
[ext_resource path="res://logic/Lobby.cs" type="Script" id=1]
3+
[ext_resource type="Script" path="res://logic/Lobby.cs" id="1"]
44

55
[node name="Lobby" type="Control"]
6+
layout_mode = 3
7+
anchors_preset = 8
68
anchor_left = 0.5
79
anchor_top = 0.5
810
anchor_right = 0.5
@@ -11,36 +13,33 @@ offset_left = -320.0
1113
offset_top = -200.0
1214
offset_right = 320.0
1315
offset_bottom = 200.0
16+
grow_horizontal = 2
17+
grow_vertical = 2
1418
size_flags_horizontal = 2
1519
size_flags_vertical = 2
16-
__meta__ = {
17-
"_edit_use_anchors_": false
18-
}
1920

2021
[node name="Title" type="Label" parent="."]
22+
layout_mode = 0
2123
offset_left = 210.0
2224
offset_top = 40.0
2325
offset_right = 430.0
2426
offset_bottom = 80.0
2527
size_flags_horizontal = 2
2628
size_flags_vertical = 0
2729
text = "Multiplayer Pong"
28-
align = 1
29-
valign = 1
3030

3131
[node name="LobbyPanel" type="Panel" parent="."]
32+
layout_mode = 0
3233
offset_left = 210.0
3334
offset_top = 160.0
3435
offset_right = 430.0
3536
offset_bottom = 270.0
3637
size_flags_horizontal = 2
3738
size_flags_vertical = 2
38-
script = ExtResource( 1 )
39-
__meta__ = {
40-
"_edit_use_anchors_": false
41-
}
39+
script = ExtResource("1")
4240

4341
[node name="AddressLabel" type="Label" parent="LobbyPanel"]
42+
layout_mode = 0
4443
offset_left = 10.0
4544
offset_top = 10.0
4645
offset_right = 62.0
@@ -50,6 +49,7 @@ size_flags_vertical = 0
5049
text = "Address"
5150

5251
[node name="Address" type="LineEdit" parent="LobbyPanel"]
52+
layout_mode = 0
5353
offset_left = 10.0
5454
offset_top = 30.0
5555
offset_right = 210.0
@@ -59,6 +59,7 @@ size_flags_vertical = 2
5959
text = "127.0.0.1"
6060

6161
[node name="HostButton" type="Button" parent="LobbyPanel"]
62+
layout_mode = 0
6263
offset_left = 10.0
6364
offset_top = 60.0
6465
offset_right = 90.0
@@ -68,36 +69,32 @@ size_flags_vertical = 2
6869
text = "Host"
6970

7071
[node name="JoinButton" type="Button" parent="LobbyPanel"]
72+
layout_mode = 0
7173
offset_left = 130.0
7274
offset_top = 60.0
7375
offset_right = 210.0
7476
offset_bottom = 80.0
7577
size_flags_horizontal = 2
7678
size_flags_vertical = 2
7779
text = "Join"
78-
__meta__ = {
79-
"_edit_use_anchors_": false
80-
}
8180

8281
[node name="StatusOk" type="Label" parent="LobbyPanel"]
82+
layout_mode = 0
8383
offset_left = 10.0
8484
offset_top = 90.0
8585
offset_right = 210.0
8686
offset_bottom = 104.0
8787
size_flags_horizontal = 2
8888
size_flags_vertical = 0
89-
custom_colors/font_color = Color(0, 1, 0.015625, 1)
90-
align = 1
9189

9290
[node name="StatusFail" type="Label" parent="LobbyPanel"]
91+
layout_mode = 0
9392
offset_left = 10.0
9493
offset_top = 90.0
9594
offset_right = 210.0
9695
offset_bottom = 104.0
9796
size_flags_horizontal = 2
9897
size_flags_vertical = 0
99-
custom_colors/font_color = Color(1, 0, 0, 1)
100-
align = 1
10198

10299
[connection signal="pressed" from="LobbyPanel/HostButton" to="LobbyPanel" method="OnHostPressed"]
103100
[connection signal="pressed" from="LobbyPanel/JoinButton" to="LobbyPanel" method="OnJoinPressed"]

mono/multiplayer_pong/logic/Ball.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public partial class Ball : Area2D
77

88
private Vector2 _direction = Vector2.Left;
99
private bool _stopped = false;
10-
private float _speed = DefaultSpeed;
10+
private double _speed = DefaultSpeed;
1111
private Vector2 _screenSize;
1212

1313
// Called when the node enters the scene tree for the first time.
@@ -17,44 +17,44 @@ public override void _Ready()
1717
}
1818

1919
// Called every frame. 'delta' is the elapsed time since the previous frame.
20-
public override void _Process(float delta)
20+
public override void _Process(double delta)
2121
{
2222
_speed += delta;
2323
// Ball will move normally for both players,
2424
// even if it's slightly out of sync between them,
2525
// so each player sees the motion as smooth and not jerky.
2626
if (!_stopped)
2727
{
28-
Translate(_speed * delta * _direction);
28+
Translate((float)(_speed * delta) * _direction);
2929
}
3030

3131
// Check screen bounds to make ball bounce.
3232
var ballPosition = Position;
33-
if ((ballPosition.y < 0 && _direction.y < 0) || (ballPosition.y > _screenSize.y && _direction.y > 0))
33+
if ((ballPosition.Y < 0 && _direction.Y < 0) || (ballPosition.Y > _screenSize.Y && _direction.Y > 0))
3434
{
35-
_direction.y = -_direction.y;
35+
_direction.Y = -_direction.Y;
3636
}
3737

38-
if (IsNetworkMaster())
38+
if (IsMultiplayerAuthority())
3939
{
40-
// Only the master will decide when the ball is out in
41-
// the left side (it's own side). This makes the game
42-
// playable even if latency is high and ball is going
43-
// fast. Otherwise ball might be out in the other
40+
// Only the server (Multiplayer Authority) will decide
41+
// when the ball is out in the left side (it's own side).
42+
// This makes the game playable even if latency is high and
43+
// ball is going fast. Otherwise ball might be out in the other
4444
// player's screen but not this one.
45-
if (ballPosition.x < 0)
45+
if (ballPosition.X < 0)
4646
{
4747
GetParent().Rpc("UpdateScore", false);
4848
Rpc("ResetBall", false);
4949
}
5050
else
5151
{
52-
// Only the puppet will decide when the ball is out in
52+
// Only the peer will decide when the ball is out in
5353
// the right side, which is it's own side. This makes
5454
// the game playable even if latency is high and ball
5555
// is going fast. Otherwise ball might be out in the
5656
// other player's screen but not this one.
57-
if (ballPosition.x > _screenSize.x)
57+
if (ballPosition.X > _screenSize.X)
5858
{
5959
GetParent().Rpc("UpdateScore", true);
6060
Rpc("ResetBall", true);
@@ -63,31 +63,31 @@ public override void _Process(float delta)
6363
}
6464
}
6565

66-
[Sync]
66+
[Rpc(mode: MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
6767
private void Bounce(bool left, float random)
6868
{
6969
// Using sync because both players can make it bounce.
7070
if (left)
7171
{
72-
_direction.x = Mathf.Abs(_direction.x);
72+
_direction.X = Mathf.Abs(_direction.X);
7373
}
7474
else
7575
{
76-
_direction.x = -Mathf.Abs(_direction.x);
76+
_direction.X = -Mathf.Abs(_direction.X);
7777
}
7878

7979
_speed *= 1.1f;
80-
_direction.y = random * 2.0f - 1;
80+
_direction.Y = random * 2.0f - 1;
8181
_direction = _direction.Normalized();
8282
}
8383

84-
[Sync]
84+
[Rpc(mode: MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
8585
private void Stop()
8686
{
8787
_stopped = true;
8888
}
8989

90-
[Sync]
90+
[Rpc(mode: MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
9191
private void ResetBall(bool forLeft)
9292
{
9393
Position = _screenSize / 2;

mono/multiplayer_pong/logic/Lobby.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public partial class Lobby : Control
1212
private Button _joinButton;
1313
private Label _statusOk;
1414
private Label _statusFail;
15-
private NetworkedMultiplayerENet _peer;
15+
private ENetMultiplayerPeer _peer;
1616

1717
public override void _Ready()
1818
{
@@ -25,11 +25,11 @@ public override void _Ready()
2525

2626
// Connect all callbacks related to networking.
2727
// Note: Use snake_case when talking to engine API.
28-
GetTree().Connect("network_peer_connected", this, nameof(PlayerConnected));
29-
GetTree().Connect("network_peer_disconnected", this, nameof(PlayerDisconnected));
30-
GetTree().Connect("connected_to_server", this, nameof(ConnectedOk));
31-
GetTree().Connect("connection_failed", this, nameof(ConnectedFail));
32-
GetTree().Connect("server_disconnected", this, nameof(ServerDisconnected));
28+
GetTree().GetMultiplayer().Connect("peer_connected", new Callable(this, nameof(PlayerConnected)));
29+
GetTree().GetMultiplayer().Connect("peer_disconnected", new Callable(this, nameof(PlayerDisconnected)));
30+
GetTree().GetMultiplayer().Connect("connected_to_server", new Callable(this, nameof(ConnectedOk)));
31+
GetTree().GetMultiplayer().Connect("connection_failed", new Callable(this, nameof(ConnectedFail)));
32+
GetTree().GetMultiplayer().Connect("server_disconnected", new Callable(this, nameof(ServerDisconnected)));
3333
}
3434

3535
// Network callbacks from SceneTree
@@ -41,15 +41,15 @@ private void PlayerConnected(int id)
4141
var pong = ResourceLoader.Load<PackedScene>("res://pong.tscn").Instantiate();
4242

4343
// Connect deferred so we can safely erase it from the callback.
44-
pong.Connect("GameFinished", this, nameof(EndGame), new Godot.Collections.Array(), (int) ConnectFlags.Deferred);
44+
pong.Connect("GameFinished", new Callable(this, nameof(EndGame)), (int) ConnectFlags.Deferred);
4545

4646
GetTree().Root.AddChild(pong);
4747
Hide();
4848
}
4949

5050
private void PlayerDisconnected(int id)
5151
{
52-
EndGame(GetTree().IsNetworkServer() ? "Client disconnected" : "Server disconnected");
52+
EndGame(GetTree().GetMultiplayer().IsServer() ? "Client disconnected" : "Server disconnected");
5353
}
5454

5555
// Callback from SceneTree, only for clients (not server).
@@ -63,7 +63,7 @@ private void ConnectedFail()
6363
{
6464
SetStatus("Couldn't connect", false);
6565

66-
GetTree().NetworkPeer = null; // Remove peer.
66+
GetTree().GetMultiplayer().MultiplayerPeer = null; // Remove peer.
6767
_hostButton.Disabled = false;
6868
_joinButton.Disabled = false;
6969
}
@@ -85,7 +85,7 @@ private void EndGame(string withError = "")
8585
Show();
8686
}
8787

88-
GetTree().NetworkPeer = null; // Remove peer.
88+
GetTree().GetMultiplayer().MultiplayerPeer = null; // Remove peer.
8989
_hostButton.Disabled = false;
9090
_joinButton.Disabled = false;
9191

@@ -109,8 +109,7 @@ private void SetStatus(string text, bool isOk)
109109

110110
private void OnHostPressed()
111111
{
112-
_peer = new NetworkedMultiplayerENet();
113-
_peer.CompressionMode = NetworkedMultiplayerENet.CompressionModeEnum.RangeCoder;
112+
_peer = new ENetMultiplayerPeer();
114113
Error err = _peer.CreateServer(DefaultPort, MaxNumberOfPeers);
115114
if (err != Error.Ok)
116115
{
@@ -119,7 +118,9 @@ private void OnHostPressed()
119118
return;
120119
}
121120

122-
GetTree().NetworkPeer = _peer;
121+
_peer.Host.Compress(ENetConnection.CompressionMode.RangeCoder);
122+
123+
GetTree().GetMultiplayer().MultiplayerPeer = _peer;
123124
_hostButton.Disabled = true;
124125
_joinButton.Disabled = true;
125126
SetStatus("Waiting for player...", true);
@@ -134,10 +135,11 @@ private void OnJoinPressed()
134135
return;
135136
}
136137

137-
_peer = new NetworkedMultiplayerENet();
138-
_peer.CompressionMode = NetworkedMultiplayerENet.CompressionModeEnum.RangeCoder;
138+
_peer = new ENetMultiplayerPeer();
139139
_peer.CreateClient(ip, DefaultPort);
140-
GetTree().NetworkPeer = _peer;
140+
_peer.Host.Compress(ENetConnection.CompressionMode.RangeCoder);
141+
142+
GetTree().GetMultiplayer().MultiplayerPeer = _peer;
141143
SetStatus("Connecting...", true);
142144
}
143145
}

0 commit comments

Comments
 (0)