2026-08-08 01:20:58 -05:00
|
|
|
extends Node
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var message_types: Dictionary[String, Array] = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func subscribe(id: String, callback: Callable) -> void:
|
|
|
|
|
if not message_types.has(id):
|
|
|
|
|
message_types.set(id, Array())
|
|
|
|
|
|
|
|
|
|
message_types.get(id).append(callback)
|
|
|
|
|
|
|
|
|
|
|
2026-08-09 18:36:28 -05:00
|
|
|
func emit_local(id: String, data: Variant, expand: bool = false) -> void:
|
|
|
|
|
emit_propagation(id, data, expand)
|
2026-08-08 01:20:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: Need to actually validate client request. Also, message bus is more
|
|
|
|
|
# about server talking to clients than clients to server. I.E, reducing
|
2026-08-09 18:36:28 -05:00
|
|
|
# the number of needed @rpc method decorator bindings.
|
2026-08-08 01:20:58 -05:00
|
|
|
@rpc("any_peer", "call_remote", "reliable")
|
|
|
|
|
func emit_request(id: String, data: Variant, expand: bool = false) -> void:
|
|
|
|
|
if not multiplayer.is_server(): return
|
2026-08-09 18:36:28 -05:00
|
|
|
if not message_types.has(id):
|
|
|
|
|
push_error("'%s' not an existing message type to handle...", [id])
|
2026-08-08 01:20:58 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
emit_propagation(id, data, expand)
|
|
|
|
|
|
|
|
|
|
@rpc("authority", "call_local", "reliable")
|
|
|
|
|
func emit_propagation(id: String, data: Variant, expand: bool = false) -> void:
|
2026-08-09 18:36:28 -05:00
|
|
|
if not message_types.has(id):
|
|
|
|
|
push_error("'%s' not an existing message type to handle...", [id])
|
2026-08-08 01:20:58 -05:00
|
|
|
return
|
|
|
|
|
|
2026-08-09 18:36:28 -05:00
|
|
|
for callback: Callable in message_types.get(id):
|
|
|
|
|
if data == null:
|
|
|
|
|
callback.call()
|
|
|
|
|
continue
|
|
|
|
|
|
2026-08-08 01:20:58 -05:00
|
|
|
if not expand:
|
|
|
|
|
callback.call(data)
|
|
|
|
|
else:
|
|
|
|
|
callback.callv(data)
|
|
|
|
|
|
|
|
|
|
@rpc("authority", "call_remote", "reliable")
|
|
|
|
|
func emit_propagation_clients_only(id: String, data: Variant, expand: bool = false) -> void:
|
2026-08-09 18:36:28 -05:00
|
|
|
if not message_types.has(id):
|
|
|
|
|
push_error("'%s' not an existing message type to handle...", [id])
|
2026-08-08 01:20:58 -05:00
|
|
|
return
|
|
|
|
|
|
2026-08-09 18:36:28 -05:00
|
|
|
for callback: Callable in message_types.get(id):
|
|
|
|
|
if data == null:
|
|
|
|
|
callback.call()
|
|
|
|
|
continue
|
|
|
|
|
|
2026-08-08 01:20:58 -05:00
|
|
|
if not expand:
|
|
|
|
|
callback.call(data)
|
|
|
|
|
else:
|
|
|
|
|
callback.callv(data)
|