Skip to content

Commit e94214d

Browse files
committed
Add ‘On [body] entered/exited’ blocks for Area2D and RigidBody2D
Each block ‘compiles’ to a callback function, which needs to be connected to the signal in question. To do this, add an additional code-generation step that generates an _init() function to connect signals. A more idiomatic approach would be to connect to the signals in the _ready() function. This would involve merging additional code with the user-supplied _ready() function, so is deferred for now. _init() works fine.
1 parent 60a2c88 commit e94214d

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

addons/block_code/ui/block_canvas/node_block_canvas/node_block_canvas.gd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ func generate_script_from_current_window(script_inherits: String = ""):
2424

2525
script += "var VAR_DICT := {}\n\n"
2626

27+
var init_func = InstructionTree.TreeNode.new("func _init():")
28+
2729
for entry_block in entry_blocks:
2830
script += entry_block.get_entry_statement() + "\n"
2931

@@ -39,4 +41,10 @@ func generate_script_from_current_window(script_inherits: String = ""):
3941

4042
script += "\n"
4143

44+
if entry_block.signal_name:
45+
init_func.add_child(InstructionTree.TreeNode.new("{0}.connect(_on_{0})".format([entry_block.signal_name])))
46+
47+
if init_func.children:
48+
script += InstructionTree.new().generate_text(init_func)
49+
4250
return script

addons/block_code/ui/blocks/entry_block/entry_block.gd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
class_name EntryBlock
33
extends StatementBlock
44

5+
## if non-empty, this block defines a callback that will be connected to the signal with this name
6+
@export var signal_name: String
7+
58

69
func _ready():
710
block_type = Types.BlockType.ENTRY
@@ -23,3 +26,9 @@ func get_entry_statement() -> String:
2326
formatted_statement = formatted_statement.replace("{%s}" % pair[0], pair[1].get_string())
2427

2528
return formatted_statement
29+
30+
31+
func get_serialized_props() -> Array:
32+
var props := super()
33+
props.append_array(serialize_props(["signal_name"]))
34+
return props

addons/block_code/ui/picker/categories/category_factory.gd

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ static func get_general_categories() -> Array[BlockCategory]:
8383
b.statement = "print({text})"
8484
test_list.append(b)
8585

86-
b = BLOCKS["entry_block"].instantiate()
87-
b.block_format = "On body enter [body: NODE_PATH]"
88-
b.statement = "func _on_body_enter(body):"
89-
test_list.append(b)
90-
9186
var test_category: BlockCategory = BlockCategory.new("Test", test_list, Color("9989df"))
9287

9388
# Signal
@@ -339,10 +334,27 @@ static func get_built_in_categories(_class_name: String) -> Array[BlockCategory]
339334
props = ["modulate"]
340335

341336
"RigidBody2D":
342-
# On body entered
337+
for verb in ["entered", "exited"]:
338+
var b = BLOCKS["entry_block"].instantiate()
339+
b.block_format = "On [body: NODE_PATH] %s" % [verb]
340+
# HACK: Blocks refer to nodes by path but the callback receives the node itself;
341+
# convert to path
342+
b.statement = "func _on_body_%s(_body: Node):\n\tvar body: NodePath = _body.get_path()" % [verb]
343+
b.signal_name = "body_%s" % [verb]
344+
block_list.append(b)
343345

344346
props = ["mass", "linear_velocity", "angular_velocity"]
345347

348+
"Area2D":
349+
for verb in ["entered", "exited"]:
350+
var b = BLOCKS["entry_block"].instantiate()
351+
b.block_format = "On [body: NODE_PATH] %s" % [verb]
352+
# HACK: Blocks refer to nodes by path but the callback receives the node itself;
353+
# convert to path
354+
b.statement = "func _on_body_%s(_body: Node2D):\n\tvar body: NodePath = _body.get_path()" % [verb]
355+
b.signal_name = "body_%s" % [verb]
356+
block_list.append(b)
357+
346358
var prop_list = ClassDB.class_get_property_list(_class_name, true)
347359

348360
var class_cat: BlockCategory = category_from_property_list(prop_list, props, _class_name, Color.SLATE_GRAY)

0 commit comments

Comments
 (0)