add_child does not seem to work when called from a _notification function

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mshka
:warning: Old Version Published before Godot 3 was released.

Hello,
i have a function which add a pause_menu to the scene(instance) which is triggered by a button press or a quit request(_notification)
the function seems to run well when triggered by the button but when its triggered from the quit request the instance isn’t added to the scene…

function:

func _on_pause_button_pressed():
	if(disablepause==false):
		var cam_pos = get_node("Camera2D").get_pos()
		var pause_menu = pause_class.instance()
		pause_menu.set_pos(cam_pos)
		add_child(pause_menu)
		get_node("sound_effect").play("button_clicks")
		pause_menu.pause_game()

_notification function:

   func _notification(what):
    	if(what == 7 and get_tree().is_paused() == false):
    		_on_pause_button_pressed()
    		print("quit request")

i tried calling add_child on get_parent() and a specific node in the scene but it wont seem to work
I’m using Godot version 1.1 stable, is this a bug that has been fixed in the newer version or am i doing something wrong

Same error on Godot 2.0.

enter image description here

DriNeo | 2016-03-20 20:47

:bust_in_silhouette: Reply From: Kermer

Add this:

func _ready():
	get_tree().set_auto_accept_quit(false)

The auto_accept_quit from project settings doesn’t work.

If auto_accept_quit is true, application runs your code (you should be able to see “quit request” message in console and maybe in debugger) and closes the app instantly after it’s done.

:bust_in_silhouette: Reply From: mshka

Answered by Mariano Suligoy and Juan Alpaca :
to add a node inside a notification use call_defferred, so the node will be added when the idle frame ends:
call_deferred(“add_child”,pause_menu)