I’m creating a game in Godot and i need to change the scene after the textbox finishes reading. I mean it in a way that when the textbox ends completely, (not just one text in the queue) it switches the scene. I tried putting the get_tree().change_scene(“scene”) line at the end of the entire code or after the state of finish but it either switches immediately, before the textbox even appears or after the first like in the queue.
This is the code i have. It’s from the youtube tutorial as i’m still new to game making but it doesn’t say anything about what i need here. I assume i need to create a new state but i don’t really know how to get around to it. It’s Godot 3.5
const CHAR_READ_RATE = 0.05
onready var textbox_container = $TextboxContainer
onready var start_symbol = $TextboxContainer/MarginContainer/HBoxContainer/start
onready var end_symbol = $TextboxContainer/MarginContainer/HBoxContainer/end
onready var label = $TextboxContainer/MarginContainer/HBoxContainer/text
enum State {
READY,
READING,
FINISHED
}
var current_state = State.READY
var text_queue = []
func _ready():
print("Starting state: State.READY")
hide_textbox()
queue_text("*someone yelling, then stops.*")
queue_text("Next.")
queue_text("*someone walks in.*")
queue_text("Hello, welcome to he-")
func _process(delta):
match current_state:
State.READY:
if !text_queue.empty():
display_text()
State.READING:
if Input.is_action_just_pressed("ui_accept"):
label.percent_visible = 1.0
$Tween.remove_all()
end_symbol.text = "v"
change_state(State.FINISHED)
State.FINISHED:
if Input.is_action_just_pressed("ui_accept"):
change_state(State.READY)
hide_textbox()
func queue_text(next_text):
text_queue.push_back(next_text)
func hide_textbox():
start_symbol.text = ""
end_symbol.text = ""
label.text = ""
textbox_container.hide()
func show_textbox():
start_symbol.text = "*"
textbox_container.show()
func display_text():
var next_text = text_queue.pop_front()
label.text = next_text
label.percent_visible = 0.0
change_state(State.READING)
show_textbox()
$Tween.interpolate_property(label, "percent_visible", 0.0, 1.0, len(next_text) * CHAR_READ_RATE, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
$Tween.start()
func change_state(next_state):
current_state = next_state
match current_state:
State.READY:
print("Changing state to: State.READY")
State.READING:
print("Changing state to: State.READING")
State.FINISHED:
print("Changing state to: State.FINISHED")
func _on_Tween_tween_all_completed(object, key):
end_symbol.text = "v"
change_state(State.FINISHED)```