Skip to content

Instantly share code, notes, and snippets.

@xdl
Created June 11, 2020 23:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xdl/14ea087708b91f97988c42eecf6d8c3e to your computer and use it in GitHub Desktop.
Save xdl/14ea087708b91f97988c42eecf6d8c3e to your computer and use it in GitHub Desktop.
Godot debug/interpreter console
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