41 lines
1.0 KiB
GDScript
41 lines
1.0 KiB
GDScript
extends Node
|
|
|
|
|
|
var message_types: Dictionary[String, Array] = {}
|
|
|
|
|
|
func subscribe(id: String, callback: Callable) -> void:
|
|
var listeners: Array = message_types.get(id)
|
|
|
|
if not listeners:
|
|
message_types.set(id, Array())
|
|
listeners = message_types.get(id)
|
|
|
|
listeners.append(callback)
|
|
|
|
|
|
func emit(id: String, data: Variant) -> void:
|
|
emit_propagation(id, data)
|
|
|
|
|
|
@rpc("any_peer", "call_remote", "reliable")
|
|
func emit_request(id: String, data: Variant) -> void:
|
|
var listeners: Array = message_types.get(id)
|
|
if not listeners:
|
|
push_error("'%s' not an existing message type to send...", [id])
|
|
return
|
|
|
|
emit_propagation(id, data)
|
|
for peer_id in multiplayer.get_peers():
|
|
emit_propagation.rpc_id(peer_id, id, data)
|
|
|
|
@rpc("authority", "call_remote", "reliable")
|
|
func emit_propagation(id: String, data: Variant) -> void:
|
|
var listeners: Array = message_types.get(id)
|
|
if not listeners:
|
|
push_error("'%s' not an existing message type to send...", [id])
|
|
return
|
|
|
|
for callback: Callable in listeners:
|
|
callback.call(data)
|