Skip to content

Commit

Permalink
add address bar for custom address + added basic README
Browse files Browse the repository at this point in the history
  • Loading branch information
ValorZard committed Sep 25, 2024
1 parent f67e73b commit 9cd0859
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 29 deletions.
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:

[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``




2 changes: 1 addition & 1 deletion examples/multiplayer-lan/godot/export_presets.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ custom_features=""
export_filter="all_resources"
include_filter=""
exclude_filter=""
export_path="build/multiplayer Lan Tutorial.exe"
export_path="../../../../build/multiplayer Lan Tutorial.exe"
encryption_include_filters=""
encryption_exclude_filters=""
encrypt_pck=false
Expand Down
46 changes: 31 additions & 15 deletions examples/multiplayer-lan/godot/multiplayerScene.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,65 @@ grow_vertical = 2
[node name="HostButton" type="Button" parent="."]
layout_mode = 0
offset_left = 74.0
offset_top = 120.0
offset_top = 163.0
offset_right = 251.0
offset_bottom = 168.0
offset_bottom = 211.0
text = "Host"

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

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

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

[node name="Label" type="Label" parent="."]
[node name="UsernameLabel" type="Label" parent="."]
layout_mode = 0
offset_left = 75.0
offset_top = 70.0
offset_right = 122.0
offset_bottom = 96.0
text = "Name"
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 = 185.0
offset_top = 228.0
offset_right = 705.0
offset_bottom = 425.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"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ impl MultiplayerController {
.get_node_as::<Button>("HostButton")
.set_visible(false);
let mut peer = ENetMultiplayerPeer::new_gd();
self.address = self.base().get_node_as::<LineEdit>("AddressLineEdit").get_text();
let error = peer.create_client(self.address.clone(), self.port);
if error != Error::OK {
godot_print!("cannot join");
Expand Down
24 changes: 11 additions & 13 deletions examples/multiplayer-lan/rust/src/scene_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ impl SceneManager {
binding.set_username(username);
}

let callable = Callable::from_fn("on_death", |args: &[&Variant]| {
let network_id = args[0].try_to::<NetworkId>().unwrap();
godot_print!("player {network_id} has died, respawning");
Ok(Variant::nil())
});

player.connect(
"death".into(),
callable,
);

self.player_list.insert(network_id, player.clone());
self.base_mut().add_child(player.clone());
}
Expand Down Expand Up @@ -77,19 +88,6 @@ impl SceneManager {
godot_print!("spawn player id {0} position {1}", network_id , player.get_global_position());
}

let callable = Callable::from_fn("on_death", |args: &[&Variant]| {
let network_id = args[0].try_to::<NetworkId>().unwrap();
godot_print!("player {network_id} has died, respawning");
// only server can respawn player
//self.base_mut().rpc("respawn_player".into(), &[Variant::from(spawn_position), Variant::from(network_id)]);
Ok(Variant::nil())
});

player.connect(
"death".into(),
callable,
);

index += 1;

if index >= spawn_points.len() {
Expand Down

0 comments on commit 9cd0859

Please sign in to comment.