debug.gd 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. extends Label
  2. # Displays some useful debug information in a Label.
  3. @onready var player = $"../Player"
  4. @onready var voxel_world = $"../VoxelWorld"
  5. func _process(_delta):
  6. if Input.is_action_just_pressed(&"debug"):
  7. visible = not visible
  8. text = "Position: " + _vector_to_string_appropriate_digits(player.transform.origin)
  9. text += "\nEffective render distance: " + str(voxel_world.effective_render_distance)
  10. text += "\nLooking: " + _cardinal_string_from_radians(player.transform.basis.get_euler().y)
  11. text += "\nMemory: " + "%3.0f" % (OS.get_static_memory_usage() / 1048576.0) + " MiB"
  12. text += "\nFPS: " + str(Engine.get_frames_per_second())
  13. # Avoids the problem of showing more digits than needed or available.
  14. func _vector_to_string_appropriate_digits(vector):
  15. var factors = [1000, 1000, 1000]
  16. for i in range(3):
  17. if abs(vector[i]) > 4096:
  18. factors[i] = factors[i] / 10
  19. if abs(vector[i]) > 65536:
  20. factors[i] = factors[i] / 10
  21. if abs(vector[i]) > 524288:
  22. factors[i] = factors[i] / 10
  23. return "(" + \
  24. str(round(vector.x * factors[0]) / factors[0]) + ", " + \
  25. str(round(vector.y * factors[1]) / factors[1]) + ", " + \
  26. str(round(vector.z * factors[2]) / factors[2]) + ")"
  27. # Expects a rotation where 0 is North, on the range -PI to PI.
  28. func _cardinal_string_from_radians(angle):
  29. if angle > TAU * 3 / 8:
  30. return "South"
  31. if angle < -TAU * 3 / 8:
  32. return "South"
  33. if angle > TAU * 1 / 8:
  34. return "West"
  35. if angle < -TAU * 1 / 8:
  36. return "East"
  37. return "North"