dialogue_player.gd 977 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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():
  10. emit_signal("dialogue_started")
  11. current = 0
  12. index_dialogue()
  13. dialogue_text = dialogue_keys[current].text
  14. dialogue_name = dialogue_keys[current].name
  15. func next_dialogue():
  16. current += 1
  17. if current == dialogue_keys.size():
  18. emit_signal("dialogue_finished")
  19. return
  20. dialogue_text = dialogue_keys[current].text
  21. dialogue_name = dialogue_keys[current].name
  22. func index_dialogue():
  23. var dialogue = load_dialogue(dialogue_file)
  24. dialogue_keys.clear()
  25. for key in dialogue:
  26. dialogue_keys.append(dialogue[key])
  27. func load_dialogue(file_path):
  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. var dialogue = test_json_conv.get_data()
  33. return dialogue