Skip to content

Commit

Permalink
@tool functionality (#5)
Browse files Browse the repository at this point in the history
It is now possible to create a @tool script and call LogDuck #4
  • Loading branch information
ZeeWeasel authored Jul 10, 2024
1 parent a3e248c commit 13510f3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
18 changes: 12 additions & 6 deletions addons/LogDuck/LogDuck.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@tool
extends LogDuckSettings
const VERSION = "v0.9"
const VERSION = "v0.9.1"

## Use LogDuck.d(), .e(), or .w() for logging debug, error, or warning messages.
## Supports up to 6 arguments (or 7, if not starting with a string message).
Expand Down Expand Up @@ -100,6 +101,8 @@ func argument_to_string(arg) -> String:


func extract_class_name_from_gdscript(path) -> String:
if path == null: return "LogDuck" #

var file = FileAccess.open(path, FileAccess.READ)
if not file:
class_name_dict[path] = "File Open Error"
Expand Down Expand Up @@ -131,11 +134,14 @@ func extract_class_name_from_gdscript(path) -> String:


func _output(level : LogLevel, msg, arg1, arg2, arg3, arg4, arg5, arg6):

var frame : Array = [
str(stack_frame()['source']),
str(stack_frame()['line']),
str(stack_frame()['function'])]
var frame : Array
if not Engine.is_editor_hint():
frame = [
str(stack_frame()['source']),
str(stack_frame()['line']),
str(stack_frame()['function'])]
else: # Since this is called inside the editor, get_stack() returns []
frame = [null,null,null]

var _class : String

Expand Down
2 changes: 2 additions & 0 deletions addons/LogDuck/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ extends EditorPlugin
## Adjust the singleton name if you prefer something more classic like "Log"
const SingletonName : String = "LogDuck"

@onready var LogDuckSingleton = get_node("/root/LogDuck");

func _enter_tree():
add_autoload_singleton(SingletonName, "res://addons/LogDuck/LogDuck.gd")
7 changes: 7 additions & 0 deletions test_scene.gd
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
@tool
extends Control
class_name TestScene

@onready var debug_msg_line_edit: LineEdit = $CenterContainer/VBoxContainer/LineEdit
@onready var check_box: CheckBox = $CenterContainer/VBoxContainer/CheckBox
@onready var checkbox_rich_output: CheckBox = $"CenterContainer/VBoxContainer/Checkbox Rich Output"

const SingletonName : String = "LogDuck"

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
LogDuck.print_rich = checkbox_rich_output.button_pressed
if Engine.is_editor_hint():
LogDuck.d("Calling LogDuck in the editor / inside @tool script");
else:
LogDuck.d("Calling LogDuck during runtime");

func _on_button_debug_pressed() -> void:
LogDuck.print_rich = checkbox_rich_output.button_pressed
Expand Down

0 comments on commit 13510f3

Please sign in to comment.