Skip to content

Commit 38f4ea6

Browse files
TaloMultiplayerPeer and sample
1 parent cf42a46 commit 38f4ea6

File tree

5 files changed

+481
-0
lines changed

5 files changed

+481
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[gd_scene load_steps=2 format=3 uid="uid://d2y2fyjhcu688"]
2+
3+
[ext_resource type="Script" path="res://addons/talo/samples/multiplayer/scripts/multiplayer.gd" id="1_uay0c"]
4+
5+
[node name="Multiplayer" type="Node2D"]
6+
script = ExtResource("1_uay0c")
7+
8+
[node name="Identify" type="VBoxContainer" parent="."]
9+
unique_name_in_owner = true
10+
offset_left = 16.0
11+
offset_top = 16.0
12+
offset_right = 400.0
13+
offset_bottom = 74.0
14+
15+
[node name="HBoxContainer" type="HBoxContainer" parent="Identify"]
16+
layout_mode = 2
17+
18+
[node name="IdentifierLineEdit" type="LineEdit" parent="Identify/HBoxContainer"]
19+
unique_name_in_owner = true
20+
layout_mode = 2
21+
size_flags_horizontal = 3
22+
placeholder_text = "identifier..."
23+
24+
[node name="IdentifyButton" type="Button" parent="Identify/HBoxContainer"]
25+
unique_name_in_owner = true
26+
layout_mode = 2
27+
text = "Identify"
28+
29+
[node name="MessageLabel" type="Label" parent="Identify"]
30+
unique_name_in_owner = true
31+
layout_mode = 2
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
[gd_scene load_steps=4 format=3 uid="uid://du82kmf6hub41"]
2+
3+
[ext_resource type="Texture2D" uid="uid://b3naludt1ank2" path="res://icon.png" id="1_cm242"]
4+
[ext_resource type="Script" path="res://addons/talo/samples/multiplayer/scripts/player.gd" id="2_myrjg"]
5+
6+
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_sjscl"]
7+
properties/0/path = NodePath(".:position")
8+
properties/0/spawn = true
9+
properties/0/replication_mode = 2
10+
11+
[node name="Player" type="Node2D"]
12+
script = ExtResource("2_myrjg")
13+
14+
[node name="NameLabel" type="Label" parent="."]
15+
unique_name_in_owner = true
16+
anchors_preset = 5
17+
anchor_left = 0.5
18+
anchor_right = 0.5
19+
offset_left = -121.0
20+
offset_top = -81.0
21+
offset_right = 121.0
22+
offset_bottom = -58.0
23+
grow_horizontal = 2
24+
text = "Identifier"
25+
horizontal_alignment = 1
26+
vertical_alignment = 1
27+
28+
[node name="RpcTestButton" type="Button" parent="."]
29+
unique_name_in_owner = true
30+
anchors_preset = 7
31+
anchor_left = 0.5
32+
anchor_top = 1.0
33+
anchor_right = 0.5
34+
anchor_bottom = 1.0
35+
offset_left = -133.0
36+
offset_top = 57.0
37+
offset_right = 133.0
38+
offset_bottom = 88.0
39+
grow_horizontal = 2
40+
grow_vertical = 0
41+
focus_mode = 0
42+
text = "RpcTestButton"
43+
44+
[node name="Sprite2D" type="Sprite2D" parent="."]
45+
scale = Vector2(0.1, 0.1)
46+
texture = ExtResource("1_cm242")
47+
48+
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
49+
replication_config = SubResource("SceneReplicationConfig_sjscl")
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
extends Node2D
2+
3+
4+
@export var services := "username"
5+
@export var multiplayer_channel_name := "test_multiplayer_channel"
6+
7+
var peer := TaloMultiplayerPeer.new()
8+
9+
func _ready() -> void:
10+
assert(not multiplayer_channel_name.is_empty())
11+
%IdentifyButton.pressed.connect(_on_identify_btn_pressed)
12+
13+
func _spawn_player(peer_id: int) -> void:
14+
var player := preload("../player.tscn").instantiate()
15+
player.name = str(peer_id)
16+
player.setup(peer_id)
17+
add_child(player)
18+
if player.is_multiplayer_authority():
19+
var vp_center := get_tree().root.size * 0.5
20+
var spawn_range := vp_center * 0.4
21+
var spawn_position := vp_center + Vector2(
22+
randf_range(-spawn_range.x, spawn_range.x),
23+
randf_range(-spawn_range.y, spawn_range.y)
24+
)
25+
player.position = spawn_position
26+
27+
func _on_identify_btn_pressed() -> void:
28+
var msg_label := %MessageLabel as Label
29+
var btn := %IdentifyButton as Button
30+
31+
btn.disabled = true
32+
# Identify
33+
if Talo.identity_check(false) != OK:
34+
var identifier := (%IdentifierLineEdit.text as String).strip_edges()
35+
if identifier.is_empty():
36+
msg_label.text = "Identify failed: identifier is empty."
37+
btn.disabled = false
38+
return
39+
40+
await Talo.players.identify(services, identifier)
41+
42+
if Talo.identity_check(false) != OK:
43+
msg_label.text = "Identify failed: please check output for more details if logging is enabled."
44+
btn.disabled = false
45+
return
46+
47+
48+
# Channels
49+
var channel_id := -1
50+
# Find exists channel.
51+
var page := 0
52+
while true:
53+
var res := await Talo.channels.get_channels(page)
54+
if not is_instance_valid(res):
55+
msg_label.text = "Join channel failed: please check output for more details if logging is enabled."
56+
btn.disabled = false
57+
return
58+
59+
for channel: TaloChannel in res.channels:
60+
if channel.display_name == multiplayer_channel_name:
61+
channel_id = channel.id
62+
break
63+
64+
if res.last_page:
65+
# Last page.
66+
break
67+
68+
page += 1
69+
70+
# Create new channel if not exists.
71+
if channel_id < 0:
72+
var channel := await Talo.channels.create(multiplayer_channel_name, true)
73+
if not is_instance_valid(channel):
74+
msg_label.text = "Join channel failed: please check output for more details if logging is enabled."
75+
btn.disabled = false
76+
return
77+
78+
channel_id = channel.id
79+
80+
# Join to the channel if need.
81+
var subscribed_channels := await Talo.channels.get_subscribed_channels()
82+
if subscribed_channels.all(func(channel: TaloChannel) -> bool: return channel.id != channel_id):
83+
var joined_channel := await Talo.channels.join(channel_id)
84+
if not is_instance_valid(joined_channel):
85+
msg_label.text = "Join channel failed: please check output for more details if logging is enabled."
86+
btn.disabled = false
87+
return
88+
89+
90+
# Join successful, setup TaloMultiplayerPeer
91+
get_tree().get_multiplayer().multiplayer_peer = peer
92+
if peer.listen_channel(channel_id) != OK:
93+
btn.disabled = false
94+
return
95+
96+
peer.peer_connected.connect(_on_peer_connected)
97+
peer.peer_disconnected.connect(_on_peer_disconnected)
98+
99+
%Identify.hide()
100+
_spawn_player(peer.get_unique_id())
101+
102+
func _on_peer_connected(id: int) -> void:
103+
_spawn_player(id)
104+
105+
func _on_peer_disconnected(id: int) -> void:
106+
for node in get_children():
107+
if node.name != str(id):
108+
continue
109+
remove_child(node)
110+
node.queue_free()
111+
return
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
extends Node2D
2+
3+
4+
const SPEED = 100.0
5+
6+
7+
var _rpc_test_btn :Button:
8+
get:
9+
return %RpcTestButton
10+
11+
12+
func _ready() -> void:
13+
_rpc_test_btn.disabled = not is_multiplayer_authority()
14+
_rpc_test_btn.pressed.connect(_on_rpc_test_btn_pressed)
15+
16+
func _process(delta: float) -> void:
17+
if not is_multiplayer_authority():
18+
return
19+
20+
var dir := Input.get_vector(&"ui_left", &"ui_right", &"ui_up", &"ui_down")
21+
if dir.is_zero_approx():
22+
return
23+
24+
translate(dir * SPEED * delta)
25+
26+
func setup(peer_id: int) -> void:
27+
set_multiplayer_authority(peer_id)
28+
%NameLabel.text = str(peer_id)
29+
if is_inside_tree():
30+
_rpc_test_btn.disabled = not is_multiplayer_authority()
31+
32+
@rpc("authority", "call_local", "reliable", 1)
33+
func _rpc_test(text: String) -> void:
34+
_rpc_test_btn.text = str(text)
35+
36+
37+
func _on_rpc_test_btn_pressed() -> void:
38+
_rpc_test.rpc("RpcTestButton: %s" % randi())

0 commit comments

Comments
 (0)