54 lines
1.3 KiB
GDScript
54 lines
1.3 KiB
GDScript
extends Control
|
|
|
|
@export var Address = "127.0.0.1";
|
|
@export var port = 8910
|
|
|
|
var peer;
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
multiplayer.peer_connected.connect(PlayerConnected)
|
|
multiplayer.peer_disconnected.connect(PlayerDisconnected)
|
|
multiplayer.connected_to_server.connect(ConnectedToServer)
|
|
multiplayer.connection_failed.connect(ConnectionFailed)
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
pass
|
|
|
|
# server and client
|
|
func PlayerConnected(id):
|
|
print("Player connected " + str(id));
|
|
|
|
func PlayerDisconnected(id):
|
|
print("Player disconnected " + str(id))
|
|
|
|
# client only
|
|
func ConnectedToServer():
|
|
print("connected to server!")
|
|
|
|
func ConnectionFailed():
|
|
print("connection failed");
|
|
|
|
func _on_host_button_down() -> void:
|
|
peer = ENetMultiplayerPeer.new()
|
|
var error = peer.create_server(port, 2)
|
|
if error != OK:
|
|
print("cannot host" + error)
|
|
return
|
|
|
|
multiplayer.set_multiplayer_peer(peer);
|
|
print("waiting for players")
|
|
|
|
|
|
func _on_join_button_down() -> void:
|
|
peer = ENetMultiplayerPeer.new()
|
|
peer.create_client(Address, port);
|
|
|
|
multiplayer.set_multiplayer_peer(peer)
|
|
|
|
|
|
func _on_start_button_down() -> void:
|
|
pass # Replace with function body.
|