Files
godot-multiplayer-demo/main.gd

130 lines
4.0 KiB
GDScript

extends Node3D
@export var player_scene : PackedScene;
@export var lobby_list_item : PackedScene;
@export var lobby_list : VBoxContainer;
@onready var lobby_id_prompt: LineEdit = $UI/HostJoinSection/LobbyIdPrompt
@onready var join_button: Button = $UI/HostJoinSection/JoinButton
@onready var ui: Control = $UI
@onready var ui_host_join_section: Control = $UI/HostJoinSection
@onready var ui_lobby_list_section: Control = $UI/LobbyListSection
func _exit_tree():
if !is_in_lobby:
return
Steam.leaveLobby(lobby_id)
var lobby_id: int = 1;
var peer : SteamMultiplayerPeer;
var is_host : bool = false;
var is_joining : bool = false
var is_in_lobby : bool = false;
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
print("Steam initialized" , Steam.steamInit(480,true));
Steam.initRelayNetworkAccess();
Steam.lobby_match_list.connect(on_lobby_match_list)
Steam.lobby_created.connect(on_lobby_created);
Steam.lobby_joined.connect(on_lobby_joined);
multiplayer.connection_failed.connect(on_connection_failed);
func on_connection_failed():
print("Connection Failed")
func host_lobby():
if is_in_lobby:
return;
Steam.createLobby(Steam.LobbyType.LOBBY_TYPE_PUBLIC, 16);
func join_lobby(joining_lobby_id: int):
if is_in_lobby:
return;
is_joining = true;
Steam.joinLobby(joining_lobby_id)
func on_lobby_joined(joined_lobby_id: int, _permissions: int, _locked: bool, _response: int):
if !is_joining:
return;
self.lobby_id = joined_lobby_id;
peer = SteamMultiplayerPeer.new();
peer.server_relay = true;
peer.create_client(Steam.getLobbyOwner(joined_lobby_id))
multiplayer.multiplayer_peer = peer;
is_joining = false;
is_in_lobby = true;
func on_lobby_created(result: int, created_lobby_id: int):
if result == Steam.Result.RESULT_OK:
self.lobby_id = created_lobby_id
peer = SteamMultiplayerPeer.new()
peer.server_relay = true
peer.create_host();
multiplayer.multiplayer_peer = peer;
multiplayer.peer_connected.connect(add_player)
multiplayer.peer_disconnected.connect(remove_player)
add_player();
is_in_lobby = true;
is_host = true;
Steam.setLobbyData(created_lobby_id,"LobbyName", "Lobby "+str(created_lobby_id))
Steam.setLobbyData(created_lobby_id,"LobbyType", "GodotSteamDemo")
print("Created Lobby, Id: ", created_lobby_id)
func add_player(id: int = 1):
var player : Node3D = player_scene.instantiate();
player.position = Vector3(0,2,0)
player.name = str(id);
call_deferred("add_child",player);
func remove_player(id: int):
if !self.has_node(str(id)):
return
self.get_node(str(id)).queue_free();
func on_lobby_match_list(lobby_ids: Array):
#print(lobby_ids)
var _lobbys = lobby_ids.map(func(id):
var result = Dictionary()
var lobbyType = Steam.getLobbyData(id,"LobbyType");
var lobbyName = Steam.getLobbyData(id,"LobbyName");
result.set("LobbyId", id)
result.set("LobbyType", lobbyType)
result.set("LobbyName", lobbyName)
return result;
);
for child in lobby_list.get_children():
child.queue_free()
for lobby in _lobbys:
var list_item : LobbyListItem = lobby_list_item.instantiate();
list_item.lobby = lobby
list_item.on_lobby_join_clicked.connect(join_lobby)
lobby_list.add_child(list_item)
print(_lobbys);
func _on_button_pressed() -> void:
host_lobby();
func _on_join_button_pressed() -> void:
join_lobby(lobby_id_prompt.text.to_int())
func _on_lobby_id_text_changed(new_text: String) -> void:
join_button.disabled = new_text.length() <=0
func _on_list_lobbies_button_pressed() -> void:
Steam.addRequestLobbyListStringFilter("LobbyType","GodotSteamDemo",Steam.LobbyComparison.LOBBY_COMPARISON_EQUAL)
Steam.requestLobbyList()
func _physics_process(_delta):
if (Input.is_action_just_pressed("ui_toggle")):
ui.visible = !ui.visible
if (Input.is_action_just_pressed("ui_toggle_host_join_section")):
ui_host_join_section.visible = !ui_host_join_section.visible
if (Input.is_action_just_pressed("ui_toggle_lobby_list_section")):
ui_lobby_list_section.visible = !ui_lobby_list_section.visible
pass