Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC
Windows 11 v26058 C Sharp dot net 8.0.100 with Copilot Godot Engine 4.2.1 multiplayer client server POC
Who am i / About the author...
since 2001, Insider since 2014.
Thanks to Microsoft EMEA GBS to let me participate in the year 2014 to the Insider project during my work. Then, This Windows test project, here in version 11, is based on the cycle of the insider Preview Builds, now in the Dev Channel: it is fun to test the new features. I can learn new features, validate them and share them with you :-)
This tutorial is made in a test environment, for pleasure and not to earn money. It is a non-profit project. It shows that the concept is possible, for a demontration, artistic, or educational purposes, school learning course. Don't test in a real environment in production.
Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC : application testing
Now, OS Windows 11 version 26058 is installed and ready, so i can test the middleware apps, frameworks...
Recently, i read a cool article by Moriss Morisson here (and thank you Moriss for the inspiration for this multiplayer client server POC).
Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC : last articles
Read this article to see how to install Godot engine
How to use signals with Godo Engine (Red bouncing ball)
A Paddle Game with Godot engine
Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC : new project
By default, mobile is a good choice
I clic Create & Edit
I save this scene multiplayer.tscn
Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC : i ask Windows Copilot
I Ask my Windows copilot with this prompt:
« how to implement a multiplayer scene in Godot 4 »
Copilot
Implementing a multiplayer scene in Godot 4 involves several steps, including setting up the scene, synchronizing properties, and handling player spawning and despawning. Here’s a high-level overview of the process:
- Scene Setup: Begin by creating your multiplayer scene and connecting signals to start a server or client.
- Synchronizing Properties: Use the
MultiplayerSynchronizer
node to configure which properties of your objects will be synchronized across the network. - Spawning and Despawning: The
MultiplayerSpawner
node helps automate the replication of dynamically instantiated nodes across peers, even when joining mid-game or reconnecting. - Networking: Initialize the
SceneMultiplayer
and set up network peers. Define Remote Procedure Calls (RPCs) to allow function calls across the network. - Security and Optimization: Ensure your game’s security by controlling which scenes can be spawned by remote peers and optimize your network code for better performance.
For a detailed guide, you can refer to the Godot Engine’s official documentation on multiplayer in Godot 4.0: Scene Replication, which provides a comprehensive tutorial on setting up a multiplayer scene with the new features in Godot 4.0.
Thank you Windows Copilot ! It’s on preview mode, but awesome ! So copilote gave me thoses links and i go to read that :
-https://godotengine.org/article/multiplayer-in-godot-4-0-scene-replication/
Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC : new control node
Now, i can do a little test :
In the Godot user Interface, i clic +other node, i select type : control
Create
And i rename it « Multiplayer » instead of « Control »
Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC : attach script
I right click on the Multiplayer node and i select « attach Script »
I select Csharp script : C# and call it MultiplayerController.cs and hit Create.
Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC : trust the authors
Visual Studio ask me if i trust the authors, i clic « Yes, i trust the authors » because i’m the author :)
Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC : solution explorer
The script opens:
usingGodot;
usingSystem;
publicpartialclassMultiplayerController : Control
{
// Called when the node enters the scene tree for the first time.
publicoverridevoid_Ready()
{
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
publicoverridevoid_Process(doubledelta)
{
}
}
I create a new Csharp Class in the solution exporer : right clic and new file
And name it GDPrint.cs , containing this wrapper code :
usingSystem;
usingGodot;
publicstaticclassGDPrint
{
publicstaticstringGetTimestamp()
{
returnDateTime.Now.ToString("dd.MM.yyyy-HH:mm:ss");
}
publicstaticvoidPrint(stringmessage)
{
GD.Print($"{GetTimestamp()}: {message}");
}
publicstaticvoidPrint(intid, stringmessage)
{
GD.Print($"{GetTimestamp()} - <{id}>: {message}");
}
publicstaticvoidPrintErr(stringmessage)
{
GD.PrintErr($"{GetTimestamp()}: {message}");
}
publicstaticvoidPrintErr(intid, stringmessage)
{
GD.PrintErr($"{GetTimestamp()} - <{id}>: {message}");
}
}
I create 3 new variables _port, _ip and maxPlayerCount and initialise them:
publicpartialclassMultiplayerController : Control
{
[Export]
privateint_port=8910;
[Export]
privatestring_ip="127.0.0.1";
[Export]
privateint_maxPlayerCount=2;
[Export] means that the variable can be seen in the User Interface editor so that we can edit them with the inspector.
I create an ENetMultiplayerPeer variable type named _peer : it the network object reference
I create the new method Hostgame() as i saw in the documentation, that will be the multiplayer game server with all the network logic. It will instance the peer server and listen for the client connections.
privatevoidHostGame()
{
_peer=newENetMultiplayerPeer();
varstatus=_peer.CreateServer(_port, _maxPlayerCount);
if (status!=Error.Ok)
{
GDPrint.PrintErr("Server could not be created:");
GDPrint.PrintErr($"Port: {_port}");
return;
}
_peer.Host.Compress(ENetConnection.CompressionMode.RangeCoder);
Multiplayer.MultiplayerPeer=_peer;
GDPrint.Print("Server started SUCCESSFULLY.");
GDPrint.Print("Waiting for players to connect ...");
}
I create the method PlayerConnected()
privatevoidPlayerConnected(longid)
{
GDPrint.Print($"Player <{id}> connected.");
}
I create the method PlayerDisconnected()
privatevoidPlayerDisconnected(longid)
{
GDPrint.Print($"Player <{id}> disconected.");
}
Those methods will be fired with custom signals/events on the peers connected to the multiplayer server.
If you you’re not too friendly with events, signals, read this documentation :
https://docs.godotengine.org/en/stable/tutorials/scripting/c_sharp/c_sharp_signals.html
In the « override void _Ready() » i call the events (signals) and the HostGame() method like this :
publicoverridevoid_Ready()
{
GDPrint.Print("<<< START SERVER >>>");
Multiplayer.PeerConnected+=PlayerConnected;
Multiplayer.PeerDisconnected+=PlayerDisconnected;
HostGame();
this.Hide();
}
Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC : server test
I lauch the scene by hitting the F5 or « play » button
I clic « Select Current »
Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC : firewall
Because i’m launching network communications, the Windows 11 firewall blocked the port. I must clic on « Allow » to make the ports opened.
And i get… a blank screen ! Except few lines in the console :
Godot Engine v4.2.1.stable.mono.official.b09f793f5 - https://godotengine.org
Vulkan API 1.3.242 - Forward Mobile - Using Vulkan Device #0: NVIDIA - Quadro M620
11.03.2024-23:35:01: <<< START SERVER >>>
11.03.2024-23:35:01: Server started SUCCESSFULLY.
11.03.2024-23:35:01: Waiting for players to connect ...
Nice ! In fact, there is nothing to see, because the server is waiting for the clients to connect.
I stop the scene by hitting F8 or the « stop » button.
--- Debugging process stopped ---
Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC : the client part
I launch another instance of the Godot editor
I create a new project, with the same logic with the main scene with a MultiplayerController type control and containing a join button :
Inspector/Text : Join
I create the « Join » button on the User Interface
I save it to multiplayer_controller_client.tscn
I created the new private variable _playerId to store the playerId once a client successfully creates a connection to the server
The code looks like that :
usingGodot;
usingSystem;
publicpartialclassMultiplayerController : Control
{
[Export]
privateint_port=8910;
[Export]
privatestring_ip="127.0.0.1";
privateENetMultiplayerPeer_peer;
privateint_playerId;
privatevoidPlayerConnected(longid)
{
GDPrint.Print(_playerId, $"Player <{id}> connected.");
}
privatevoidPlayerDisconnected(longid)
{
GDPrint.Print(_playerId, $"Player <${id}> disconnected.");
}
// Called when the node enters the scene tree for the first time.
publicoverridevoid_Ready()
{
Multiplayer.PeerConnected+=PlayerConnected;
Multiplayer.PeerDisconnected+=PlayerDisconnected;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
publicoverridevoid_Process(doubledelta)
{
}
}
I add the two others methods ConnectionSuccessful() and ConnectionFailed() . As same that last time, with events handlers . The code became something like that :
privatevoidConnectionFailed()
{
GDPrint.Print("Connection FAILED.");
GDPrint.Print("Could not connect to server.");
}
privatevoidConnectionSuccessful()
{
GDPrint.Print("Connection SUCCESSFULL.");
_playerId=Multiplayer.GetUniqueId();
GDPrint.Print(_playerId, "Sending player information to server.");
GDPrint.Print(_playerId, $"Id: {_playerId}");
RpcId(1, "SendPlayerInformation", _playerId);
}
I add the ConnectToServer() method :
publicvoidConnectToServer()
{
_peer=newENetMultiplayerPeer();
varstatus=_peer.CreateClient(_ip, _port);
if (status!=Error.Ok)
{
GDPrint.PrintErr("Creating client FAILED.");
return;
}
}
I add the OnJoinPressed() method connected to the Join button in my Godot UI. This button will trigger the connection to the server
publicvoidOnJoinPressed()
{
GDPrint.PrintErr("Connect To Server...");
ConnectToServer();
}
I add the compression type RangeCoder in the ConnectToServer() method :
publicvoidConnectToServer()
{
_peer=newENetMultiplayerPeer();
varstatus=_peer.CreateClient(_ip, _port);
if (status!=Error.Ok)
{
GDPrint.PrintErr("Creating client FAILED.");
return;
}
_peer.Host.Compress(ENetConnection.CompressionMode.RangeCoder);
Multiplayer.MultiplayerPeer=_peer;
}
Testing the Server to Client to Server mode…
I launch both instances, Client and Server
First, i launch Server, by hitting F5 and play button:
Godot Engine v4.2.1.stable.mono.official.b09f793f5 - https://godotengine.org
Vulkan API 1.3.242 - Forward Mobile - Using Vulkan Device #0: NVIDIA - Quadro M620
12.03.2024-03:01:57: <<< START SERVER >>>
12.03.2024-03:01:57: Server started SUCCESSFULLY.
12.03.2024-03:01:57: Waiting for players to connect ...
Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC : join the session
Now i lauch the client
Godot Engine v4.2.1.stable.mono.official.b09f793f5 - https://godotengine.org
Vulkan API 1.3.242 - Forward Mobile - Using Vulkan Device #0: NVIDIA - Quadro M620
12.03.2024-03:08:08: Connect To Server...
12.03.2024-03:08:08 - <0>: Player <1> connected.
The client is connected.
And on the server console, i see :
12.03.2024-03:08:08: Player <1079064274> connected.
Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC : conclusion done
Done! We made a networking game client server POC with C# in Godot 4.2.1 on windows 11 version 26058.
You see here the client connecting to the server when i click the "join" button:
Server:
12.03.2024-18:26:39: <<< START SERVER >>>
12.03.2024-18:26:39: Server started SUCCESSFULLY.
12.03.2024-18:26:39: Waiting for players to connect ...
12.03.2024-18:33:58: Player <1026985424> connected.
Client:
12.03.2024-18:33:58: Connect To Server...
12.03.2024-18:33:58 - <0>: Player <1> connected.
Windows 11 v26058 C Sharp with Copilot Godot Engine multiplayer client server POC : need help ?
Need help to write C# code or use godot engine on Windows 11 ?
Fill this form or send an email on loic @consultingit.fr or Esta dirección de correo electrónico está siendo protegida contra los robots de spam. Necesita tener JavaScript habilitado para poder verlo. and i will be happy to help you :)