Godot Version
Godot 4.4
Question
I want to make a button that instantiates a new text label every time it is pressed. However, I only know how to instantiate one text label. How can I code the button so that it cycles through new texts when the same button is pressed?
Here is what the project looks like now. When the button is pressed, a new label appears underneath the first label. I want the new labels to display different text from the label above.
Here is the code of the main scene:
extends Control
@onready var button: Button = $Panel/MarginContainer/VBoxContainer/Button
const TEXTS = preload("res://scenes/texts.tscn")
@onready var dialogue_entries: VBoxContainer = $Panel/MarginContainer/VBoxContainer/ScrollContainer/DialogueEntries
func _on_button_pressed() -> void:
var entry = TEXTS.instantiate()
dialogue_entries.add_child(entry)
entry.set_content("And, on his ample forehead aiming full,")
Here is the code of the text labels:
extends MarginContainer
@onready var richlabel: RichTextLabel = $RichTextLabel
func set_content(text: String) -> void:
var content = text
richlabel.text = content
await get_tree().process_frame
