Skip to content

[WIP] Add Multiplayer Example #905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e3d63b7
start replacing gdscript classes with rust classes
ValorZard Sep 23, 2024
3e8e3fa
replicated most of the gameplay from the gdscript side
ValorZard Sep 23, 2024
96821ba
replace gdscript version of player with rust
ValorZard Sep 23, 2024
2931157
commiting to save progress, but theres some weird bugs cropping up no…
ValorZard Sep 24, 2024
9eb685c
redid scene manager, but still getting error regarding player desync
ValorZard Sep 24, 2024
d3ab6b6
fix networking again, did some refactors
ValorZard Sep 24, 2024
b814869
converted everything, now time to debug all this
ValorZard Sep 24, 2024
d14981c
refactor a bit while trying to hunt down bug
ValorZard Sep 24, 2024
33b7080
fixed connection bug, but now we have bigger problems
ValorZard Sep 24, 2024
6098b09
it works now i guess
ValorZard Sep 24, 2024
30258c4
fixed edge cases with making game wait
ValorZard Sep 24, 2024
be54093
attempt to turn this into an actual game, but im tired
ValorZard Sep 24, 2024
e1f74f9
revert accidental changes to dodge the creeps
ValorZard Sep 24, 2024
0d0cc6e
remove unnecessary GDScript and scenes
ValorZard Sep 24, 2024
71bab84
remove game_manager and just pass around player_database instead
ValorZard Sep 24, 2024
58c250f
remove binaries that accidentally got generated inside repo
ValorZard Sep 24, 2024
e6e7210
remove assets and replace them with dodge the creep player sprite for…
ValorZard Sep 24, 2024
2e69a8a
make all instances of NetworkId called network_id for sanity sake + a…
ValorZard Sep 24, 2024
abc1612
made server in charge of starting the game
ValorZard Sep 24, 2024
2f9098b
did some refactoring, still can't get rid of inconsistent connection bug
ValorZard Sep 24, 2024
57ea430
no more weird sync errors! but now the players have dissapeared
ValorZard Sep 24, 2024
2e67f77
add more debugging tools
ValorZard Sep 24, 2024
f67e73b
add more comments, and add callback on death
ValorZard Sep 25, 2024
9cd0859
add address bar for custom address + added basic README
ValorZard Sep 25, 2024
1f0dd45
fix ci
ValorZard Sep 25, 2024
f89291e
address review comments
ValorZard Sep 27, 2024
4116339
fix formatting besides one weird cliipy error
ValorZard Sep 27, 2024
043db66
Replace every instance of magic number 1 with MultiplayerPeer::TARGET…
ValorZard Sep 27, 2024
9679781
restarting from scratch, look at previous commit if you want to pick …
ValorZard Sep 28, 2024
a1d7b30
this is where i stop
ValorZard Sep 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ members = [
"itest/rust",
"itest/repo-tweak",
"examples/dodge-the-creeps/rust",
"examples/hot-reload/rust",
"examples/hot-reload/rust", "examples/multiplayer-lan/rust",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you forget newline

]

# Note about Jetbrains IDEs: "IDE Sync" (Refresh Cargo projects) may cause static analysis errors such as
Expand Down
2 changes: 2 additions & 0 deletions examples/multiplayer-lan/godot/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Normalize EOL for all files that Git considers text files.
* text=auto eol=lf
3 changes: 3 additions & 0 deletions examples/multiplayer-lan/godot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Godot 4+ specific ignores
.godot/
/android/
17 changes: 17 additions & 0 deletions examples/multiplayer-lan/godot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Multiplayer Lan Example

This is a sample multiplayer example showing off how to set up a lobby + dedicated server with godot-rust.



Largely adapted from FinePointCGI's Godot multiplayer tutorial:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we adapting youtube tutorial instead of one of godot's demo projects? https://github.com/godotengine/godot-demo-projects/tree/master/networking


[Basics Of Multiplayer In Godot 4! - YouTube](https://youtu.be/e0JLO_5UgQo?si=mHYry88uC8j4r2DM)



To run as dedicated server, run the exported version of this game from the command line with the arguments ``--headless --server``




Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions examples/multiplayer-lan/godot/bullet.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[gd_scene load_steps=3 format=3 uid="uid://dmnqqew4xpfhw"]

[ext_resource type="Texture2D" uid="uid://4hxff102a1as" path="res://icon.svg" id="1_wn0vy"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_e3xmc"]
size = Vector2(30, 6)

[node name="Bullet" type="Bullet"]
position = Vector2(1, -1)

[node name="Icon" type="Sprite2D" parent="."]
position = Vector2(14, 0)
scale = Vector2(0.226563, 0.0390625)
texture = ExtResource("1_wn0vy")

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(14, 0)
shape = SubResource("RectangleShape2D_e3xmc")
116 changes: 116 additions & 0 deletions examples/multiplayer-lan/godot/export_presets.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
[preset.0]

name="Windows Desktop"
platform="Windows Desktop"
runnable=true
advanced_options=false
dedicated_server=false
custom_features=""
export_filter="all_resources"
include_filter=""
exclude_filter=""
export_path="../../../../build/multiplayer Lan Tutorial.exe"
encryption_include_filters=""
encryption_exclude_filters=""
encrypt_pck=false
encrypt_directory=false
script_export_mode=2

[preset.0.options]

custom_template/debug=""
custom_template/release=""
debug/export_console_wrapper=1
binary_format/embed_pck=false
texture_format/s3tc_bptc=true
texture_format/etc2_astc=false
binary_format/architecture="x86_64"
codesign/enable=false
codesign/timestamp=true
codesign/timestamp_server_url=""
codesign/digest_algorithm=1
codesign/description=""
codesign/custom_options=PackedStringArray()
application/modify_resources=true
application/icon=""
application/console_wrapper_icon=""
application/icon_interpolation=4
application/file_version=""
application/product_version=""
application/company_name=""
application/product_name=""
application/file_description=""
application/copyright=""
application/trademarks=""
application/export_angle=0
application/export_d3d12=0
application/d3d12_agility_sdk_multiarch=true
ssh_remote_deploy/enabled=false
ssh_remote_deploy/host="user@host_ip"
ssh_remote_deploy/port="22"
ssh_remote_deploy/extra_args_ssh=""
ssh_remote_deploy/extra_args_scp=""
ssh_remote_deploy/run_script="Expand-Archive -LiteralPath '{temp_dir}\\{archive_name}' -DestinationPath '{temp_dir}'
$action = New-ScheduledTaskAction -Execute '{temp_dir}\\{exe_name}' -Argument '{cmd_args}'
$trigger = New-ScheduledTaskTrigger -Once -At 00:00
$settings = New-ScheduledTaskSettingsSet
$task = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings
Register-ScheduledTask godot_remote_debug -InputObject $task -Force:$true
Start-ScheduledTask -TaskName godot_remote_debug
while (Get-ScheduledTask -TaskName godot_remote_debug | ? State -eq running) { Start-Sleep -Milliseconds 100 }
Unregister-ScheduledTask -TaskName godot_remote_debug -Confirm:$false -ErrorAction:SilentlyContinue"
ssh_remote_deploy/cleanup_script="Stop-ScheduledTask -TaskName godot_remote_debug -ErrorAction:SilentlyContinue
Unregister-ScheduledTask -TaskName godot_remote_debug -Confirm:$false -ErrorAction:SilentlyContinue
Remove-Item -Recurse -Force '{temp_dir}'"
texture_format/bptc=true
texture_format/s3tc=true
texture_format/etc=false
texture_format/etc2=false
dotnet/include_scripts_content=false
dotnet/include_debug_symbols=true

[preset.1]

name="Linux/X11"
platform="Linux"
runnable=true
advanced_options=false
dedicated_server=false
custom_features=""
export_filter="all_resources"
include_filter=""
exclude_filter=""
export_path="build/linux/multiplayer-Lan-Tutorial.x86_64"
encryption_include_filters=""
encryption_exclude_filters=""
encrypt_pck=false
encrypt_directory=false
script_export_mode=2

[preset.1.options]

custom_template/debug=""
custom_template/release=""
debug/export_console_wrapper=1
binary_format/embed_pck=false
texture_format/s3tc_bptc=true
texture_format/etc2_astc=false
binary_format/architecture="x86_64"
ssh_remote_deploy/enabled=false
ssh_remote_deploy/host="user@host_ip"
ssh_remote_deploy/port="22"
ssh_remote_deploy/extra_args_ssh=""
ssh_remote_deploy/extra_args_scp=""
ssh_remote_deploy/run_script="#!/usr/bin/env bash
export DISPLAY=:0
unzip -o -q \"{temp_dir}/{archive_name}\" -d \"{temp_dir}\"
\"{temp_dir}/{exe_name}\" {cmd_args}"
ssh_remote_deploy/cleanup_script="#!/usr/bin/env bash
kill $(pgrep -x -f \"{temp_dir}/{exe_name} {cmd_args}\")
rm -rf \"{temp_dir}\""
texture_format/bptc=true
texture_format/s3tc=true
texture_format/etc=false
texture_format/etc2=false
dotnet/include_scripts_content=false
dotnet/include_debug_symbols=true
1 change: 1 addition & 0 deletions examples/multiplayer-lan/godot/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions examples/multiplayer-lan/godot/mulCBE9.tmp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[gd_scene load_steps=2 format=3 uid="uid://btw6ethmr2246"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this tmp file necessary to check in?


[ext_resource type="Script" path="res://MutiplayerController.gd" id="1_omkav"]

[node name="Control" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_omkav")
Address = null
port = null

[node name="Host" type="Button" parent="."]
layout_mode = 0
offset_left = 74.0
offset_top = 120.0
offset_right = 251.0
offset_bottom = 168.0
text = "Host"

[node name="Join" type="Button" parent="."]
layout_mode = 0
offset_left = 270.0
offset_top = 120.0
offset_right = 447.0
offset_bottom = 168.0
text = "Join"

[node name="StartGame" type="Button" parent="."]
layout_mode = 0
offset_left = 479.0
offset_top = 120.0
offset_right = 656.0
offset_bottom = 168.0
text = "Start Game"

[node name="LineEdit" type="LineEdit" parent="."]
layout_mode = 0
offset_left = 156.0
offset_top = 68.0
offset_right = 508.0
offset_bottom = 99.0

[node name="Label" type="Label" parent="."]
layout_mode = 0
offset_left = 75.0
offset_top = 70.0
offset_right = 122.0
offset_bottom = 96.0
text = "Name"

[connection signal="button_down" from="Host" to="." method="_on_host_button_down"]
[connection signal="button_down" from="Join" to="." method="_on_join_button_down"]
[connection signal="button_down" from="StartGame" to="." method="_on_start_game_button_down"]
78 changes: 78 additions & 0 deletions examples/multiplayer-lan/godot/multiplayerScene.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
[gd_scene load_steps=2 format=3 uid="uid://btw6ethmr2246"]

[ext_resource type="PackedScene" uid="uid://bmxv4ilh115ct" path="res://testScene.tscn" id="1_2n0a4"]

[node name="MultiplayerController" type="MultiplayerController"]
game_scene = ExtResource("1_2n0a4")
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2

[node name="HostButton" type="Button" parent="."]
layout_mode = 0
offset_left = 74.0
offset_top = 163.0
offset_right = 251.0
offset_bottom = 211.0
text = "Host"

[node name="JoinButton" type="Button" parent="."]
layout_mode = 0
offset_left = 270.0
offset_top = 163.0
offset_right = 447.0
offset_bottom = 211.0
text = "Join"

[node name="StartButton" type="Button" parent="."]
layout_mode = 0
offset_left = 479.0
offset_top = 163.0
offset_right = 656.0
offset_bottom = 211.0
text = "Start Game"

[node name="UsernameLineEdit" type="LineEdit" parent="."]
layout_mode = 0
offset_left = 156.0
offset_top = 104.0
offset_right = 508.0
offset_bottom = 135.0
text = "Player"

[node name="UsernameLabel" type="Label" parent="."]
layout_mode = 0
offset_left = 75.0
offset_top = 106.0
offset_right = 126.0
offset_bottom = 132.0
text = "Name:"

[node name="AddressLineEdit" type="LineEdit" parent="."]
layout_mode = 0
offset_left = 156.0
offset_top = 55.0
offset_right = 508.0
offset_bottom = 86.0
text = "127.0.0.1"

[node name="AddressLabel" type="Label" parent="."]
layout_mode = 0
offset_left = 75.0
offset_top = 57.0
offset_right = 142.0
offset_bottom = 83.0
text = "Address:"

[node name="MultiplayerLog" type="RichTextLabel" parent="."]
layout_mode = 0
offset_left = 66.0
offset_top = 228.0
offset_right = 705.0
offset_bottom = 468.0

[connection signal="button_down" from="HostButton" to="." method="_on_host_button_down"]
[connection signal="button_down" from="JoinButton" to="." method="_on_join_button_down"]
[connection signal="button_down" from="StartButton" to="." method="_on_start_game_button_down"]
47 changes: 47 additions & 0 deletions examples/multiplayer-lan/godot/player.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[gd_scene load_steps=6 format=3 uid="uid://dp50474qin2xu"]

[ext_resource type="Texture2D" uid="uid://60wa7yj1fufb" path="res://art/playerGrey_walk1.png" id="2_h8a1g"]
[ext_resource type="PackedScene" uid="uid://dmnqqew4xpfhw" path="res://bullet.tscn" id="2_niiqb"]

[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_f0ham"]
radius = 30.0
height = 70.0

[sub_resource type="CircleShape2D" id="CircleShape2D_paly2"]
radius = 38.71

[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_c85fd"]
properties/0/path = NodePath(".:sync_position")
properties/0/spawn = true
properties/0/replication_mode = 1
properties/1/path = NodePath(".:sync_rotation")
properties/1/spawn = true
properties/1/replication_mode = 1
properties/2/path = NodePath("GunRotation:rotation")
properties/2/spawn = true
properties/2/replication_mode = 1

[node name="Player" type="Player" groups=["Player"]]
bullet_scene = ExtResource("2_niiqb")

[node name="Sprite2D" type="Sprite2D" parent="."]
scale = Vector2(0.5, 0.5)
texture = ExtResource("2_h8a1g")

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CapsuleShape2D_f0ham")

[node name="HurtBox" type="Area2D" parent="."]

[node name="CollisionShape2D" type="CollisionShape2D" parent="HurtBox"]
shape = SubResource("CircleShape2D_paly2")

[node name="GunRotation" type="Node2D" parent="."]

[node name="BulletSpawn" type="Node2D" parent="GunRotation"]
position = Vector2(53, 0)

[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
replication_interval = 0.1
delta_interval = 0.1
replication_config = SubResource("SceneReplicationConfig_c85fd")
Loading
Loading