How do I keep a script from starting until something else happens?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By indolent_gamemakers

Hello! We are creating the opening sequence of a game. The goal is for an animation to play the first time you hit a button, for a second animation to play the second time you hit the button, and then for a speech bubble to pop up the third time you press the button.

Our main script looks like this:

extends Node2D

var event_trigger = 0

var Opening_Sequence_Finished = false

func _process(_delta):
	if Input.is_action_just_pressed("ui_select"):
		if event_trigger == 0:
		if event_trigger == 1:
		if event_trigger == 2:
			$Speech_Bubble.visible = true
		event_trigger += 1 
		print($AnimationPlayer.current_animation, event_trigger)
func shake():
func shake2():

Then our speech bubble script looks like this (we followed a YouTube tutorial for this one):

extends TextureRect
#How to Make a Dialog Box System in Godot in 6 minutes by Afely

export var dialogPath = ""
export(float) var textSpeed = 0.05
var dialog
var phraseNum = 0
var finished = false
func _ready():
	$Timer.wait_time = textSpeed
	dialog = getDialog()
	assert(dialog, "Dialog not found")
func _process(_delta):
	$Indicator.visible = finished
	if Input.is_action_just_pressed("ui_accept"):
		if finished:
			$Text.visible_characters = len($Text.text)
func getDialog() -> Array:
	var f =
	assert(f.file_exists(dialogPath), "File path does not exist"), File.READ)
	var json = f.get_as_text()
	var output = parse_json(json)
	if typeof(output) == TYPE_ARRAY:
		return output
		return []
func nextPhrase() -> void:
	if phraseNum >= len(dialog):
	finished = false
	#$Name.bbcode_text = dialog[phraseNum]["Name"]
	$Text.bbcode_text = dialog[phraseNum]["Text"]
	$Text.visible_characters = 0
	#var f =
	#var img = dialog[phraseNum]["Name"] + dialog[phraseNum]["Emotion"] + ".png"
	#if f.file_exists(img):
		#$Portrait.texture = load(img)
	#else: $Portrait.texture = null
	while $Text.visible_characters < len($Text.text):
		$Text.visible_characters += 1
		yield($Timer, "timeout")
	finished = true
	phraseNum += 1

The problem is that the speech bubble is running even when it’s set to invisible. Currently we’re putting in three lines of dialogue we don’t care if people see, but obviously that’s messy and less-than-ideal. How do you ACTUALLY make it so that the $Speech_Bubble stuff doesn’t start until event_trigger == 2?

:bust_in_silhouette: Reply From: Gluon

There are several possibilities, you can add boolean guards linked to a timer or event, or you could use the yield function built in to godot (docs here GDScript reference — Godot Engine (stable) documentation in English) but I would suggest have a signal attached to a timer to delay the new text showing.

Its your choice though any one of those methods should work.