Moving some of lobby to use MessageBus
This commit is contained in:
57
globals/message_bus.gd
Normal file
57
globals/message_bus.gd
Normal file
@@ -0,0 +1,57 @@
|
||||
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)
|
||||
|
||||
|
||||
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, 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, expand)
|
||||
|
||||
@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:
|
||||
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)
|
||||
1
globals/message_bus.gd.uid
Normal file
1
globals/message_bus.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://oatwf3crdtpt
|
||||
Reference in New Issue
Block a user