Skip to content

Commit 78fb1c7

Browse files
authored
Merge pull request #192 from endlessm/T35591-read-blocks-from-files
T35591 Read block definition from resource files
2 parents 3ba53f5 + 3971f47 commit 78fb1c7

File tree

14 files changed

+80
-114
lines changed

14 files changed

+80
-114
lines changed

addons/block_code/blocks/lifecycle/physics_process.tres

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
[resource]
66
script = ExtResource("1_s0hq0")
77
name = &"physics_process"
8+
description = "Attached blocks will be executed during the \"physics\" processing step of the main loop"
9+
category = "Lifecycle"
810
type = 1
911
variant_type = 0
1012
display_template = "On Physics Process"
1113
code_template = "func _physics_process(delta):"
12-
description = "The following will be executed during the \"physics\" processing step of the main loop"
13-
category = "Lifecycle"
1414
defaults = {}
1515
signal_name = ""
16+
scope = ""

addons/block_code/blocks/lifecycle/process.tres

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
[resource]
66
script = ExtResource("1_pmina")
77
name = &"process"
8+
description = "Attached blocks will be executed during the processing step of the main loop"
9+
category = "Lifecycle"
810
type = 1
911
variant_type = 0
1012
display_template = "On Process"
1113
code_template = "func _process(delta):"
12-
description = "The following will be executed during the processing step of the main loop"
13-
category = "Lifecycle"
1414
defaults = {}
1515
signal_name = ""
16+
scope = ""

addons/block_code/blocks/lifecycle/ready.tres

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
[resource]
66
script = ExtResource("1_vk0xk")
77
name = &"ready"
8+
description = "Attached blocks will be executed once when the node is \"ready\""
9+
category = "Lifecycle"
810
type = 1
911
variant_type = 0
1012
display_template = "On Ready"
1113
code_template = "func _ready():"
12-
description = "The following will be executed when the node is \"ready\""
13-
category = "Lifecycle"
1414
defaults = {}
1515
signal_name = ""
16+
scope = ""

addons/block_code/blocks/log/print.tres

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ type = 2
1111
variant_type = 0
1212
display_template = "Print {text: STRING}"
1313
code_template = "print({text})"
14-
defaults = {}
14+
defaults = {
15+
"text": "Hello"
16+
}
1517
signal_name = ""
1618
scope = ""

addons/block_code/blocks/loops/for.tres

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
[resource]
66
script = ExtResource("1_u01bi")
77
name = &"for"
8+
description = "Run the connected blocks [i]number[/i] times"
9+
category = "Loops"
810
type = 4
911
variant_type = 0
10-
display_template = "Repeat {n: INT}"
11-
code_template = "for __i in {n}:"
12-
description = "Run the connected blocks [i]n[/i] times"
13-
category = "Loops"
12+
display_template = "Repeat {number: INT}"
13+
code_template = "for __i in {number}:"
1414
defaults = {}
1515
signal_name = ""
16+
scope = ""

addons/block_code/blocks/loops/while.tres

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
[resource]
66
script = ExtResource("1_fxxh0")
77
name = &"while"
8+
description = "Run the connected blocks as long as [i]condition[/i] is true.
9+
10+
Hint: snap a [b]Comparison[/b] block into the condition."
11+
category = "Loops"
812
type = 4
913
variant_type = 0
1014
display_template = "While {condition: BOOL}"
1115
code_template = "while {condition}:"
12-
description = "Run the connected blocks as long as [i]condition[/i] is true.\\nHint: snap a [b]Comparison[/b] block into the condition."
13-
category = "Loops"
1416
defaults = {}
1517
signal_name = ""
18+
scope = ""

addons/block_code/code_generation/block_definition.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ extends Resource
55
const Types = preload("res://addons/block_code/types/types.gd")
66

77
@export var name: StringName
8-
@export var description: String
8+
@export_multiline var description: String
99
@export var category: String
1010

1111
@export var type: Types.BlockType
1212
@export var variant_type: Variant.Type
1313

1414
@export var display_template: String
15-
@export var code_template: String
15+
@export_multiline var code_template: String
1616
@export var defaults: Dictionary
1717

1818
## Only for blocks of type Types.ENTRY. If non-empty, this block defines a

addons/block_code/code_generation/blocks_catalog.gd

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ extends Object
22

33
const BlockDefinition = preload("res://addons/block_code/code_generation/block_definition.gd")
44
const Types = preload("res://addons/block_code/types/types.gd")
5+
const Util = preload("res://addons/block_code/code_generation/util.gd")
6+
7+
const _BLOCKS_PATH = "res://addons/block_code/blocks/"
58

69
static var _catalog: Dictionary
710

@@ -11,24 +14,11 @@ static func setup():
1114
return
1215

1316
_catalog = {}
14-
var block_definition: BlockDefinition = BlockDefinition.new()
15-
block_definition.name = &"ready_block"
16-
block_definition.type = Types.BlockType.ENTRY
17-
block_definition.display_template = "On Ready"
18-
block_definition.code_template = "func _ready():"
19-
block_definition.description = 'Attached blocks will be executed once when the node is "ready"'
20-
block_definition.category = "Lifecycle"
21-
_catalog[&"ready_block"] = block_definition
22-
23-
block_definition = BlockDefinition.new()
24-
block_definition.name = &"print"
25-
block_definition.type = Types.BlockType.STATEMENT
26-
block_definition.display_template = "print {text: STRING}"
27-
block_definition.code_template = "print({text})"
28-
block_definition.defaults = {"text": "Hello"}
29-
block_definition.description = "Print the text to output"
30-
block_definition.category = "Log"
31-
_catalog[&"print"] = block_definition
17+
18+
var definition_files = Util.get_files_in_dir_recursive(_BLOCKS_PATH, "*.tres")
19+
for file in definition_files:
20+
var block_definition = load(file)
21+
_catalog[block_definition.name] = block_definition
3222

3323

3424
static func get_block(block_name: StringName):
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
extends Object
2+
3+
4+
static func get_files_in_dir_recursive(path: String, pattern: String) -> Array:
5+
var files = []
6+
var dir := DirAccess.open(path)
7+
8+
if not dir:
9+
return files
10+
11+
dir.list_dir_begin()
12+
13+
var file_name = dir.get_next()
14+
15+
while file_name != "":
16+
var file_path = path + "/" + file_name
17+
if dir.current_is_dir():
18+
files.append_array(get_files_in_dir_recursive(file_path, pattern))
19+
elif file_name.matchn(pattern):
20+
files.append(file_path)
21+
22+
file_name = dir.get_next()
23+
24+
return files

addons/block_code/examples/pong_game/pong_game.tscn

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ serialized_props = [["color", Color(0.92549, 0.231373, 0.34902, 1)], ["scope", "
3535

3636
[sub_resource type="Resource" id="Resource_yiq7s"]
3737
script = ExtResource("4_qtggh")
38-
name = &"ready_block"
38+
name = &"ready"
3939
position = Vector2(75, 175)
4040
path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_q65fe")]]
4141
block_serialized_properties = SubResource("Resource_q1vhx")
@@ -130,7 +130,7 @@ serialized_props = [["color", Color(0.92549, 0.231373, 0.34902, 1)], ["scope", "
130130

131131
[sub_resource type="Resource" id="Resource_hrubs"]
132132
script = ExtResource("4_qtggh")
133-
name = &"ready_block"
133+
name = &"ready"
134134
position = Vector2(50, 175)
135135
path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_ttkrd")]]
136136
block_serialized_properties = SubResource("Resource_06s7w")
@@ -269,7 +269,7 @@ serialized_props = [["color", Color(0.92549, 0.231373, 0.34902, 1)], ["scope", "
269269

270270
[sub_resource type="Resource" id="Resource_1h6wi"]
271271
script = ExtResource("4_qtggh")
272-
name = &"ready_block"
272+
name = &"ready"
273273
position = Vector2(54, 47)
274274
path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_gywyt")]]
275275
block_serialized_properties = SubResource("Resource_b0aen")
@@ -862,7 +862,7 @@ serialized_props = [["color", Color(0.92549, 0.231373, 0.34902, 1)], ["scope", "
862862

863863
[sub_resource type="Resource" id="Resource_njwj4"]
864864
script = ExtResource("4_qtggh")
865-
name = &"ready_block"
865+
name = &"ready"
866866
position = Vector2(54, 47)
867867
path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_8xxm3")]]
868868
block_serialized_properties = SubResource("Resource_g3mty")

0 commit comments

Comments
 (0)