Skip to content

Commit 9a840c2

Browse files
committed
BlockScriptSerialization: Re-generate block definition for object property blocks
The drag & drop object property's blocks disappear after save & re-open the Godot project. And, shows error: core/variant/variant_utility.cpp:1092 - Cannot construct block from null block definition. res://addons/block_code/ui/block_canvas/block_canvas.gd:348 - Invalid call. Nonexistent function 'set_parameter_values_on_ready' in base 'Nil'. It is because the object property blocks are not the predefined blocks in the catalog. So, Block Coding plugin cannot find the block definition from the catalog when places the object property blocks into the cavas via _block_to_ast_node() after re-open the project. Therefore, introduce _get_obj_property_block_definition() generating object property's getter/setter block definition for get_block_definition(). Besides, it also needs the object property's value type. So, pass the AST node's arguments which is a Dictionary and may contain the "value" feild into get_block_definition(), too. https://phabricator.endlessm.com/T35649
1 parent a0df3fd commit 9a840c2

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

addons/block_code/serialization/block_script_serialization.gd

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func instantiate_block(block_definition: BlockDefinition) -> Block:
7070

7171

7272
func instantiate_block_by_name(block_name: String) -> Block:
73-
var block_definition := get_block_definition(block_name)
73+
var block_definition := get_block_definition(block_name, {})
7474

7575
if block_definition == null:
7676
push_warning("Cannot find a block definition for %s" % block_name)
@@ -79,24 +79,29 @@ func instantiate_block_by_name(block_name: String) -> Block:
7979
return instantiate_block(block_definition)
8080

8181

82-
func get_block_definition(block_name: String) -> BlockDefinition:
82+
func get_block_definition(block_name: String, arguments: Dictionary) -> BlockDefinition:
8383
var split := block_name.split(":", true, 1)
84+
var block_definition: BlockDefinition
8485

8586
if len(split) > 1:
8687
return _get_parameter_block_definition(split[0], split[1])
8788

88-
var block_definition = _get_base_block_definition(block_name)
89+
block_definition = _get_base_block_definition(block_name)
90+
if block_definition != null:
91+
return block_definition
8992

90-
if block_definition == null:
91-
# FIXME: This is a workaround for old-style output block references.
92-
# These were generated ahead of time using a block name that has
93-
# a "_" before the parameter name. Now, these parameter blocks
94-
# are generated on demand for any block name containing a ":".
95-
# Please remove this fallback when it is no longer necessary.
96-
split = block_name.rsplit("_", true, 1)
97-
return _get_parameter_block_definition(split[0], split[1])
93+
var property_value = arguments.get("value", null)
94+
block_definition = _get_obj_property_block_definition(block_name, property_value)
95+
if block_definition != null:
96+
return block_definition
9897

99-
return block_definition
98+
# FIXME: This is a workaround for old-style output block references.
99+
# These were generated ahead of time using a block name that has
100+
# a "_" before the parameter name. Now, these parameter blocks
101+
# are generated on demand for any block name containing a ":".
102+
# Please remove this fallback when it is no longer necessary.
103+
split = block_name.rsplit("_", true, 1)
104+
return _get_parameter_block_definition(split[0], split[1])
100105

101106

102107
func _get_base_block_definition(block_name: String) -> BlockDefinition:
@@ -133,6 +138,33 @@ func _get_parameter_block_definition(block_name: String, parameter_name: String)
133138
return block_definition
134139

135140

141+
func _get_obj_property_block_definition(block_name: String, property_value: Variant) -> BlockDefinition:
142+
var block_definition: BlockDefinition
143+
var variable: VariableDefinition
144+
var property_name: String
145+
var is_getter = true
146+
147+
if block_name.find("get_var_") == 0:
148+
property_name = block_name.get_slice("get_var_", 1)
149+
elif block_name.find("set_var_") == 0:
150+
property_name = block_name.get_slice("set_var_", 1)
151+
is_getter = false
152+
else:
153+
return null
154+
155+
if is_getter:
156+
# Getter block needs the property name and variant type information by
157+
# decoding block name
158+
var split := property_name.rsplit("_", true, 1)
159+
variable = VariableDefinition.new(split[0], int(split[1]))
160+
block_definition = BlocksCatalog.get_property_getter_block_definition(variable)
161+
else:
162+
variable = VariableDefinition.new(property_name, typeof(property_value))
163+
block_definition = BlocksCatalog.get_property_setter_block_definition(variable)
164+
165+
return block_definition
166+
167+
136168
func _update_block_definitions():
137169
_available_blocks.clear()
138170
_available_blocks.append_array(_get_inherited_block_definitions())
@@ -202,14 +234,15 @@ func _tree_to_ast(tree: BlockSerializationTree) -> BlockAST:
202234

203235
func _block_to_ast_node(node: BlockSerialization) -> BlockAST.ASTNode:
204236
var ast_node := BlockAST.ASTNode.new()
205-
ast_node.data = get_block_definition(node.name)
206237

207238
for arg_name in node.arguments:
208239
var argument = node.arguments[arg_name]
209240
if argument is ValueBlockSerialization:
210241
argument = _value_to_ast_value(argument)
211242
ast_node.arguments[arg_name] = argument
212243

244+
ast_node.data = get_block_definition(node.name, ast_node.arguments)
245+
213246
var children: Array[BlockAST.ASTNode]
214247
for c in node.children:
215248
children.append(_block_to_ast_node(c))
@@ -220,14 +253,15 @@ func _block_to_ast_node(node: BlockSerialization) -> BlockAST.ASTNode:
220253

221254
func _value_to_ast_value(value_node: ValueBlockSerialization) -> BlockAST.ASTValueNode:
222255
var ast_value_node := BlockAST.ASTValueNode.new()
223-
ast_value_node.data = get_block_definition(value_node.name)
224256

225257
for arg_name in value_node.arguments:
226258
var argument = value_node.arguments[arg_name]
227259
if argument is ValueBlockSerialization:
228260
argument = _value_to_ast_value(argument)
229261
ast_value_node.arguments[arg_name] = argument
230262

263+
ast_value_node.data = get_block_definition(value_node.name, value_node.arguments)
264+
231265
return ast_value_node
232266

233267

0 commit comments

Comments
 (0)