No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DialogicClass.gd 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. extends Node
  2. ## Exposed and safe to use methods for Dialogic
  3. ## See documentation here:
  4. ## https://github.com/coppolaemilio/dialogic
  5. ## ### /!\ ###
  6. ## Do not use methods from other classes as it could break the plugin's integrity
  7. ## ### /!\ ###
  8. ##
  9. ## Trying to follow this documentation convention: https://github.com/godotengine/godot/pull/41095
  10. class_name Dialogic
  11. ## Starts the dialog for the given timeline and returns a Dialog node.
  12. ## You must then add it manually to the scene to display the dialog.
  13. ##
  14. ## Example:
  15. ## var new_dialog = Dialogic.start('Your Timeline Name Here')
  16. ## add_child(new_dialog)
  17. ##
  18. ## This is exactly the same as using the editor:
  19. ## you can drag and drop the scene located at /addons/dialogic/Dialog.tscn
  20. ## and set the current timeline via the inspector.
  21. ##
  22. ## @param timeline The timeline to load. You can provide the timeline name or the filename.
  23. ## @param reset_saves True to reset dialogic saved data such as definitions.
  24. ## @param dialog_scene_path If you made a custom Dialog scene or moved it from its default path, you can specify its new path here.
  25. ## @param debug_mode Debug is disabled by default but can be enabled if needed.
  26. ## @returns A Dialog node to be added into the scene tree.
  27. static func start(timeline: String, reset_saves: bool=true, dialog_scene_path: String="res://addons/dialogic/Dialog.tscn", debug_mode: bool=false):
  28. var dialog: = load(dialog_scene_path)
  29. var d = dialog.instance()
  30. d.reset_saves = reset_saves
  31. d.debug_mode = debug_mode
  32. if not timeline.empty():
  33. for t in DialogicUtil.get_timeline_list():
  34. if t['name'] == timeline or t['file'] == timeline:
  35. d.timeline = t['file']
  36. return d
  37. d.dialog_script = {
  38. "events":[{"character":"","portrait":"",
  39. "text":"[Dialogic Error] Loading dialog [color=red]" + timeline + "[/color]. It seems like the timeline doesn't exists. Maybe the name is wrong?"}]
  40. }
  41. return d
  42. ## Same as the start method above, but using the last timeline saved.
  43. ##
  44. ## @param initial_timeline The timeline to load in case no save is found.
  45. ## @param dialog_scene_path If you made a custom Dialog scene or moved it from its default path, you can specify its new path here.
  46. ## @param debug_mode Debug is disabled by default but can be enabled if needed.
  47. ## @returns A Dialog node to be added into the scene tree.
  48. static func start_from_save(initial_timeline: String, dialog_scene_path: String="res://addons/dialogic/Dialog.tscn", debug_mode: bool=false):
  49. var current := get_current_timeline()
  50. if current.empty():
  51. current = initial_timeline
  52. return start(current, false, dialog_scene_path, debug_mode)
  53. ## Gets default values for definitions.
  54. ##
  55. ## @returns Dictionary in the format {'variables': [], 'glossary': []}
  56. static func get_default_definitions() -> Dictionary:
  57. return DialogicSingleton.get_default_definitions()
  58. ## Gets currently saved values for definitions.
  59. ##
  60. ## @returns Dictionary in the format {'variables': [], 'glossary': []}
  61. static func get_definitions() -> Dictionary:
  62. return DialogicSingleton.get_definitions()
  63. ## Save current definitions to the filesystem.
  64. ## Definitions are automatically saved on timeline start/end
  65. ##
  66. ## @returns Error status, OK if all went well
  67. static func save_definitions():
  68. # Always try to save as much as possible.
  69. var err1 = DialogicSingleton.save_definitions()
  70. var err2 = DialogicSingleton.save_state()
  71. # Try to combine the two error states in a way that makes sense.
  72. return err1 if err1 != OK else err2
  73. ## Sets whether to use Dialogic's built-in autosave functionality.
  74. static func set_autosave(save: bool) -> void:
  75. DialogicSingleton.set_autosave(save);
  76. ## Gets whether to use Dialogic's built-in autosave functionality.
  77. static func get_autosave() -> bool:
  78. return DialogicSingleton.get_autosave();
  79. ## Resets data to default values. This is the same as calling start with reset_saves to true
  80. static func reset_saves():
  81. DialogicSingleton.init(true)
  82. ## Gets the value for the variable with the given name.
  83. ## The returned value is a String but can be easily converted into a number
  84. ## using Godot built-in methods:
  85. ## [`is_valid_float`](https://docs.godotengine.org/en/stable/classes/class_string.html#class-string-method-is-valid-float)
  86. ## [`float()`](https://docs.godotengine.org/en/stable/classes/class_float.html#class-float-method-float).
  87. ##
  88. ## @param name The name of the variable to find.
  89. ## @returns The variable's value as string, or an empty string if not found.
  90. static func get_variable(name: String) -> String:
  91. return DialogicSingleton.get_variable(name)
  92. ## Sets the value for the variable with the given name.
  93. ## The given value will be converted to string using the
  94. ## [`str()`](https://docs.godotengine.org/en/stable/classes/class_string.html) function.
  95. ##
  96. ## @param name The name of the variable to edit.
  97. ## @param value The value to set the variable to.
  98. static func set_variable(name: String, value) -> void:
  99. DialogicSingleton.set_variable(name, value)
  100. ## Gets the glossary data for the definition with the given name.
  101. ## Returned format:
  102. ## { title': '', 'text' : '', 'extra': '' }
  103. ##
  104. ## @param name The name of the glossary to find.
  105. ## @returns The glossary data as a Dictionary.
  106. ## A structure with empty strings is returned if the glossary was not found.
  107. static func get_glossary(name: String) -> Dictionary:
  108. return DialogicSingleton.get_glossary(name)
  109. ## Sets the data for the glossary of the given name.
  110. ##
  111. ## @param name The name of the glossary to edit.
  112. ## @param title The title to show in the information box.
  113. ## @param text The text to show in the information box.
  114. ## @param extra The extra information at the bottom of the box.
  115. static func set_glossary(name: String, title: String, text: String, extra: String) -> void:
  116. DialogicSingleton.set_glossary(name, title, text, extra)
  117. ## Gets the currently saved timeline.
  118. ## Timeline saves are set on timeline start, and cleared on end.
  119. ## This means you can keep track of timeline changes and detect when the dialog ends.
  120. ##
  121. ## @returns The current timeline filename, or an empty string if none was saved.
  122. static func get_current_timeline() -> String:
  123. return DialogicSingleton.get_current_timeline()
  124. ## Sets the currently saved timeline.
  125. ## Use this if you disabled current timeline autosave and want to control it yourself
  126. ##
  127. ## @param timelinie The new timeline to save.
  128. static func set_current_timeline(new_timeline: String) -> String:
  129. return DialogicSingleton.set_current_timeline(new_timeline)
  130. ## Export the current Dialogic state.
  131. ## This can be used as part of your own saving mechanism if you have one. If you use this,
  132. ## you should also disable autosaving.
  133. ##
  134. ## @return A dictionary of data that can be later provided to import().
  135. static func export() -> Dictionary:
  136. return DialogicSingleton.export()
  137. ## Import a Dialogic state.
  138. ## This can be used as part of your own saving mechanism if you have one. If you use this,
  139. ## you should also disable autosaving.
  140. ##
  141. ## @param data A dictionary of data as created by export().
  142. static func import(data: Dictionary) -> void:
  143. DialogicSingleton.import(data)