Skip to content

Commit

Permalink
Replace every instance of magic number 1 with MultiplayerPeer::TARGET…
Browse files Browse the repository at this point in the history
…_PEER_SERVER
  • Loading branch information
ValorZard committed Sep 27, 2024
1 parent 4116339 commit 043db66
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
4 changes: 2 additions & 2 deletions examples/multiplayer-lan/rust/src/bullet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use godot::classes::{CharacterBody2D, ICharacterBody2D, SceneTreeTimer};
use godot::classes::{CharacterBody2D, ICharacterBody2D, MultiplayerPeer, SceneTreeTimer};
use godot::prelude::*;

use crate::NetworkId;
Expand All @@ -23,7 +23,7 @@ impl ICharacterBody2D for Bullet {
fn init(base: Base<CharacterBody2D>) -> Self {
Self {
direction: Vector2::new(1., 0.),
network_id: 1,
network_id: MultiplayerPeer::TARGET_PEER_SERVER,
timer: OnReady::from_base_fn(|base| {
base.get_tree().unwrap().create_timer(LIFETIME).unwrap()
}),
Expand Down
15 changes: 10 additions & 5 deletions examples/multiplayer-lan/rust/src/multiplayer_controller.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::collections::HashMap;

use godot::classes::{
Button, Control, ENetMultiplayerPeer, IControl, LineEdit, MultiplayerApi, RichTextLabel,
Button, Control, ENetMultiplayerPeer, IControl, LineEdit, MultiplayerApi, MultiplayerPeer,
RichTextLabel,
};
use godot::global::Error;
use godot::obj::WithBaseField;
Expand Down Expand Up @@ -53,9 +54,9 @@ impl MultiplayerController {
.get_node_as::<LineEdit>("UsernameLineEdit")
.get_text();
let network_id = self.multiplayer.get_unique_id();
// server always has peer id of 1
// server always has peer id of TARGET_PEER_SERVER (1)
self.base_mut().rpc_id(
1,
MultiplayerPeer::TARGET_PEER_SERVER.into(),
"send_player_information".into(),
&[Variant::from(username), Variant::from(network_id)],
);
Expand Down Expand Up @@ -134,7 +135,7 @@ impl MultiplayerController {
if self.multiplayer.is_server() {
for id in player_ids {
// don't call rpc on server
if id == 1 {
if id == MultiplayerPeer::TARGET_PEER_SERVER {
continue;
}
// force other clients to also load the game up
Expand Down Expand Up @@ -229,7 +230,11 @@ impl MultiplayerController {
#[func]
fn on_start_button_down(&mut self) {
// have client call server to start up game
self.base_mut().rpc_id(1, "load_game".into(), &[]);
self.base_mut().rpc_id(
MultiplayerPeer::TARGET_PEER_SERVER.into(),
"load_game".into(),
&[],
);
}
}

Expand Down
6 changes: 3 additions & 3 deletions examples/multiplayer-lan/rust/src/player.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use godot::classes::{
Area2D, CharacterBody2D, ICharacterBody2D, MultiplayerSynchronizer, PhysicsBody2D,
ProjectSettings,
Area2D, CharacterBody2D, ICharacterBody2D, MultiplayerPeer, MultiplayerSynchronizer,
PhysicsBody2D, ProjectSettings,
};
use godot::global::move_toward;
use godot::obj::WithBaseField;
Expand Down Expand Up @@ -130,7 +130,7 @@ impl ICharacterBody2D for Player {
health: MAX_HEALTH,
bullet_scene: PackedScene::new_gd(),
multiplayer_synchronizer: OnReady::node("MultiplayerSynchronizer"),
network_id: 1,
network_id: MultiplayerPeer::TARGET_PEER_SERVER,
username: "Player".into(),
sync_position: Vector2::new(0., 0.),
sync_rotation: 0.,
Expand Down
11 changes: 9 additions & 2 deletions examples/multiplayer-lan/rust/src/scene_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::collections::HashMap;

use godot::{classes::RichTextLabel, prelude::*};
use godot::{
classes::{MultiplayerPeer, RichTextLabel},
prelude::*,
};

use crate::{multiplayer_controller::MultiplayerController, player::Player, NetworkId};

Expand Down Expand Up @@ -130,7 +133,11 @@ impl INode2D for SceneManager {
.unwrap()
.get_node_as::<MultiplayerController>("MultiplayerController");
// Tell the server that this peer has loaded in.
multiplayer_controller.rpc_id(1, "load_in_player".into(), &[]);
multiplayer_controller.rpc_id(
MultiplayerPeer::TARGET_PEER_SERVER.into(),
"load_in_player".into(),
&[],
);
}

fn process(&mut self, _delta: f64) {
Expand Down

0 comments on commit 043db66

Please sign in to comment.