From 01d4c6e372cc77b4b65837e9f8a775cbb76d317a Mon Sep 17 00:00:00 2001 From: Rikhardur Bjarni Einarsson Date: Mon, 29 Apr 2019 23:57:32 +0100 Subject: [PATCH] Changed to extend Reference and added a Node wrapper. Also validate function calls before calling. --- .../nodes/message_dispatcher_wrapper.gd | 33 +++++++++++++++++++ .../godot-next/objects/message_dispatcher.gd | 16 ++++++--- 2 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 addons/godot-next/nodes/message_dispatcher_wrapper.gd diff --git a/addons/godot-next/nodes/message_dispatcher_wrapper.gd b/addons/godot-next/nodes/message_dispatcher_wrapper.gd new file mode 100644 index 0000000..cf02e29 --- /dev/null +++ b/addons/godot-next/nodes/message_dispatcher_wrapper.gd @@ -0,0 +1,33 @@ +# MessageDispatcherWrapper +# author: MunWolf (Rikhardur Bjarni Einarsson) +# license: MIT +# copyright: Copyright (c) 2019 Rikhardur Bjarni Einarsson +# brief_description: A wrapper for MessageDispatcher that is extendable on a Node, +# people can also use this as a template to implement it on +# their own if they want to extend something else. + +extends Node + +class_name MessageDispatcherWrapper + +##### PROPERTIES ##### + +var _message_dispatcher = MessageDispatcher.new() + +##### PUBLIC METHODS ##### + +# See same function on MessageDispatcher +func connect_message(message_type: String, obj: Object, function: String) -> void: + _message_dispatcher.connect_message(message_type, obj, function) + +# See same function on MessageDispatcher +func disconnect_message(message_type: String, obj: Object, function: String) -> void: + _message_dispatcher.disconnect_message(message_type, obj, function) + +# See same function on MessageDispatcher +func disconnect_all_message() -> void: + _message_dispatcher.disconnect_all_message() + +# See same function on MessageDispatcher +func emit_message(message_type: String, message_data: Dictionary) -> bool: + return _message_dispatcher.emit_message(message_type, message_data) diff --git a/addons/godot-next/objects/message_dispatcher.gd b/addons/godot-next/objects/message_dispatcher.gd index f1acab3..d7ed474 100644 --- a/addons/godot-next/objects/message_dispatcher.gd +++ b/addons/godot-next/objects/message_dispatcher.gd @@ -2,9 +2,9 @@ # author: MunWolf (Rikhardur Bjarni Einarsson) # license: MIT # copyright: Copyright (c) 2019 Rikhardur Bjarni Einarsson -# brief_description: A message handler for non predefined signals, can be extended by any class -# Node, Reference, Resource etc, can also be directly attached to a Node. -extends Object +# brief_description: A message handler for non predefined signals, if you want to use this by extending it on a Node please use MessageDispatcherWrapper. + +extends Reference class_name MessageDispatcher @@ -43,9 +43,17 @@ func disconnect_all_message() -> void: # message_data: extra data that can be used by the handler or where the handler can store results. # return: returns if it was passed to any handler or not. func emit_message(message_type: String, message_data: Dictionary) -> bool: + var invalid var handlers = _message_handlers[message_type] if handlers != null: + var invalid = [] for handler in handlers: - handler[0].call(handler[1], message_type, message_data) + if is_instance_valid(handler[0]): + handler[0].call(handler[1], message_type, message_data) + else: + invalid.push_back(handler) + + for handler in invalid: + handlers.erase(handler) return handlers != null && !handlers.empty()