Skip to content

Commit

Permalink
Changed to extend Reference and added a Node wrapper. Also validate f…
Browse files Browse the repository at this point in the history
…unction calls before calling.
  • Loading branch information
MunWolf committed Apr 29, 2019
1 parent f06e5e0 commit 01d4c6e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
33 changes: 33 additions & 0 deletions addons/godot-next/nodes/message_dispatcher_wrapper.gd
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 12 additions & 4 deletions addons/godot-next/objects/message_dispatcher.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()

0 comments on commit 01d4c6e

Please sign in to comment.