From 93eb572dad319dc3d193f4bcd2399a7c07edfd8e Mon Sep 17 00:00:00 2001 From: Rodrigo Volpe Battistin Date: Thu, 10 Aug 2023 17:35:05 -0300 Subject: [PATCH] Avoid having to declare resource id in every class Get resource_id() from class_name, on all RuleBasedResources Godot doesn't have an easy way to get a custom class name, because get_class() only gets base classes and apperently there is no better way [1], so the solution was to get the source code for the class and find a "class_name" definition [1]: https://github.com/godotengine/godot/issues/21789 Signed-off-by: Rodrigo Volpe Battistin --- .../resources/actions/abstract_action.gd | 5 ++--- .../resources/actions/call_method_action.gd | 1 - .../resources/actions/emit_signal_action.gd | 1 - .../resources/actions/set_property_action.gd | 1 - .../rule_based_godot/resources/matches/abstract_match.gd | 2 -- .../resources/matches/boolean/AND_match.gd | 3 --- .../resources/matches/boolean/OR_match.gd | 3 --- .../resources/matches/boolean/multi_bool_match.gd | 4 ++-- .../resources/matches/datum/area_detection_match.gd | 1 - .../resources/matches/datum/datum_match.gd | 4 ++-- .../resources/matches/datum/distance_match.gd | 1 - .../resources/matches/datum/hierarchy_match.gd | 1 - .../resources/matches/datum/numeric_match.gd | 1 - .../resources/matches/datum/string_match.gd | 1 - addons/rule_based_godot/resources/rule_based_resource.gd | 9 +++++++++ addons/rule_based_godot/resources/rule_set.gd | 4 ++-- 16 files changed, 17 insertions(+), 25 deletions(-) diff --git a/addons/rule_based_godot/resources/actions/abstract_action.gd b/addons/rule_based_godot/resources/actions/abstract_action.gd index eb4ae2c..a13d05e 100644 --- a/addons/rule_based_godot/resources/actions/abstract_action.gd +++ b/addons/rule_based_godot/resources/actions/abstract_action.gd @@ -1,7 +1,6 @@ extends RuleBasedResource class_name AbstractAction -var action_id: StringName var repr_vars: Array[StringName] # variables of json format var Agent_Nodes: bool = true: @@ -121,14 +120,14 @@ func _get_agent_nodes(bindings: Dictionary) -> Array: func json_format() -> String: # ["ID", "?wild"|["groups"]|"agent", vars...] - var string = '["' + action_id + '", "?wild"|["groups"]|"agent"' + var string = '["' + resource_id() + '", "?wild"|["groups"]|"agent"' for variable in repr_vars: string += ', ' + variable return string + ']' func to_json_repr() -> Variant: # ["ID", "?wild"|["groups"]|"agent", vars...] - var json_array = [action_id] + var json_array = [resource_id()] match agent_type: AgentType.PATH: json_array.append(var_to_str(agent_path)) diff --git a/addons/rule_based_godot/resources/actions/call_method_action.gd b/addons/rule_based_godot/resources/actions/call_method_action.gd index 5d82015..f821508 100644 --- a/addons/rule_based_godot/resources/actions/call_method_action.gd +++ b/addons/rule_based_godot/resources/actions/call_method_action.gd @@ -6,7 +6,6 @@ extends AbstractAction @export var arguments: Array func _init(): - action_id = "CallMethod" repr_vars = ["method", "arguments"] func _result_from_agent(agent: Node, bindings: Dictionary) -> Variant: diff --git a/addons/rule_based_godot/resources/actions/emit_signal_action.gd b/addons/rule_based_godot/resources/actions/emit_signal_action.gd index 3862279..fc155f3 100644 --- a/addons/rule_based_godot/resources/actions/emit_signal_action.gd +++ b/addons/rule_based_godot/resources/actions/emit_signal_action.gd @@ -7,7 +7,6 @@ extends AbstractAction @export var arguments: Array = [] # same order as dict func _init(): - action_id = "EmitSignal" repr_vars = ["signal_name", "parameter_to_type", "arguments"] pre_add_signal("signal_name", "parameter_to_type") diff --git a/addons/rule_based_godot/resources/actions/set_property_action.gd b/addons/rule_based_godot/resources/actions/set_property_action.gd index 9dc25dd..d2aebaa 100644 --- a/addons/rule_based_godot/resources/actions/set_property_action.gd +++ b/addons/rule_based_godot/resources/actions/set_property_action.gd @@ -12,7 +12,6 @@ var _property: StringName = "" var _value: Variant = null func _init(): - action_id = "SetProperty" repr_vars = ["property_and_value"] func _result_from_agent(agent: Node, bindings: Dictionary) -> Variant: diff --git a/addons/rule_based_godot/resources/matches/abstract_match.gd b/addons/rule_based_godot/resources/matches/abstract_match.gd index 95292bb..9526c11 100644 --- a/addons/rule_based_godot/resources/matches/abstract_match.gd +++ b/addons/rule_based_godot/resources/matches/abstract_match.gd @@ -2,8 +2,6 @@ class_name AbstractMatch extends RuleBasedResource # Abstract class for condition components -var match_id: StringName # ID for json format - func is_satisfied(bindings: Dictionary) -> bool: push_error("Abstract method") return false diff --git a/addons/rule_based_godot/resources/matches/boolean/AND_match.gd b/addons/rule_based_godot/resources/matches/boolean/AND_match.gd index ad03ca9..7f06c9e 100644 --- a/addons/rule_based_godot/resources/matches/boolean/AND_match.gd +++ b/addons/rule_based_godot/resources/matches/boolean/AND_match.gd @@ -1,9 +1,6 @@ class_name ANDMatch extends MultiBoolMatch -func _init(): - match_id = "AND" - func is_satisfied(bindings: Dictionary) -> bool: if subconditions.is_empty(): print_debug("Empty ANDMatch") diff --git a/addons/rule_based_godot/resources/matches/boolean/OR_match.gd b/addons/rule_based_godot/resources/matches/boolean/OR_match.gd index cca5f3d..a269f3a 100644 --- a/addons/rule_based_godot/resources/matches/boolean/OR_match.gd +++ b/addons/rule_based_godot/resources/matches/boolean/OR_match.gd @@ -1,9 +1,6 @@ class_name ORMatch extends MultiBoolMatch -func _init(): - match_id = "OR" - func is_satisfied(bindings: Dictionary) -> bool: if subconditions.is_empty(): print_debug("Empty ORMatch") diff --git a/addons/rule_based_godot/resources/matches/boolean/multi_bool_match.gd b/addons/rule_based_godot/resources/matches/boolean/multi_bool_match.gd index f5b8d5b..7d38493 100644 --- a/addons/rule_based_godot/resources/matches/boolean/multi_bool_match.gd +++ b/addons/rule_based_godot/resources/matches/boolean/multi_bool_match.gd @@ -11,14 +11,14 @@ func setup(system_node: Node) -> void: func json_format() -> String: # ["ID", [conditions]] - return '["' + match_id + '", [conditions]]' + return '["' + resource_id() + '", [conditions]]' func to_json_repr() -> Variant: # ["ID", [conditions]] var conditions_array := [] for condition in subconditions: conditions_array.append(condition.to_json_repr()) - return [match_id, conditions_array] + return [resource_id(), conditions_array] func build_from_repr(json_repr) -> void: # ["ID", [conditions]] diff --git a/addons/rule_based_godot/resources/matches/datum/area_detection_match.gd b/addons/rule_based_godot/resources/matches/datum/area_detection_match.gd index b1cf3f4..ef9e76e 100644 --- a/addons/rule_based_godot/resources/matches/datum/area_detection_match.gd +++ b/addons/rule_based_godot/resources/matches/datum/area_detection_match.gd @@ -9,7 +9,6 @@ var _area # Area2D or Area3D var _overlapping := [] func _init(): - match_id = "AreaDetection" repr_vars = ["area_path"] Data_Retrieval = false preset_node_path("area_path", "_area") diff --git a/addons/rule_based_godot/resources/matches/datum/datum_match.gd b/addons/rule_based_godot/resources/matches/datum/datum_match.gd index 66743e8..b05933b 100644 --- a/addons/rule_based_godot/resources/matches/datum/datum_match.gd +++ b/addons/rule_based_godot/resources/matches/datum/datum_match.gd @@ -254,7 +254,7 @@ func _get_data(tester_node: Node) -> Variant: ################################ JSON format ################################### func json_format() -> String: # ["ID", ("?data"), vars..., "?wild"|"tester", ("prop"|"method", [args])] - var string = '["' + match_id + '", ("?data")' + var string = '["' + resource_id() + '", ("?data")' for variable in repr_vars: string += ', ' + variable string += ', "?wild"|"tester", ("prop"|"method", [args])]' @@ -262,7 +262,7 @@ func json_format() -> String: func to_json_repr() -> Variant: # ["ID", ("?data"), vars..., "?wild"|"tester", ("prop"|"method", [args])] - var json_array = [match_id] + var json_array = [resource_id()] if retrieval_should_retrieve: json_array.append(var_to_str('?' + retrieval_variable)) diff --git a/addons/rule_based_godot/resources/matches/datum/distance_match.gd b/addons/rule_based_godot/resources/matches/datum/distance_match.gd index b1c6f6a..cc54e0e 100644 --- a/addons/rule_based_godot/resources/matches/datum/distance_match.gd +++ b/addons/rule_based_godot/resources/matches/datum/distance_match.gd @@ -8,7 +8,6 @@ var _source_node: Node @export var max_distance: float func _init(): - match_id = "Distance" repr_vars = ["min_distance", "max_distance", "source_node_path"] preset_node_path("source_node_path", "_source_node") diff --git a/addons/rule_based_godot/resources/matches/datum/hierarchy_match.gd b/addons/rule_based_godot/resources/matches/datum/hierarchy_match.gd index af57263..e1deaa3 100644 --- a/addons/rule_based_godot/resources/matches/datum/hierarchy_match.gd +++ b/addons/rule_based_godot/resources/matches/datum/hierarchy_match.gd @@ -8,7 +8,6 @@ var _source_node: Node func _init(): Data_Retrieval = false - match_id = "Hierarchy" repr_vars = ["source_node_path", "relation"] preset_node_path("source_node_path", "_source_node") diff --git a/addons/rule_based_godot/resources/matches/datum/numeric_match.gd b/addons/rule_based_godot/resources/matches/datum/numeric_match.gd index 9fea6fa..ac852db 100644 --- a/addons/rule_based_godot/resources/matches/datum/numeric_match.gd +++ b/addons/rule_based_godot/resources/matches/datum/numeric_match.gd @@ -7,7 +7,6 @@ extends DatumMatch func _init(): Data_Extraction = true - match_id = "Numeric" repr_vars = ["min_value", "max_value"] func _data_satisfies_match(data: Variant) -> bool: diff --git a/addons/rule_based_godot/resources/matches/datum/string_match.gd b/addons/rule_based_godot/resources/matches/datum/string_match.gd index 9b30944..5bce414 100644 --- a/addons/rule_based_godot/resources/matches/datum/string_match.gd +++ b/addons/rule_based_godot/resources/matches/datum/string_match.gd @@ -6,7 +6,6 @@ extends DatumMatch func _init(): Data_Extraction = true - match_id = "String" repr_vars = ["string_value"] func _data_satisfies_match(data: Variant) -> bool: diff --git a/addons/rule_based_godot/resources/rule_based_resource.gd b/addons/rule_based_godot/resources/rule_based_resource.gd index 09e45d0..6c405e7 100644 --- a/addons/rule_based_godot/resources/rule_based_resource.gd +++ b/addons/rule_based_godot/resources/rule_based_resource.gd @@ -7,6 +7,15 @@ var _system_node: Node func setup(system_node: Node) -> void: _system_node = system_node +func resource_id() -> String: + var source_code: String = get_script().source_code + var start_index: int = source_code.find("class_name") + 11 + var length: int = \ + min(source_code.find("\n", start_index), source_code.find(" ", start_index)) \ + - start_index + var id: String = source_code.substr(start_index, length) + return id.trim_suffix("Match").trim_suffix("Action") + func json_format() -> String: # Abstract method push_error("Abstract method call") diff --git a/addons/rule_based_godot/resources/rule_set.gd b/addons/rule_based_godot/resources/rule_set.gd index 40a8f34..7e344a0 100644 --- a/addons/rule_based_godot/resources/rule_set.gd +++ b/addons/rule_based_godot/resources/rule_set.gd @@ -49,13 +49,13 @@ func add_rules(rules: Array[Rule]) -> void: func remove_rule_by_name(rule_name: StringName) -> void: for rule in rules: - if rule.resource_name == rule_name: + if rule.resource_id() == rule_name: rules.erase(rule) break func get_rule_by_name(rule_name: StringName) -> Rule: for rule in rules: - if rule.resource_name == rule_name: + if rule.resource_id() == rule_name: return rule return null