Skip to content

Commit

Permalink
Avoid having to declare resource id in every class
Browse files Browse the repository at this point in the history
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]: godotengine/godot#21789

Signed-off-by: Rodrigo Volpe Battistin <[email protected]>
  • Loading branch information
rvbatt committed Aug 10, 2023
1 parent 770969f commit 93eb572
Show file tree
Hide file tree
Showing 16 changed files with 17 additions and 25 deletions.
5 changes: 2 additions & 3 deletions addons/rule_based_godot/resources/actions/abstract_action.gd
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions addons/rule_based_godot/resources/matches/abstract_match.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
3 changes: 0 additions & 3 deletions addons/rule_based_godot/resources/matches/boolean/OR_match.gd
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,15 @@ 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])]'
return 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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions addons/rule_based_godot/resources/rule_based_resource.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions addons/rule_based_godot/resources/rule_set.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 93eb572

Please sign in to comment.