Setting a child based on user input?

Godot 4.4

I’m making a script that will change a scene depending on the word the user types in.

This is the code I have tried to use:

extends ScrollContainer

var hand_scene = load("res://scenes/hand_scene.tscn")
var face_scene = load("res://sprites/hand painting stretch.png")

func _on_input_text_submitted(new_text: String) -> void:
	if new_text == "hand":
		print("hand loaded")
		add_child(hand_scene)
	if new_text == "face":
		print("face loaded")
		add_child(face_scene)

This is my main node tree:

And this is what I want the stage to look like when a scene is called:

It will print the functions when the word is typed, but I believe scenes are an invalid “child” to load. Is there another way to instantiate a scene from user input?

extends ScrollContainer

const HAND = preload("res://scenes/hand_scene.tscn")
const FACE = preload("res://sprites/hand painting stretch.png")

func _on_input_text_submitted(new_text: String) -> void:
	if new_text == "hand":
		print("hand loaded")
		var hand_scene = HAND.instantiate()
		add_child(hand_scene)
	if new_text == "face":
		print("face loaded")
		var face_scene = TextureRect.new()
		face_scene.textuire = FACE
		add_child(face_scene)

I’m not positive on the png one. You may have to play around with it a bit. But that should get you in the ballpark.

That worked for me, thank you so much!
The PNG was a mistake, I was meant to load a different scene

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.