Help with instantiating a scene using a string value

apologize if i’m not giving enough information;

i have a scene preloaded as below in my root scene (sceneManager);

@export var gamePlayScn: PackedScene

what is supposed to happen is that a button is pressed which signals to call the loadScn function and passes the value ‘gamePlayScn’

now, I want to use that value to instantiate the preloaded scene and add it as a child to the root scene using a function, as detailed below;

func loadScn(scene):
var x = scene.instantiate()

where ‘x’ would be the name of the preloaded packedscene at the very beginning, but, the error I get is ‘Invalid call. Nonexistent function ‘instantiate’ in base ‘String’’ in the loadScn function;

I would think that there would be a casting option available to convert the string to Node;
tried doing x: PackedScene = scene.instantiate(), but that doesn’t work either;

thanks in advance

How are you calling loadScn? seems like you are giving it a string instead of a PackedScene. I think you want to use load(scene).instantiate()?

Be sure to paste code between three ticks like so

type or paste code here

this is the sceneManager.tscn

extends Node

@export var mainMenuScn: PackedScene
@export var gamePlayScn: PackedScene

func _ready():
func loadScn(scene):
	var sg
	if typeof(scene) == 4:
		var x = load(scene)
		sg = x.instantiate()
		sg = scene.instantiate()
	sg.connect('changeScn', Callable(self, 'loadScn'))

this is the code for the mainMenuScn;

extends Control

signal changeScn(scene)
var scnName

func _on_btt_start_pressed():
	print("emitting start button signal")
	scnName = "gamePlayScn"
	emit_signal("changeScn", scnName)

So what happens is that the program starts and the sceneManager is loaded initially;
Then, via its ready function, the mainMenuScn is loaded right away;
The mainMenuScn has a “start” button, which when pressed is supposed to load the gamePlayScn;
Both the mainMenuScn and the gamePlayScn are declared in the initial sceneManager;

I tried the load(scene) as per your suggestion and I think it will work, but the issue is that it can’t find the gamePlayScn because it’s not looking in the directory; this seems weird to me since it is a declared export variable at the very beginning;

here is the error;

E 0:00:04:0708 @ loadScn(): Resource file not found: res://gamePlayScn (expected type: )
  <C++ Error>    Condition "!file_check->file_exists(p_path)" is true. Returning: Ref<Resource>()
  <C++ Source>   core/io/resource_loader.cpp:279 @ _load()
  <Stack Trace> @ loadScn()
        @ _on_btt_start_pressed()

the path should be “res://scenes/views/playScn.tscn” and it is correct in the export variable field in the editor;

Hey, I got something similar to this working for myself a while back but I just preloaded all the scenes in an AutoLoad script, listed them in an array, and passed in an index when I wanted to change them. I did it this way because I kept running into problems where the scenes weren’t properly loading for me when I needed them.

If I had to get it done your way, I would make sure that it at least worked hard-coded, and then at least loads something when passed in, because you’re going to have to figure out the fault line. I use print statements to figure this kind of thing out. You’ve also got to be careful of asynchronous code, sometimes programs like to finish before they receive data and that can mess you up in all kinds of ways. If it is an issue of it not loading in time, try using await.

I am assuming you’ve already read the documentation? Here are some links just in case you need them:

I get that egg. I can also understand fatigue. You’re simply getting that error because while you updated your script to @export var gamePlayScn: PackedScene
The export function added the resource selection to your Godot Inspector. You have not selected the actual tscn file there. I hope this helps. No error!

thanks again for everyone’s thoughts and prayers :wink:

I will try using an autoload script next with the scenes preloaded, however, the problem seems to be that i’m calling the loadScn function with a variable that is a string and that is not being recognized or can’t be typecast as the packedscene that’s already been preloaded at the top;

can you clarify with an example how I would select it from the loadScn function?

I keep running into the problem of the variable being a String, so it doesn’t seem to equate the value with the name of the corresponding packed scene;

I think they mean you should try reseting this property in the Node’s properties, then loading the packed scene again. No functions/code, only clicking in the editor/inspector