Skip to content

Commit

Permalink
WIP: Add named classes for final blocks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dylanmccall committed Jun 19, 2024
1 parent 2c47fde commit 2886d40
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 12 deletions.
2 changes: 1 addition & 1 deletion addons/block_code/drag_manager/drag_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func drag_block(block: Block, copied_from: Block = null):


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


func copy_picked_block_and_drag(block: Block):
Expand Down
11 changes: 10 additions & 1 deletion addons/block_code/ui/block_canvas/block_canvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,16 @@ func clear_canvas():

func load_tree(parent: Node, node: SerializedBlockTreeNode):
var _block_scene_path = _block_scenes_by_class[node.serialized_block.block_class]
var scene: Block = load(_block_scene_path).instantiate()
var _block_scene_resource = load(_block_scene_path)
var scene: Block
if _block_scene_resource is PackedScene:
scene = _block_scene_resource.instantiate()
elif _block_scene_resource is Script:
scene = _block_scene_resource.new()
else:
push_error("Unable to instantiate block type: ", _block_scene_path)
return

for prop_pair in node.serialized_block.serialized_props:
scene.set(prop_pair[0], prop_pair[1])

Expand Down
12 changes: 3 additions & 9 deletions addons/block_code/ui/picker/categories/category_factory.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ static func get_general_categories() -> Array[BlockCategory]:

# Lifecycle
var lifecycle_list: Array[Block] = []
b = BLOCKS["entry_block"].instantiate()
b.block_name = "ready_block"
b.block_format = "On Ready"
b.statement = "func _ready():"
lifecycle_list.append(b)

lifecycle_list.append(ReadyBlock.new())

b = BLOCKS["entry_block"].instantiate()
b.block_name = "process_block"
Expand Down Expand Up @@ -78,10 +75,7 @@ static func get_general_categories() -> Array[BlockCategory]:
# Test
var test_list: Array[Block] = []

b = BLOCKS["statement_block"].instantiate()
b.block_format = "print {text: STRING}"
b.statement = "print({text})"
test_list.append(b)
test_list.append(PrintBlock.new())

b = BLOCKS["entry_block"].instantiate()
b.block_format = "On body enter [body: NODE_PATH]"
Expand Down
32 changes: 32 additions & 0 deletions addons/block_code/ui/picker/categories/print_block.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@tool
class_name PrintBlock
extends StatementBlock

func _init():
var _block = load(StatementBlock.get_scene_path()).instantiate() as Node
_block.replace_by(self, true)
block_name = _block.block_name
label = _block.label
color = _block.color
block_type = _block.block_type
bottom_snap_path = _block.bottom_snap_path
_block.queue_free()

block_name = "print_block"
block_format = "print {text: STRING}"
statement = "print({text})"
color = Color("9989df")

func copy_block():
return PrintBlock.new()

static func get_block_class():
return "PrintBlock"

static func get_scene_path():
return "res://addons/block_code/ui/picker/categories/print_block.gd"

# Strip out properties that never change for this block type
func get_serialized_props() -> Array:
var props = super()
return props.filter(func(prop): return prop[0] not in ["block_name", "label", "color", "block_type", "block_format", "statement"])
32 changes: 32 additions & 0 deletions addons/block_code/ui/picker/categories/ready_block.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@tool
class_name ReadyBlock
extends EntryBlock

func _init():
var _block = load(EntryBlock.get_scene_path()).instantiate() as Node
_block.replace_by(self, true)
block_name = _block.block_name
label = _block.label
color = _block.color
block_type = _block.block_type
bottom_snap_path = _block.bottom_snap_path
_block.queue_free()

block_name = "ready_block"
block_format = "On Ready"
statement = "func _ready():"
color = Color("fa5956")

func copy_block():
return ReadyBlock.new()

static func get_block_class():
return "ReadyBlock"

static func get_scene_path():
return "res://addons/block_code/ui/picker/categories/ready_block.gd"

# Strip out properties that never change for this block type
func get_serialized_props() -> Array:
var props = super()
return props.filter(func(prop): return prop[0] not in ["block_name", "label", "color", "block_type", "block_format", "statement"])
45 changes: 44 additions & 1 deletion test_game/test_game.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=177 format=3 uid="uid://bbwmxee7ukgul"]
[gd_scene load_steps=183 format=3 uid="uid://bbwmxee7ukgul"]

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

[sub_resource type="Resource" id="Resource_bkrpj"]
script = ExtResource("3_dpt5n")
block_class = &"PrintBlock"
serialized_props = [["position", Vector2(0, 0)], ["param_input_strings", {
"text": "test"
}]]

[sub_resource type="Resource" id="Resource_8i26i"]
script = ExtResource("2_pqvcj")
serialized_block = SubResource("Resource_bkrpj")
path_child_pairs = []

[sub_resource type="Resource" id="Resource_l25ts"]
script = ExtResource("3_dpt5n")
block_class = &"ReadyBlock"
serialized_props = [["position", Vector2(156, 133)], ["param_input_strings", {}]]

[sub_resource type="Resource" id="Resource_o5do6"]
script = ExtResource("2_pqvcj")
serialized_block = SubResource("Resource_l25ts")
path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_8i26i")]]

[sub_resource type="Resource" id="Resource_vhdmi"]
script = ExtResource("4_xt862")
array = Array[ExtResource("2_pqvcj")]([SubResource("Resource_o5do6")])

[sub_resource type="Resource" id="Resource_t3mtx"]
script = ExtResource("5_q37d3")
script_inherits = "Camera2D"
block_trees = SubResource("Resource_vhdmi")
generated_script = "extends Camera2D
var VAR_DICT := {}
func _ready():
print('test')
"

[sub_resource type="Resource" id="Resource_uwmna"]
script = ExtResource("3_dpt5n")
block_class = &"StatementBlock"
Expand Down Expand Up @@ -1109,6 +1148,10 @@ func _process(delta):

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

[node name="BlockCode" type="Node" parent="Camera2D"]
script = ExtResource("2_ewral")
block_script = SubResource("Resource_t3mtx")

[node name="Will" parent="." instance=ExtResource("1_hrpwq")]
position = Vector2(-71, -18)

Expand Down

0 comments on commit 2886d40

Please sign in to comment.