Advertisement
Cevelean

Untitled

Feb 8th, 2024
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 3.71 KB | Gaming | 0 0
  1. @tool
  2. extends CanvasLayer
  3. #READ JSON FILE
  4. @export_category("PATH TO JSON DIALOGUE")
  5. @export var json_path = "json path here"
  6. @export_category("PORTRAITS")
  7. @export var character_portraits : Array[Texture2D]
  8. #ONREADY
  9. @onready var text_box : RichTextLabel = $Control/MarginContainer/DialogueBox/TextBox
  10. @onready var name_box : RichTextLabel = $Control/MarginContainer/DialogueBox/NameBox/NameTextBox
  11. @onready var portrait : TextureRect = $Control/Portrait
  12. @onready var animation_player : AnimationPlayer = $AnimationPlayer
  13. #VARS
  14. var dialogue = []   #this is where we will store the data of our json
  15. var index : int = 0 #this is the current line of dialogue in our json (starting from 0 'til the last)
  16. var id : int    #ID of the character speaking, we use their numbers to match the portraits ARRAY
  17. var current_color : String  #Color of the text
  18. #SIGNAL
  19. signal write_dialogue
  20.  
  21. func _ready():
  22.     #Use this function whenever you want your dialogue to start.
  23.     start_dialogue(json_path)
  24.  
  25. func _unhandled_input(event):
  26.     #We advance the dialogue with left click or a key
  27.     #You will need to add "interact" to your input map in project settings
  28.     if event.is_action_pressed("interact"):
  29.         advance_dialogue()
  30.  
  31. #LOAD THE JSON FILE INTO A READABLE FORMAT FOR GODOT
  32. func load_dialogue(path : String):
  33.     var json_as_text = FileAccess.get_file_as_string(path)
  34.     var json_as_dict = JSON.parse_string(json_as_text)
  35.     if json_as_dict:
  36.         return json_as_dict
  37.  
  38. #STORE THE JSON FILES DATA INTO OUR 'DIALOGUE' ARRAY
  39. func start_dialogue(path : String):
  40.     emit_signal("write_dialogue")
  41.     index = 0
  42.     if FileAccess.file_exists(json_path):
  43.         dialogue = load_dialogue(path)
  44.         #We access the data of our json, and add it to our text box and name box
  45.         text_box.text = dialogue[index]['text']
  46.         name_box.text = dialogue[index]['name']
  47.         id = dialogue[index]['id']
  48.         check_for_color()
  49.         check_for_portrait()
  50.     else : pass
  51.  
  52. func advance_dialogue():
  53.     #If our text is entirely visible (meaning is done writing) we can move on to the next line
  54.     match text_box.visible_ratio == 1:
  55.         true:
  56.             emit_signal("write_dialogue")
  57.             index += 1
  58.             #We check if we still have lines of dialogue left, if we do, we continue
  59.             if index < dialogue.size():
  60.                 text_box.text = dialogue[index]['text']
  61.                 name_box.text = dialogue[index]['name']
  62.                 id = dialogue[index]['id']
  63.                 current_color = dialogue[index]['Color']
  64.                 check_for_portrait()
  65.                 check_for_color()
  66.             #If we are at the last line of dialogue, the scene deletes itself and we end.
  67.             else :
  68.                 queue_free()
  69.  
  70. #WE CHECK THE ID GIVEN IN THE JSON, AND CHANGE THE PORTRAIT BASED ON THE INT NUMBER GIVEN
  71. func check_for_portrait():
  72.     match id:
  73.         0: portrait.texture = null  #I ALWAYS MAKE 0 NULL, JUST IN CASE THERE ARE NO PORTRAITS DISPLAYED
  74.         1: portrait.texture = character_portraits[1]
  75.         2: portrait.texture = character_portraits[2]
  76.         3: portrait.texture = character_portraits[3]
  77.         4: portrait.texture = character_portraits[4]
  78.         5: portrait.texture = character_portraits[5]
  79.  
  80. #WE CHECK THE COLOR GIVEN IN OUR JSON, AND CHANGE THE TEXT BASED ON THE STRING PROVIDED
  81. func check_for_color():
  82.     match current_color:
  83.         'white':
  84.             text_box.add_theme_color_override("default_color", Color.WHITE)
  85.         'red':
  86.             text_box.add_theme_color_override("default_color", Color.RED)
  87.         'blue':
  88.             text_box.add_theme_color_override("default_color", Color.BLUE)
  89.  
  90. #WE START THE ANIMATION THAT WILL GIVE OUR TEXT A TYPING EFFECT
  91. func _on_write_dialogue():
  92.     animation_player.play("WriteDialogue")
  93.  
  94. #YOU CAN ADD AN ARROW THAT INDICATES YOUR DIALOGUE IS DONE TYPING HERE
  95. #I DON'T HAVE ONE FOR THE EXAMPLE SO I WILL LEAVE IT BLANK
  96. func _on_animation_player_animation_finished(anim_name):
  97.     #Example:
  98.     #end_arrow_texture.visible = true
  99.     pass
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement