dialogue_player.gd 1012 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. extends Node
  2. signal dialogue_started
  3. signal dialogue_finished
  4. @export_file("*.json") var dialogue_file: String
  5. var dialogue_keys := []
  6. var dialogue_name := ""
  7. var current := 0
  8. var dialogue_text := ""
  9. func start_dialogue() -> void:
  10. dialogue_started.emit()
  11. current = 0
  12. index_dialogue()
  13. dialogue_text = dialogue_keys[current].text
  14. dialogue_name = dialogue_keys[current].name
  15. func next_dialogue() -> void:
  16. current += 1
  17. if current == dialogue_keys.size():
  18. dialogue_finished.emit()
  19. return
  20. dialogue_text = dialogue_keys[current].text
  21. dialogue_name = dialogue_keys[current].name
  22. func index_dialogue() -> void:
  23. var dialogue: Dictionary = load_dialogue(dialogue_file)
  24. dialogue_keys.clear()
  25. for key: String in dialogue:
  26. dialogue_keys.append(dialogue[key])
  27. func load_dialogue(file_path: String) -> Dictionary:
  28. var file := FileAccess.open(file_path, FileAccess.READ)
  29. if file:
  30. var test_json_conv := JSON.new()
  31. test_json_conv.parse(file.get_as_text())
  32. return test_json_conv.data
  33. return {}