How do I use a key in game to activate a instanced scene?

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

So I’ve got a working textbox (code below) but I don’t know how to put the textbox onto something such as an enemy or friendly AI in game, any help?

Code for textbox:
extends CanvasLayer

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/Label

enum State{
READY,
READING,
FINISHED
}

var current_state = State.READY
var text_queue =

func _ready():
print(“Starting state: State.READY”)
hide_textbox()
queue_text(“This is a test of skill and practice”)
queue_text(“This is not a test of skill and practice”)
queue_text(“This however is a test of skill and practice”)
queue_text(“This is a test of only skill and practice”)

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.stop_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 _on_Tween_tween_all_completed():
end_symbol.text = “v”
change_state(State.FINISHED)

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”)

That’s a lot of unformatted code to look through. Please edit your post and format the code for the forum. To do that, select the block of code and press the { } button in the forum editor’s toolbar…

jgodfrey | 2022-07-15 15:43

:bust_in_silhouette: Reply From: USBashka

Load the scene with textbox to the script that will be spawn them

var textbox = preload("res://Textbox.tscn") #Put your path instead

Create an instance

var new_textbox = textbox.instance()

Attach it to the character

$Character.add_child(new_textbox) #Put your path to the node

You can change the position (relative to the character) with new_textbox.position and put text to it with (I guess) new_textbox.text_queue

And don’t forget to add code formatting next time.