From 34335fdcacc14a1e82ecb2816009f4e6ec0a0e2d Mon Sep 17 00:00:00 2001 From: Dylan McCall Date: Thu, 20 Jun 2024 10:18:06 -0700 Subject: [PATCH] Add a "Send signal to node" block --- .../block_code_node/utilities/signal_manager.gd | 10 ++++++++++ .../ui/picker/categories/category_factory.gd | 8 +++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/addons/block_code/block_code_node/utilities/signal_manager.gd b/addons/block_code/block_code_node/utilities/signal_manager.gd index 2c7748fd..927c1515 100644 --- a/addons/block_code/block_code_node/utilities/signal_manager.gd +++ b/addons/block_code/block_code_node/utilities/signal_manager.gd @@ -7,3 +7,13 @@ func broadcast_signal(group: String, signal_name: String): await get_tree().root.ready get_tree().call_group(group, "signal_" + signal_name) + + +func send_signal_to_node(path: NodePath, signal_name: String): + # Make sure all nodes have been readied and scripts loaded before running signals + if not get_tree().root.is_node_ready(): + await get_tree().root.ready + + var node = get_node(path) + if node: + node.call("signal_" + signal_name) diff --git a/addons/block_code/ui/picker/categories/category_factory.gd b/addons/block_code/ui/picker/categories/category_factory.gd index 8f1fded9..ac92f35b 100644 --- a/addons/block_code/ui/picker/categories/category_factory.gd +++ b/addons/block_code/ui/picker/categories/category_factory.gd @@ -198,7 +198,13 @@ static func get_general_blocks() -> Array[Block]: b = BLOCKS["statement_block"].instantiate() b.block_format = "Send signal {signal: STRING} to group {group: STRING}" - b.statement = 'var signal_manager = get_tree().root.get_node_or_null("SignalManager")\n' + "if signal_manager:\n" + "\tsignal_manager.broadcast_signal({group}, {signal})" + b.statement = 'if get_tree().root.has_node("SignalManager"):\n' + '\tget_tree().root.get_node_or_null("SignalManager").broadcast_signal({group}, {signal})' + b.category = "Signal" + block_list.append(b) + + b = BLOCKS["statement_block"].instantiate() + b.block_format = "Send signal {signal: STRING} to node {node: NODE_PATH}" + b.statement = 'if get_tree().root.has_node("SignalManager"):\n' + '\tget_tree().root.get_node_or_null("SignalManager").send_signal_to_node({node}, {signal})' b.category = "Signal" block_list.append(b)