-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_controller.gd
65 lines (46 loc) · 1.39 KB
/
sync_controller.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
extends Node
const PORT = 45982
const ADDRESS = "localhost"
const BROADCAST_PERIOD = 120
var socket = PacketPeerUDP.new()
var host_ip
var broadcast_wait = BROADCAST_PERIOD
func _ready():
socket.listen(PORT + 1)
func _physics_process(_delta):
if get_tree().is_network_server():
broadcast_wait -= 1
if broadcast_wait <= 0:
broadcast_wait = BROADCAST_PERIOD
socket.put_packet(PoolByteArray([0]))
else:
if socket.get_available_packet_count() > 0:
socket.get_packet()
var ip = socket.get_packet_ip()
if host_ip != ip:
host_ip = ip
print("Found server %s" % ip)
remote func sync_tree(data: String):
for child in $"/root/Main".get_children():
if child.name.begins_with("Task"):
child.free()
$"/root/Main".load_tree(data)
func _host():
var peer = NetworkedMultiplayerENet.new()
peer.create_server(PORT, 2)
get_tree().network_peer = peer
print("Created server with port %d" % PORT)
socket.set_broadcast_enabled(true)
socket.set_dest_address("255.255.255.255", PORT + 1)
socket.put_packet(PoolByteArray([0]))
func _join():
var peer = NetworkedMultiplayerENet.new()
peer.create_client(host_ip, PORT)
get_tree().network_peer = peer
print("Joined address \"%s\" with port %d" % [host_ip, PORT])
func _on_ButtonHost_pressed():
_host()
func _on_ButtonConnect_pressed():
_join()
func _on_ButtonSync_pressed():
rpc("sync_tree", $"/root/Main/Task".surrender_data())