Advertisement
Dieton

debug_console.gd

Sep 30th, 2024
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 1.85 KB | Gaming | 0 0
  1. extends Control
  2.  
  3. @export var is_active = true # Set this to true if you want to show the terminal by default
  4.  
  5. var command_history = []
  6. var current_input = ""
  7. var max_lines = 50 # Max number of lines to display
  8.  
  9. @onready var output_box = $OutputText
  10. @onready var input_box = $InputText
  11.  
  12. func _ready():
  13.     hide() # Hide the terminal by default
  14.     input_box.connect("text_entered", Callable(self, "_on_input_entered"))
  15.    
  16.     if is_active:
  17.         show()
  18.  
  19. func _process(_delta):
  20.     # Toggle the console with the key `
  21.     if Input.is_action_just_pressed("ui_debug_console"):
  22.         if is_visible():
  23.             hide()
  24.         else:
  25.             show()
  26.    
  27.     if Input.is_action_just_pressed("enter_key"):
  28.         _on_input_entered(input_box.text)
  29.  
  30. # Add a message to the debug output
  31. func log_message(message: String):
  32.     command_history.append(message)
  33.     if command_history.size() > max_lines:
  34.         command_history.pop_front()
  35.     _update_output()
  36.  
  37. # Handle the input command and perform actions
  38. func _on_input_entered(new_command: String):
  39.     log_message("> " + new_command)  # Log the command
  40.     input_box.text = ""  # Clear input after enter
  41.     _process_command(new_command)  # Process the entered command
  42.  
  43. # Update the output box with new log messages
  44. func _update_output():
  45.     output_box.text = ""
  46.     for line in command_history:
  47.         output_box.text += line + "\n"
  48.  
  49. # Example of how to process entered commands
  50. func _process_command(command: String):
  51.     var args = command.split(" ")
  52.  
  53.     match args[0]:
  54.         "print":
  55.             if args.size() > 1:
  56.                 var message = ""
  57.                 for i in range(args.size()):
  58.                     if i > 0:
  59.                         message += " "
  60.                     message += args[i]
  61.                 log_message(message)
  62.         "help":
  63.             log_message("Available commands: print, help, quit")
  64.         "quit":
  65.             get_tree().quit()
  66.         _:
  67.             log_message("Unknown command: " + command)
  68.  
  69. func _on_input_entered_pressed() -> void:
  70.     _on_input_entered(input_box.text)
  71.  
Tags: Godot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement