diff --git a/MyObsidianVault/Godot_Training/2D/Tutorial Game 4 - Telling a story/M7.L6 - Animating the text and playing sounds/1.) using tweens for animation.md b/MyObsidianVault/Godot_Training/2D/Tutorial Game 4 - Telling a story/M7.L6 - Animating the text and playing sounds/1.) using tweens for animation.md new file mode 100644 index 0000000..e85372a --- /dev/null +++ b/MyObsidianVault/Godot_Training/2D/Tutorial Game 4 - Telling a story/M7.L6 - Animating the text and playing sounds/1.) using tweens for animation.md @@ -0,0 +1,19 @@ +![[Pasted image 20240626083938.png]] + +`Do you remember how to find the name of the property in code? You can hover over the property in the editor, and a tooltip will show you the code name.` + +```` +## Draws the current text to the rich text element +func show_text() -> void: + var current_item := dialogue_items[current_item_index] + rich_text_label.text = current_item + rich_text_label.visible_ratio = 0.0 # <--rich +```` + +```` +func show_text() -> void: + # ... + var tween := create_tween() + var text_appearing_duration := 1.2 + tween.tween_property(rich_text_label, "visible_ratio", 1.0, text_appearing_duration) +```` \ No newline at end of file diff --git a/MyObsidianVault/Godot_Training/2D/Tutorial Game 4 - Telling a story/M7.L6 - Animating the text and playing sounds/2.) AudioStream.md b/MyObsidianVault/Godot_Training/2D/Tutorial Game 4 - Telling a story/M7.L6 - Animating the text and playing sounds/2.) AudioStream.md new file mode 100644 index 0000000..b180da9 --- /dev/null +++ b/MyObsidianVault/Godot_Training/2D/Tutorial Game 4 - Telling a story/M7.L6 - Animating the text and playing sounds/2.) AudioStream.md @@ -0,0 +1,16 @@ +Generic: + AudioStream +Positional: + AudioStream2D + AudioStream3D + +` +``` + var text_appearing_duration = 1.2 # value is in seconds + var sound_max_length := audio_stream_player.stream.get_length() - text_appearing_duration + var sound_start_position := randf() * sound_max_length + audio_stream_player.play(sound_start_position) + tween.finished.connect(audio_stream_player.stop) + + +``` \ No newline at end of file diff --git a/MyObsidianVault/Godot_Training/2D/Tutorial Game 4 - Telling a story/M7.L6 - Animating the text and playing sounds/3.) Full code.md b/MyObsidianVault/Godot_Training/2D/Tutorial Game 4 - Telling a story/M7.L6 - Animating the text and playing sounds/3.) Full code.md new file mode 100644 index 0000000..70f5d4b --- /dev/null +++ b/MyObsidianVault/Godot_Training/2D/Tutorial Game 4 - Telling a story/M7.L6 - Animating the text and playing sounds/3.) Full code.md @@ -0,0 +1,50 @@ +``` +extends Control + +## An array of strings. Each string contains the text we want the character to +## say. +var dialogue_items: Array[String] = [ + "I'm learning about Arrays...", + "...and it is a little bit complicated.", + "Let's see if I got it right: an array is a list of values!", + "Did I get it right? Did I?", + "Hehe! Bye bye~!", +] +## Holds the index of the currently displayed text +var current_item_index := 0 + +@onready var rich_text_label: RichTextLabel = %RichTextLabel +@onready var next_button: Button = %NextButton +@onready var audio_stream_player: AudioStreamPlayer = %AudioStreamPlayer + + +func _ready() -> void: + show_text() + next_button.pressed.connect(advance) + +## Draws the current text to the rich text element +func show_text() -> void: + var current_item := dialogue_items[current_item_index] + rich_text_label.text = current_item + # We animate the text appearing letter by letter. + rich_text_label.visible_ratio = 0.0 + var tween := create_tween() + var text_appearing_duration := 1.2 + tween.tween_property(rich_text_label, "visible_ratio", 1.0, text_appearing_duration) + # This is where we play the audio. We randomize the audio playback's start + # time to make it sound different every time. + var sound_max_length := audio_stream_player.stream.get_length() - text_appearing_duration + var sound_start_position := randf() * sound_max_length + audio_stream_player.play(sound_start_position) + # We stop the audio when the text finishes appearing. + tween.finished.connect(audio_stream_player.stop) + +func advance() -> void: + current_item_index += 1 + # If we reached the end of the dialogue, we quit the game. Otherwise, we + # show the next dialogue line. + if current_item_index == dialogue_items.size(): + get_tree().quit() + else: + show_text() +``` \ No newline at end of file diff --git a/MyObsidianVault/Pasted image 20240626083938.png b/MyObsidianVault/Pasted image 20240626083938.png new file mode 100644 index 0000000..cd9b498 Binary files /dev/null and b/MyObsidianVault/Pasted image 20240626083938.png differ