Can't get button_pressed signal to emit to called function

Godot v4

I have a “main menu” button to go back from settings to start screen, the signal I have connected to the button doesn’t seem to be working like I intended. Button registers being hit but signal to main script doesn’t work. I have the settings as an instantiated scene when opened if that matters.
Here’s code for settings menu:

extends Node
var scroll_speed = Vector2(-5, 0)
signal exit

func _ready() -> void:
	$"Music off".hide()
	$"FX off".hide()
	pass

func _physics_process(_delta: float) -> void:
	$ParallaxBackground.scroll_offset += scroll_speed
	

func _on_music_toggled(toggled_on: bool) -> void:
	if toggled_on == true:
 	    $"Music off".hide()
		$"Music on".show()
		#add sound "on" toggle here
	if toggled_on == false:
		$"Music on".hide()
		$"Music off".show()
		#add sound "off" toggle here

func _on_fx_toggled(toggled_on: bool) -> void:
	if toggled_on == true:
		$"FX on".show()
		$"FX off".hide()
		#add FX sound "on" here
	if toggled_on == false:
		$"FX off".show()
		$"FX on".hide()
     	        #add FX sound "off" here

func _on_main_menu_pressed(): **#here's where the signal emits to main script**
	exit.emit()
	print("button hit")

Here’s code for main scene:

@onready var player = preload("res://scenes/player.tscn")
@onready var HUD = preload("res://scenes/HUD.tscn")
@onready var opt = preload("res://scenes/settings_menu.tscn")

func spawn(pos):
	$Player.position = pos
	$Player/CollisionShape2D.disabled = false
	

func _ready():
	$HUD/Play.hide()
	$HUD/Quit.hide()
	$HUD/Settings.hide()
	$HUD/Title.hide()
	remove_child($"Settings Menu")
	remove_child($Player)
	$"Level 1".hide()
	await get_tree().create_timer(0.5).timeout
	$HUD/Title.show()
	await get_tree().create_timer(1.5).timeout
	$HUD/Play.show()
	$HUD/Quit.show()
	$HUD/Settings.show()
	

func Game_start():
	var P = player.instantiate()>
	$"Start Screen".hide()
	$"Level 1".show()
	remove_child($HUD)
	await get_tree().create_timer(1.0).timeout
	add_child(P)
	spawn($"player spawn".position)
	pass

func quit():
	get_tree().quit()

func Settings(): **#instantiating settings scene here**
	var O = opt.instantiate()
	remove_child($HUD)
	$"Start Screen".hide()
	add_child(O)

func _on_settings_menu_main(): **#Linked through editor from `exit` signal, not getting signal here** 
	var H = HUD.instantiate()
	remove_child($"Settings Menu")
	add_child(H)
	$"Start Screen".show()
	print("exit settings")

First, use the </> button to format your code, its hard to read as is.

Second, you emit the exit signal but i don’t see you listering for this signal anywhere, also, you don’t have anything that calls _on_settings_menu_main. If you’re connecting them using the editor a print showing the connections would help.

fixed the </> issue sorry about that, I used the signal connector on the right side to link the exit signal to the _on_settings_menu_main. That worked for my other signals from other scripts so figured it would work here as well.

I did a video what i mean, your code still unformated:


About the signal, _on_settings_menu_main is triggered from which signal? Show a print of your editor signal inspector.

Here’s a screenshot of signal inspector and linked function. Sorry for the whole formatting thing it is really unclear to me, but I appreciate the help and all pointers are helpful!

Something is not right, in your print the signal is connected to the _exit function and the function exists in the main_scene script, but the code your put here this function doesn’t exist and instead has the _on_settings_menu_main, are you sure you didn’t renamed the function that receive the signal?

yea i renamed function while waiting for replies on the post the _exit is the _on_settings_main_menu but I disconnected the signal and re connected after changing, hoping that would fix the issue.

The only thing i can think is the emit signal is never be emitted, you see the “button hit” print? Also you see the “exit settings” print?

I only see the “button hit” print I never receive the “exit settings” print. The only thing I thought was it had to do with removing the Settings scene and adding it back in later as an instance?

Yeah, if you’re doing that you need to reconnect the signal by code

Okay would I have to do that in the main script of my settings script? Im assuming I will be using connect method, something like this?

func Settings():
	var O = opt.instantiate()
	remove_child($HUD)
	$"Start Screen".hide()
	add_child(O)
	O.connect("exit", _exit())

Yes, will be in the script that instantiate the scene, but the way to connect signals in 4.0 is: node_that_have_the_signal.signal_name.connect(function_that_will_be_called_with_no_()) so would be: O.exit.connect(_exit)

1 Like

okay now when I try to open settings I get this error.
Invalid type in function 'connect' in base 'Signal'. Cannot convert argument 1 from Nil to Callable.

NVM thank you! I added the “()” by accident works now!

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