Creating a new scene instance via signal

Godot Version

4

Question

Hello! I am super new to Godot (and development as a whole, honestly) and my game is a restaurant management game. My goal now is to be able to press a button from a menu and spawn a server NPC. Currently I have a “game” scene, where everything comes together, and then I have a “hireMenuS” scene as a child of the “game” scene. Here is the code I have so far (in GDscript):

Inside hireMenuS script
var loadServer = preload(“res://Scenes/server.tscn”)
signal hireServer

func _on_hire_emp_button_s_pressed():
emit_signal(“hireServer”)
print(“eeee”)

Inside game script
var preloadServer = preload(“res://Scenes/server.tscn”)

func _on_hire_menu_s_hire_server():
var serverInstance = preloadServer.instantiate()
add_child(serverInstance)
print(“server added”)

The problem is that the _on_hire_menu_s_hire_server() function does not seem to run. The print statement inside the hireMenuS script does work, but the one inside the game script never does. Is there some issue with the signal? Any help is appreciated. Also, if there is a better way to spawn the server, please let me know!
Thank you

you will need to connect signal hireServer from hireMenuS script in game script function

The function _on_hire_menu_s_hire_server(): in the game script is connected to the signal I believe, but it still doesn’t seem to run

ok how :

be instantiated?

Yes, it does show that. I just double checked, the signal should be properly connected, so I am still not sure what’s wrong! Is there maybe a different, easier way to do this? Or is there some other error?

ok how :

be instantiated? to the scene?

if you want it to be 100% sure to work add this in your game main script:

func _ready():
	hireMenuNode.hireServer.connect(_on_hire_menu_s_hire_server)

i dont know if you have reference to your hireMenuScript’s node or not, what’s its name, so i just use hireMenuNode. Assuming you have reference to HireMenuScript attached node

Thank you. I tried adding that line but it threw an error because it was already connected, and it still doesn’t seem to run the function. I appreciate your help though

by any chance how many hireMenuNode you have in a scene?

Just one

if my guess is correct, dont name your signal with PascalCase, do it with snake_case

if above is not the case, try direcly emit the signal by:

hireServer.emit()

based on the _ready() function trying to connect the signal by code, it said it’s already connected, so the signal does indeed connected, but emit doesnt get emitted, hence never triggered the _on_hire_menu_s_hire_server function

I changed both things as you suggested, but for some reason it is still not working, nor is it printing the “server added” statement. I’m really not sure at this point why nothing is happening!

can you show your scene node of that game script attached node and hiremenuS attached node in a scene tree?

Like this?
(Sorry for the color)