Skip to content

Commit

Permalink
🐛 do not rely on autoloads to avoid startup errors (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbrain authored Jan 6, 2024
1 parent ba53fbe commit 902d480
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 15 deletions.
2 changes: 2 additions & 0 deletions addons/beehave/debug/debugger.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
extends EditorDebuggerPlugin

const DebuggerTab := preload("debugger_tab.gd")
const BeehaveUtils := preload("res://addons/beehave/utils/utils.gd")

var debugger_tab := DebuggerTab.new()
var floating_window: Window
var session: EditorDebuggerSession
Expand Down
4 changes: 4 additions & 0 deletions addons/beehave/debug/debugger_tab.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@tool
class_name BeehaveDebuggerTab extends PanelContainer


const BeehaveUtils := preload("res://addons/beehave/utils/utils.gd")


signal make_floating()

const BeehaveGraphEdit := preload("graph_edit.gd")
Expand Down
4 changes: 4 additions & 0 deletions addons/beehave/debug/frames.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@tool
extends RefCounted


const BeehaveUtils := preload("res://addons/beehave/utils/utils.gd")


const SUCCESS_COLOR := Color("#009944c8")
const NORMAL_COLOR := Color("#15181e")
const FAILURE_COLOR := Color("#cf000f80")
Expand Down
8 changes: 4 additions & 4 deletions addons/beehave/debug/global_debugger.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extends Node

var _registered_trees: Dictionary
var _active_tree: BeehaveTree
var _active_tree


func _enter_tree() -> void:
Expand All @@ -20,7 +20,7 @@ func _on_debug_message(message: String, data: Array) -> bool:


func _set_active_tree(tree_id: int) -> void:
var tree: BeehaveTree = _registered_trees.get(tree_id, null)
var tree = _registered_trees.get(tree_id, null)
if not tree:
return

Expand All @@ -30,9 +30,9 @@ func _set_active_tree(tree_id: int) -> void:
_active_tree._can_send_message = true


func register_tree(tree: BeehaveTree) -> void:
func register_tree(tree) -> void:
_registered_trees[tree.get_instance_id()] = tree


func unregister_tree(tree: BeehaveTree) -> void:
func unregister_tree(tree) -> void:
_registered_trees.erase(tree.get_instance_id())
4 changes: 4 additions & 0 deletions addons/beehave/debug/graph_node.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@tool
extends GraphNode


const BeehaveUtils := preload("res://addons/beehave/utils/utils.gd")


const DEFAULT_COLOR := Color("#dad4cb")

const PORT_TOP_ICON := preload("icons/port_top.svg")
Expand Down
2 changes: 2 additions & 0 deletions addons/beehave/debug/tree_node.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ extends RefCounted
const SIBLING_DISTANCE: float = 20.0
const LEVEL_DISTANCE: float = 40.0

const BeehaveUtils := preload("res://addons/beehave/utils/utils.gd")

var x: float
var y: float
var mod: float
Expand Down
6 changes: 3 additions & 3 deletions addons/beehave/metrics/beehave_global_metrics.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ extends Node

var _tree_count: int = 0
var _active_tree_count: int = 0
var _registered_trees: Array[BeehaveTree] = []
var _registered_trees: Array = []


func _enter_tree() -> void:
Performance.add_custom_monitor("beehave/total_trees", _get_total_trees)
Performance.add_custom_monitor("beehave/total_enabled_trees", _get_total_enabled_trees)


func register_tree(tree: BeehaveTree) -> void:
func register_tree(tree) -> void:
if _registered_trees.has(tree):
return

Expand All @@ -24,7 +24,7 @@ func register_tree(tree: BeehaveTree) -> void:
tree.tree_disabled.connect(_on_tree_disabled)


func unregister_tree(tree: BeehaveTree) -> void:
func unregister_tree(tree) -> void:
if not _registered_trees.has(tree):
return

Expand Down
22 changes: 17 additions & 5 deletions addons/beehave/nodes/beehave_tree.gd
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ signal tree_disabled
custom_monitor = b
if custom_monitor and _process_time_metric_name != '':
Performance.add_custom_monitor(_process_time_metric_name, _get_process_time_metric_value)
BeehaveGlobalMetrics.register_tree(self)
_get_global_metrics().register_tree(self)
else:
if _process_time_metric_name != '':
# Remove tree metric from the engine
Performance.remove_custom_monitor(_process_time_metric_name)
BeehaveGlobalMetrics.unregister_tree(self)
_get_global_metrics().unregister_tree(self)

BeehaveDebuggerMessages.unregister_tree(get_instance_id())

Expand Down Expand Up @@ -138,12 +138,12 @@ func _ready() -> void:
# Register custom metric to the engine
if custom_monitor and not Engine.is_editor_hint():
Performance.add_custom_monitor(_process_time_metric_name, _get_process_time_metric_value)
BeehaveGlobalMetrics.register_tree(self)
_get_global_metrics().register_tree(self)

if Engine.is_editor_hint():
update_configuration_warnings.call_deferred()
else:
BeehaveGlobalDebugger.register_tree(self)
_get_global_debugger().register_tree(self)
BeehaveDebuggerMessages.register_tree(_get_debugger_data(self))

# Randomize at what frames tick() will happen to avoid stutters
Expand Down Expand Up @@ -266,7 +266,7 @@ func _exit_tree() -> void:
if _process_time_metric_name != '':
# Remove tree metric from the engine
Performance.remove_custom_monitor(_process_time_metric_name)
BeehaveGlobalMetrics.unregister_tree(self)
_get_global_metrics().unregister_tree(self)

BeehaveDebuggerMessages.unregister_tree(get_instance_id())

Expand All @@ -291,3 +291,15 @@ func _get_debugger_data(node: Node) -> Dictionary:

func get_class_name() -> Array[StringName]:
return [&"BeehaveTree"]


# required to avoid lifecycle issues on initial load
# due to loading order problems with autoloads
func _get_global_metrics() -> Node:
return get_tree().root.get_node("BeehaveGlobalMetrics")


# required to avoid lifecycle issues on initial load
# due to loading order problems with autoloads
func _get_global_debugger() -> Node:
return get_tree().root.get_node("BeehaveGlobalDebugger")
1 change: 0 additions & 1 deletion addons/beehave/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ func _init():
add_autoload_singleton("BeehaveGlobalDebugger", "debug/global_debugger.gd")
print("Beehave initialized!")


func _enter_tree() -> void:
editor_debugger = BeehaveEditorDebugger.new()
frames = preload("debug/frames.gd").new()
Expand Down
2 changes: 0 additions & 2 deletions addons/beehave/utils/utils.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
@tool
class_name BeehaveUtils


static func get_plugin() -> EditorPlugin:
var tree: SceneTree = Engine.get_main_loop()
Expand Down

0 comments on commit 902d480

Please sign in to comment.