Last active
June 15, 2020 12:00
-
-
Save jonchun/dec665e40c085a5194989da60c81a6db to your computer and use it in GitHub Desktop.
Custom GDScript debug print statement that lets you know which file/line the print is being called from and also has a rate limit feature which is useful to use in _physics_process/_process(). I like to add Debug.gd as an Autoload script and name it "D". I can then just write D.print("Hello world", 1.0) in my code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
var _rate_limits: = {} | |
func print(val, rate_limit: = 0.0): | |
# rate_limit is how often to print in seconds. | |
var stack = get_stack() | |
var source_file = stack[1].source | |
var source_function = stack[1].function | |
var source_line = stack[1].line | |
var source_name = source_file.rsplit("/", true, 1)[-1] | |
var identifier = "%s|%s|%s" % [source_file, source_function, source_line] | |
var formatted = "[%s:%s] %s" % [source_name, source_line, val] | |
# Uncomment below if you want it to be formatted exactly like GDScript's built-in print_debug() statement | |
# formatted = "%s\n At: %s:%s:%s()" % [val, source_file, source_line, source_function] | |
var last_printed = _rate_limits.get(identifier, -INF) | |
if OS.get_ticks_msec() - last_printed > rate_limit * 1000: | |
print(formatted) | |
if rate_limit > 0: | |
_rate_limits[identifier] = OS.get_ticks_msec() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: THIS IS FOR DEBUG USE ONLY. DO NOT LEAVE THIS IN YOUR FINAL GAME! See GDQuest's tweet.