Multiplayer connects but needs to load players and finish setup checks.
This commit is contained in:
@@ -3,28 +3,48 @@ extends Node
|
||||
|
||||
const SERVER_PORT = 8080
|
||||
const SERVER_IP = "127.0.0.1"
|
||||
var handle : String
|
||||
var id : int
|
||||
var players : Dictionary = {}
|
||||
|
||||
func become_host() -> void:
|
||||
print("Starting host!")
|
||||
|
||||
var server_peer = ENetMultiplayerPeer.new()
|
||||
server_peer.create_server(SERVER_PORT)
|
||||
|
||||
players[handle] = 1
|
||||
multiplayer.multiplayer_peer = server_peer
|
||||
|
||||
print("%s %d t" % [handle, multiplayer.get_unique_id()])
|
||||
multiplayer.peer_connected.connect(_add_player_to_game)
|
||||
multiplayer.peer_disconnected.connect(_remove_player_from_game)
|
||||
|
||||
|
||||
func join_game() -> void:
|
||||
func join_game(handle : String) -> Error:
|
||||
print("Player 2 joining.")
|
||||
|
||||
var client_peer = ENetMultiplayerPeer.new()
|
||||
client_peer.create_client(SERVER_IP, SERVER_PORT)
|
||||
|
||||
var error = client_peer.create_client(SERVER_IP, SERVER_PORT)
|
||||
multiplayer.multiplayer_peer = client_peer
|
||||
|
||||
var p = multiplayer.get_unique_id()
|
||||
var is_host = multiplayer.is_server()
|
||||
if !error:
|
||||
print("%s %d" % [handle, multiplayer.get_unique_id()])
|
||||
rpc_id(1, "receive_peer_handle",multiplayer.get_unique_id(), handle )
|
||||
return !error
|
||||
|
||||
func _add_player_to_game(id : int) -> void:
|
||||
print("Player %s joined the game!" % id)
|
||||
players[handle] = id
|
||||
|
||||
func _remove_player_from_game(id : int) -> void:
|
||||
print("Player %s left the game!" % id)
|
||||
|
||||
@rpc("any_peer")
|
||||
func get_handle_from_peer(peer_id) -> void:
|
||||
rpc_id(peer_id, "receive_peer_handle", id, handle)
|
||||
|
||||
@rpc("any_peer", "call_remote")
|
||||
func receive_peer_handle(peer_id, peer_handle) -> void:
|
||||
print("Player %s identified as %s!" % [peer_id, peer_handle])
|
||||
players[peer_handle] = peer_id
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class_name MultiplayerPlayer extends Player
|
||||
class_name MultiplayerPC extends Player
|
||||
|
||||
@export var player_id := 1 :
|
||||
set(id):
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
extends Control
|
||||
|
||||
var host : bool
|
||||
|
||||
var handle : String
|
||||
func _on_host_game_button_pressed() -> void:
|
||||
%Menu.visible = false
|
||||
%Hosting.visible = true
|
||||
@@ -19,10 +19,17 @@ func _on_menu_back_button_pressed() -> void:
|
||||
|
||||
|
||||
func _on_start_hosting_button_pressed() -> void:
|
||||
var host_name = %HostHandle.text
|
||||
if len(host_name) < 3:
|
||||
return
|
||||
handle = host_name
|
||||
Multiplayer.handle = handle
|
||||
#Validate entries
|
||||
%Hosting.visible = false
|
||||
#Set fields
|
||||
%ParticipantsText.text = "[color=FFFF00]*%s (HOST)[/color]\n" % handle
|
||||
%"Host Waiting".visible = true
|
||||
Multiplayer.become_host()
|
||||
|
||||
|
||||
func _on_hosting_back_button_pressed() -> void:
|
||||
@@ -43,10 +50,15 @@ func _on_waiting_back_button_pressed() -> void:
|
||||
|
||||
|
||||
func _on_join_button_pressed() -> void:
|
||||
var host_name = %JoinHandle.text
|
||||
if len(host_name) < 3:
|
||||
return
|
||||
handle = host_name
|
||||
#Check for valid game
|
||||
#If found
|
||||
%Joining.visible = false
|
||||
%"Host Waiting".visible = true
|
||||
if Multiplayer.join_game(handle):
|
||||
%"Host Waiting".visible = true
|
||||
|
||||
|
||||
func _on_joining_back_button_pressed() -> void:
|
||||
|
||||
Reference in New Issue
Block a user