Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 31 additions & 0 deletions addons/talo/samples/multiplayer/multiplayer.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[gd_scene load_steps=2 format=3 uid="uid://d2y2fyjhcu688"]

[ext_resource type="Script" path="res://addons/talo/samples/multiplayer/scripts/multiplayer.gd" id="1_uay0c"]

[node name="Multiplayer" type="Node2D"]
script = ExtResource("1_uay0c")

[node name="Identify" type="VBoxContainer" parent="."]
unique_name_in_owner = true
offset_left = 16.0
offset_top = 16.0
offset_right = 400.0
offset_bottom = 74.0

[node name="HBoxContainer" type="HBoxContainer" parent="Identify"]
layout_mode = 2

[node name="IdentifierLineEdit" type="LineEdit" parent="Identify/HBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 3
placeholder_text = "identifier..."

[node name="IdentifyButton" type="Button" parent="Identify/HBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
text = "Identify"

[node name="MessageLabel" type="Label" parent="Identify"]
unique_name_in_owner = true
layout_mode = 2
51 changes: 51 additions & 0 deletions addons/talo/samples/multiplayer/player.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[gd_scene load_steps=4 format=3 uid="uid://du82kmf6hub41"]

[ext_resource type="Texture2D" uid="uid://b3naludt1ank2" path="res://icon.png" id="1_cm242"]
[ext_resource type="Script" path="res://addons/talo/samples/multiplayer/scripts/player.gd" id="2_myrjg"]

[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_sjscl"]
properties/0/path = NodePath(".:position")
properties/0/spawn = true
properties/0/replication_mode = 2

[node name="Player" type="Node2D"]
script = ExtResource("2_myrjg")

[node name="NameLabel" type="Label" parent="."]
unique_name_in_owner = true
anchors_preset = 5
anchor_left = 0.5
anchor_right = 0.5
offset_left = -121.0
offset_top = -81.0
offset_right = 121.0
offset_bottom = -58.0
grow_horizontal = 2
text = "Identifier"
horizontal_alignment = 1
vertical_alignment = 1

[node name="RpcTestButton" type="Button" parent="."]
unique_name_in_owner = true
anchors_preset = 7
anchor_left = 0.5
anchor_top = 1.0
anchor_right = 0.5
anchor_bottom = 1.0
offset_left = -133.0
offset_top = 57.0
offset_right = 133.0
offset_bottom = 88.0
grow_horizontal = 2
grow_vertical = 0
focus_mode = 0
text = "RpcTestButton"

[node name="Sprite2D" type="Sprite2D" parent="."]
scale = Vector2(0.1, 0.1)
texture = ExtResource("1_cm242")

[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
replication_interval = 0.03
delta_interval = 0.03
replication_config = SubResource("SceneReplicationConfig_sjscl")
111 changes: 111 additions & 0 deletions addons/talo/samples/multiplayer/scripts/multiplayer.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
extends Node2D


@export var services := "username"
@export var multiplayer_channel_name := "test_multiplayer_channel"

var peer := TaloMultiplayerPeer.new()

func _ready() -> void:
assert(not multiplayer_channel_name.is_empty())
%IdentifyButton.pressed.connect(_on_identify_btn_pressed)

func _spawn_player(peer_id: int) -> void:
var player := preload("../player.tscn").instantiate()
player.name = str(peer_id)
player.setup(peer_id)
add_child(player)
if player.is_multiplayer_authority():
var vp_center := get_tree().root.size * 0.5
var spawn_range := vp_center * 0.4
var spawn_position := vp_center + Vector2(
randf_range(-spawn_range.x, spawn_range.x),
randf_range(-spawn_range.y, spawn_range.y)
)
player.position = spawn_position

func _on_identify_btn_pressed() -> void:
var msg_label := %MessageLabel as Label
var btn := %IdentifyButton as Button

btn.disabled = true
# Identify
if Talo.identity_check(false) != OK:
var identifier := (%IdentifierLineEdit.text as String).strip_edges()
if identifier.is_empty():
msg_label.text = "Identify failed: identifier is empty."
btn.disabled = false
return

await Talo.players.identify(services, identifier)

if Talo.identity_check(false) != OK:
msg_label.text = "Identify failed: please check output for more details if logging is enabled."
btn.disabled = false
return


# Channels
var channel_id := -1
# Find exists channel.
var page := 0
while true:
var res := await Talo.channels.get_channels(page)
if not is_instance_valid(res):
msg_label.text = "Join channel failed: please check output for more details if logging is enabled."
btn.disabled = false
return

for channel: TaloChannel in res.channels:
if channel.name == multiplayer_channel_name:
channel_id = channel.id
break

if res.is_last_page:
# Last page.
break

page += 1

# Create new channel if not exists.
if channel_id < 0:
var channel := await Talo.channels.create(multiplayer_channel_name, true)
if not is_instance_valid(channel):
msg_label.text = "Join channel failed: please check output for more details if logging is enabled."
btn.disabled = false
return

channel_id = channel.id

# Join to the channel if need.
var subscribed_channels := await Talo.channels.get_subscribed_channels()
if subscribed_channels.all(func(channel: TaloChannel) -> bool: return channel.id != channel_id):
var joined_channel := await Talo.channels.join(channel_id)
if not is_instance_valid(joined_channel):
msg_label.text = "Join channel failed: please check output for more details if logging is enabled."
btn.disabled = false
return


# Join successful, setup TaloMultiplayerPeer
get_tree().get_multiplayer().multiplayer_peer = peer
if peer.listen_channel(channel_id) != OK:
btn.disabled = false
return

peer.peer_connected.connect(_on_peer_connected)
peer.peer_disconnected.connect(_on_peer_disconnected)

%Identify.hide()
_spawn_player(peer.get_unique_id())

func _on_peer_connected(id: int) -> void:
_spawn_player(id)

func _on_peer_disconnected(id: int) -> void:
for node in get_children():
if node.name != str(id):
continue
remove_child(node)
node.queue_free()
return
38 changes: 38 additions & 0 deletions addons/talo/samples/multiplayer/scripts/player.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
extends Node2D


const SPEED = 100.0


var _rpc_test_btn :Button:
get:
return %RpcTestButton


func _ready() -> void:
_rpc_test_btn.disabled = not is_multiplayer_authority()
_rpc_test_btn.pressed.connect(_on_rpc_test_btn_pressed)

func _process(delta: float) -> void:
if not is_multiplayer_authority():
return

var dir := Input.get_vector(&"ui_left", &"ui_right", &"ui_up", &"ui_down")
if dir.is_zero_approx():
return

translate(dir * SPEED * delta)

func setup(peer_id: int) -> void:
set_multiplayer_authority(peer_id)
%NameLabel.text = str(peer_id)
if is_inside_tree():
_rpc_test_btn.disabled = not is_multiplayer_authority()

@rpc("authority", "call_local", "reliable", 1)
func _rpc_test(text: String) -> void:
_rpc_test_btn.text = str(text)


func _on_rpc_test_btn_pressed() -> void:
_rpc_test.rpc("RpcTestButton: %s" % randi())
Loading