Skip to content

Commit 42cbcdd

Browse files
committed
WIP: Add named classes for final blocks
Instead of defining blocks in category_factory.gd, define blocks ahead of time, each in their own GDScript file. Although it is not a concept in GDScript itself, we can consider block classes such as EntryBlock to be abstract block types. By doing this, we are able to reduce duplication in the block script resource associated with each BlockCode node. In particular, the serialized data can simply refer to "PrintBlock", and all of the EntryBlock properties expected for the print block are implied. This commit only adds ReadyBlock and PrintBlock as an example, and likely breaks everything else.
1 parent f98cdc0 commit 42cbcdd

File tree

6 files changed

+132
-13
lines changed

6 files changed

+132
-13
lines changed

addons/block_code/drag_manager/drag_manager.gd

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func drag_block(block: Block, copied_from: Block = null):
117117

118118

119119
func copy_block(block: Block) -> Block:
120-
return block.duplicate(DUPLICATE_USE_INSTANTIATION) # use instantiation
120+
return block.copy_block()
121121

122122

123123
func copy_picked_block_and_drag(block: Block):

addons/block_code/ui/block_canvas/block_canvas.gd

+10-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,16 @@ func clear_canvas():
7171

7272
func load_tree(parent: Node, node: SerializedBlockTreeNode):
7373
var _block_scene_path = _block_scenes_by_class[node.serialized_block.block_class]
74-
var scene: Block = load(_block_scene_path).instantiate()
74+
var _block_scene_resource = load(_block_scene_path)
75+
var scene: Block
76+
if _block_scene_resource is PackedScene:
77+
scene = _block_scene_resource.instantiate()
78+
elif _block_scene_resource is Script:
79+
scene = _block_scene_resource.new()
80+
else:
81+
push_error("Unable to instantiate block type: ", _block_scene_path)
82+
return
83+
7584
for prop_pair in node.serialized_block.serialized_props:
7685
scene.set(prop_pair[0], prop_pair[1])
7786

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

+3-10
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ static func get_general_categories() -> Array[BlockCategory]:
1515

1616
# Lifecycle
1717
var lifecycle_list: Array[Block] = []
18-
b = BLOCKS["entry_block"].instantiate()
19-
b.block_name = "ready_block"
20-
b.block_format = "On Ready"
21-
b.statement = "func _ready():"
22-
lifecycle_list.append(b)
18+
19+
lifecycle_list.append(ReadyBlock.new())
2320

2421
b = BLOCKS["entry_block"].instantiate()
2522
b.block_name = "process_block"
@@ -78,11 +75,7 @@ static func get_general_categories() -> Array[BlockCategory]:
7875
# Test
7976
var test_list: Array[Block] = []
8077

81-
b = BLOCKS["statement_block"].instantiate()
82-
b.block_format = "print {text: STRING}"
83-
b.statement = "print({text})"
84-
b.defaults = {"text": "Hello"}
85-
test_list.append(b)
78+
test_list.append(PrintBlock.new())
8679

8780
var test_category: BlockCategory = BlockCategory.new("Test", test_list, Color("9989df"))
8881

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
@tool
2+
class_name PrintBlock
3+
extends StatementBlock
4+
5+
6+
func _init():
7+
var _block = load(StatementBlock.get_scene_path()).instantiate() as Node
8+
_block.replace_by(self, true)
9+
block_name = _block.block_name
10+
label = _block.label
11+
color = _block.color
12+
block_type = _block.block_type
13+
bottom_snap_path = _block.bottom_snap_path
14+
_block.queue_free()
15+
16+
block_name = "print_block"
17+
block_format = "print {text: STRING}"
18+
statement = "print({text})"
19+
color = Color("9989df")
20+
21+
22+
func copy_block():
23+
return PrintBlock.new()
24+
25+
26+
static func get_block_class():
27+
return "PrintBlock"
28+
29+
30+
static func get_scene_path():
31+
return "res://addons/block_code/ui/picker/categories/print_block.gd"
32+
33+
34+
# Strip out properties that never change for this block type
35+
func get_serialized_props() -> Array:
36+
var props = super()
37+
return props.filter(func(prop): return prop[0] not in ["block_name", "label", "color", "block_type", "block_format", "statement"])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
@tool
2+
class_name ReadyBlock
3+
extends EntryBlock
4+
5+
6+
func _init():
7+
var _block = load(EntryBlock.get_scene_path()).instantiate() as Node
8+
_block.replace_by(self, true)
9+
block_name = _block.block_name
10+
label = _block.label
11+
color = _block.color
12+
block_type = _block.block_type
13+
bottom_snap_path = _block.bottom_snap_path
14+
_block.queue_free()
15+
16+
block_name = "ready_block"
17+
block_format = "On Ready"
18+
statement = "func _ready():"
19+
color = Color("fa5956")
20+
21+
22+
func copy_block():
23+
return ReadyBlock.new()
24+
25+
26+
static func get_block_class():
27+
return "ReadyBlock"
28+
29+
30+
static func get_scene_path():
31+
return "res://addons/block_code/ui/picker/categories/ready_block.gd"
32+
33+
34+
# Strip out properties that never change for this block type
35+
func get_serialized_props() -> Array:
36+
var props = super()
37+
return props.filter(func(prop): return prop[0] not in ["block_name", "label", "color", "block_type", "block_format", "statement"])

test_game/test_game.tscn

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[gd_scene load_steps=177 format=3 uid="uid://bbwmxee7ukgul"]
1+
[gd_scene load_steps=183 format=3 uid="uid://bbwmxee7ukgul"]
22

33
[ext_resource type="PackedScene" uid="uid://ddx1cd5q6t61o" path="res://addons/block_code/simple_nodes/simple_character/simple_character.tscn" id="1_hrpwq"]
44
[ext_resource type="Script" path="res://addons/block_code/block_code_node/block_code.gd" id="2_ewral"]
@@ -8,6 +8,45 @@
88
[ext_resource type="Script" path="res://addons/block_code/block_script_data/block_script_data.gd" id="5_q37d3"]
99
[ext_resource type="Texture2D" uid="uid://dr8e0tvfxjy1f" path="res://icon.svg" id="7_a27o8"]
1010

11+
[sub_resource type="Resource" id="Resource_bkrpj"]
12+
script = ExtResource("3_dpt5n")
13+
block_class = &"PrintBlock"
14+
serialized_props = [["position", Vector2(0, 0)], ["param_input_strings", {
15+
"text": "test"
16+
}]]
17+
18+
[sub_resource type="Resource" id="Resource_8i26i"]
19+
script = ExtResource("2_pqvcj")
20+
serialized_block = SubResource("Resource_bkrpj")
21+
path_child_pairs = []
22+
23+
[sub_resource type="Resource" id="Resource_l25ts"]
24+
script = ExtResource("3_dpt5n")
25+
block_class = &"ReadyBlock"
26+
serialized_props = [["position", Vector2(156, 133)], ["param_input_strings", {}]]
27+
28+
[sub_resource type="Resource" id="Resource_o5do6"]
29+
script = ExtResource("2_pqvcj")
30+
serialized_block = SubResource("Resource_l25ts")
31+
path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_8i26i")]]
32+
33+
[sub_resource type="Resource" id="Resource_vhdmi"]
34+
script = ExtResource("4_xt862")
35+
array = Array[ExtResource("2_pqvcj")]([SubResource("Resource_o5do6")])
36+
37+
[sub_resource type="Resource" id="Resource_t3mtx"]
38+
script = ExtResource("5_q37d3")
39+
script_inherits = "Camera2D"
40+
block_trees = SubResource("Resource_vhdmi")
41+
generated_script = "extends Camera2D
42+
43+
var VAR_DICT := {}
44+
45+
func _ready():
46+
print('test')
47+
48+
"
49+
1150
[sub_resource type="Resource" id="Resource_uwmna"]
1251
script = ExtResource("3_dpt5n")
1352
block_class = &"StatementBlock"
@@ -1109,6 +1148,10 @@ func _process(delta):
11091148

11101149
[node name="Camera2D" type="Camera2D" parent="."]
11111150

1151+
[node name="BlockCode" type="Node" parent="Camera2D"]
1152+
script = ExtResource("2_ewral")
1153+
block_script = SubResource("Resource_t3mtx")
1154+
11121155
[node name="Will" parent="." instance=ExtResource("1_hrpwq")]
11131156
position = Vector2(-71, -18)
11141157

0 commit comments

Comments
 (0)