-
-
Save salvamomo/b5974b1a301c887b059e647fbbf4d7e4 to your computer and use it in GitHub Desktop.
Godot debug/interpreter console
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 Control | |
# Mostly inspired off https://www.youtube.com/watch?v=80cwYGvKB9U | |
# and https://github.com/godotengine/godot/issues/8003 | |
onready var input_node: LineEdit = $Input | |
onready var output_node: TextEdit = $Output | |
# Dump some commands in here for convenience | |
var history = ["get_tree().get_root().get_node('Main')"] | |
var current_line = history.size() | |
# Called when the node enters the scene tree for the first time. | |
func _ready(): | |
input_node.grab_focus() | |
func _input(event): | |
if event.is_action_pressed("toggle_console"): | |
accept_event() | |
get_tree().paused = false | |
queue_free() | |
elif event is InputEventKey and event.is_pressed(): | |
if event.scancode == KEY_UP: | |
goto_history(-1) | |
if event.scancode == KEY_DOWN: | |
goto_history(1) | |
func goto_history(offset): | |
current_line = clamp(current_line + offset, 0, history.size() - 1) | |
input_node.text = history[current_line] | |
input_node.call_deferred("set_cursor_position", 9999) | |
func output_text(text: String): | |
output_node.text = str( | |
output_node.text, | |
"" if not output_node.text else "\n", | |
text) | |
output_node.set_v_scroll(9999999) | |
func eval_code(expr): | |
var script = GDScript.new() | |
script.source_code += "extends Node\n" | |
script.source_code += "func _eval():\n" | |
script.source_code += "\n\treturn " + expr | |
script.reload() | |
var script_instance = script.new() | |
add_child(script_instance) | |
return script_instance._eval() | |
func process_command(expr): | |
var res = eval_code(expr) | |
output_text(expr) | |
output_text(str(res)) | |
func _on_Input_text_entered(new_text): | |
input_node.clear() | |
process_command(new_text) | |
history.append(new_text) | |
current_line = history.size() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment