Moving some of game to use MessageBus

This commit is contained in:
2026-08-08 01:22:54 -05:00
parent 6ebee75920
commit 50439bc8e0
7 changed files with 54 additions and 83 deletions

View File

@@ -5,36 +5,52 @@ var message_types: Dictionary[String, Array] = {}
func subscribe(id: String, callback: Callable) -> void:
var listeners: Array = message_types.get(id)
if not listeners:
if not message_types.has(id):
message_types.set(id, Array())
listeners = message_types.get(id)
listeners.append(callback)
message_types.get(id).append(callback)
func emit(id: String, data: Variant) -> void:
emit_propagation(id, data)
# TODO: Need to actually validate client request. Also, message bus is more
# about server talking to clients than clients to server. I.E, reducing
# the number of needed @rpc method decorator bindings.
@rpc("any_peer", "call_remote", "reliable")
func emit_request(id: String, data: Variant) -> void:
func emit_request(id: String, data: Variant, expand: bool = false) -> void:
if not multiplayer.is_server(): return
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)
emit_propagation(id, data, expand)
@rpc("authority", "call_remote", "reliable")
func emit_propagation(id: String, data: Variant) -> void:
@rpc("authority", "call_local", "reliable")
func emit_propagation(id: String, data: Variant, expand: bool = false) -> 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)
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:
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:
if not expand:
callback.call(data)
else:
callback.callv(data)