Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @tool
- extends CanvasLayer
- #READ JSON FILE
- @export_category("PATH TO JSON DIALOGUE")
- @export var json_path = "json path here"
- @export_category("PORTRAITS")
- @export var character_portraits : Array[Texture2D]
- #ONREADY
- @onready var text_box : RichTextLabel = $Control/MarginContainer/DialogueBox/TextBox
- @onready var name_box : RichTextLabel = $Control/MarginContainer/DialogueBox/NameBox/NameTextBox
- @onready var portrait : TextureRect = $Control/Portrait
- @onready var animation_player : AnimationPlayer = $AnimationPlayer
- #VARS
- var dialogue = [] #this is where we will store the data of our json
- var index : int = 0 #this is the current line of dialogue in our json (starting from 0 'til the last)
- var id : int #ID of the character speaking, we use their numbers to match the portraits ARRAY
- var current_color : String #Color of the text
- #SIGNAL
- signal write_dialogue
- func _ready():
- #Use this function whenever you want your dialogue to start.
- start_dialogue(json_path)
- func _unhandled_input(event):
- #We advance the dialogue with left click or a key
- #You will need to add "interact" to your input map in project settings
- if event.is_action_pressed("interact"):
- advance_dialogue()
- #LOAD THE JSON FILE INTO A READABLE FORMAT FOR GODOT
- func load_dialogue(path : String):
- var json_as_text = FileAccess.get_file_as_string(path)
- var json_as_dict = JSON.parse_string(json_as_text)
- if json_as_dict:
- return json_as_dict
- #STORE THE JSON FILES DATA INTO OUR 'DIALOGUE' ARRAY
- func start_dialogue(path : String):
- emit_signal("write_dialogue")
- index = 0
- if FileAccess.file_exists(json_path):
- dialogue = load_dialogue(path)
- #We access the data of our json, and add it to our text box and name box
- text_box.text = dialogue[index]['text']
- name_box.text = dialogue[index]['name']
- id = dialogue[index]['id']
- check_for_color()
- check_for_portrait()
- else : pass
- func advance_dialogue():
- #If our text is entirely visible (meaning is done writing) we can move on to the next line
- match text_box.visible_ratio == 1:
- true:
- emit_signal("write_dialogue")
- index += 1
- #We check if we still have lines of dialogue left, if we do, we continue
- if index < dialogue.size():
- text_box.text = dialogue[index]['text']
- name_box.text = dialogue[index]['name']
- id = dialogue[index]['id']
- current_color = dialogue[index]['Color']
- check_for_portrait()
- check_for_color()
- #If we are at the last line of dialogue, the scene deletes itself and we end.
- else :
- queue_free()
- #WE CHECK THE ID GIVEN IN THE JSON, AND CHANGE THE PORTRAIT BASED ON THE INT NUMBER GIVEN
- func check_for_portrait():
- match id:
- 0: portrait.texture = null #I ALWAYS MAKE 0 NULL, JUST IN CASE THERE ARE NO PORTRAITS DISPLAYED
- 1: portrait.texture = character_portraits[1]
- 2: portrait.texture = character_portraits[2]
- 3: portrait.texture = character_portraits[3]
- 4: portrait.texture = character_portraits[4]
- 5: portrait.texture = character_portraits[5]
- #WE CHECK THE COLOR GIVEN IN OUR JSON, AND CHANGE THE TEXT BASED ON THE STRING PROVIDED
- func check_for_color():
- match current_color:
- 'white':
- text_box.add_theme_color_override("default_color", Color.WHITE)
- 'red':
- text_box.add_theme_color_override("default_color", Color.RED)
- 'blue':
- text_box.add_theme_color_override("default_color", Color.BLUE)
- #WE START THE ANIMATION THAT WILL GIVE OUR TEXT A TYPING EFFECT
- func _on_write_dialogue():
- animation_player.play("WriteDialogue")
- #YOU CAN ADD AN ARROW THAT INDICATES YOUR DIALOGUE IS DONE TYPING HERE
- #I DON'T HAVE ONE FOR THE EXAMPLE SO I WILL LEAVE IT BLANK
- func _on_animation_player_animation_finished(anim_name):
- #Example:
- #end_arrow_texture.visible = true
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement