From 4a4e5d5cf749891c8b30616a80ba7fd925be7e15 Mon Sep 17 00:00:00 2001 From: Jian-Hong Pan Date: Mon, 11 Nov 2024 18:27:29 +0800 Subject: [PATCH] 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, visit the block code node's parent node to finding the property by _get_parent_node_property_info() to have the type. https://phabricator.endlessm.com/T35649 --- .../block_script_serialization.gd | 76 +++++++++++++++---- 1 file changed, 62 insertions(+), 14 deletions(-) diff --git a/addons/block_code/serialization/block_script_serialization.gd b/addons/block_code/serialization/block_script_serialization.gd index e0847f02..67939f49 100644 --- a/addons/block_code/serialization/block_script_serialization.gd +++ b/addons/block_code/serialization/block_script_serialization.gd @@ -72,7 +72,7 @@ func instantiate_block(block_definition: BlockDefinition) -> Block: func instantiate_block_by_name(block_name: String) -> Block: - var block_definition := get_block_definition(block_name) + var block_definition := get_block_definition(block_name, {}) if block_definition == null: push_warning("Cannot find a block definition for %s" % block_name) @@ -81,24 +81,29 @@ func instantiate_block_by_name(block_name: String) -> Block: return instantiate_block(block_definition) -func get_block_definition(block_name: String) -> BlockDefinition: +func get_block_definition(block_name: String, arguments: Dictionary) -> BlockDefinition: var split := block_name.split(":", true, 1) + var block_definition: BlockDefinition if len(split) > 1: return _get_parameter_block_definition(split[0], split[1]) - var block_definition = _get_base_block_definition(block_name) + block_definition = _get_base_block_definition(block_name) + if block_definition != null: + return block_definition - if block_definition == null: - # FIXME: This is a workaround for old-style output block references. - # These were generated ahead of time using a block name that has - # a "_" before the parameter name. Now, these parameter blocks - # are generated on demand for any block name containing a ":". - # Please remove this fallback when it is no longer necessary. - split = block_name.rsplit("_", true, 1) - return _get_parameter_block_definition(split[0], split[1]) + var property_value = arguments.get("value", null) + block_definition = _get_obj_property_block_definition(block_name, property_value) + if block_definition != null: + return block_definition - return block_definition + # FIXME: This is a workaround for old-style output block references. + # These were generated ahead of time using a block name that has + # a "_" before the parameter name. Now, these parameter blocks + # are generated on demand for any block name containing a ":". + # Please remove this fallback when it is no longer necessary. + split = block_name.rsplit("_", true, 1) + return _get_parameter_block_definition(split[0], split[1]) func _get_base_block_definition(block_name: String) -> BlockDefinition: @@ -135,6 +140,47 @@ func _get_parameter_block_definition(block_name: String, parameter_name: String) return block_definition +func _get_obj_property_block_definition(block_name: String, property_value: Variant) -> BlockDefinition: + var block_definition: BlockDefinition + var variable: VariableDefinition + var property_name: String + var is_getter = true + + if block_name.begins_with("get_var_"): + property_name = block_name.get_slice("get_var_", 1) + elif block_name.begins_with("set_var_"): + property_name = block_name.get_slice("set_var_", 1) + is_getter = false + else: + return null + + # Getter block needs the property's variant type information by visiting the + # block_code_node's parent node because the type is not saved as a key of + # the resource in the scene file + var property_info := _get_parent_node_property_info(property_name) + if not property_info.has("type"): + return null + + if is_getter: + variable = VariableDefinition.new(property_name, property_info["type"]) + block_definition = BlocksCatalog.get_property_getter_block_definition(variable) + else: + variable = VariableDefinition.new(property_name, property_info["type"]) + block_definition = BlocksCatalog.get_property_setter_block_definition(variable) + + return block_definition + + +func _get_parent_node_property_info(property_name: String) -> Dictionary: + var properties := block_code_node.get_parent().get_property_list() + + for property in properties: + if property["name"] == property_name: + return property + + return {} + + func _update_block_definitions(): _available_blocks.clear() _available_blocks.append_array(_get_inherited_block_definitions()) @@ -204,7 +250,6 @@ func _tree_to_ast(tree: BlockSerializationTree) -> BlockAST: func _block_to_ast_node(node: BlockSerialization) -> BlockAST.ASTNode: var ast_node := BlockAST.ASTNode.new() - ast_node.data = get_block_definition(node.name) for arg_name in node.arguments: var argument = node.arguments[arg_name] @@ -212,6 +257,8 @@ func _block_to_ast_node(node: BlockSerialization) -> BlockAST.ASTNode: argument = _value_to_ast_value(argument) ast_node.arguments[arg_name] = argument + ast_node.data = get_block_definition(node.name, ast_node.arguments) + var children: Array[BlockAST.ASTNode] for c in node.children: children.append(_block_to_ast_node(c)) @@ -222,7 +269,6 @@ func _block_to_ast_node(node: BlockSerialization) -> BlockAST.ASTNode: func _value_to_ast_value(value_node: ValueBlockSerialization) -> BlockAST.ASTValueNode: var ast_value_node := BlockAST.ASTValueNode.new() - ast_value_node.data = get_block_definition(value_node.name) for arg_name in value_node.arguments: var argument = value_node.arguments[arg_name] @@ -230,6 +276,8 @@ func _value_to_ast_value(value_node: ValueBlockSerialization) -> BlockAST.ASTVal argument = _value_to_ast_value(argument) ast_value_node.arguments[arg_name] = argument + ast_value_node.data = get_block_definition(value_node.name, value_node.arguments) + return ast_value_node