Invalid call. Nonexistent function 'instance' in base 'int'.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dumuz

My problem lies with the last couple lines. This is my first time trying to make a packedscene in script for use of instancing later. But on this test add_child(), it keeps failing with this error I put in the title: Invalid call. Nonexistent function ‘instance’ in base ‘int’. I’m not sure what I’m doing wrong here. I’ve tried it without packing the scene and then instancing and it works fine, once. But it won’t add anymore because “the child already has a parent”, so I need to instance this.

func _on_give_Toy_btn_pressed():
	var highlight = Button.new()
	var empty = StyleBoxEmpty.new()
	highlight.rect_min_size = Vector2(100,100)
	highlight.set('custom_styles/normal', empty )
	highlight.connect("pressed",pet,"_praise",["toy","doll"])
	highlight.connect("pressed",self,"_clean_up",[highlight.get_instance_id()])

	var highlighted = PackedScene.new().pack(highlight).instance()
	$Inv_GUI/inv_cont.get_child(0).add_child(highlighted)

I’m not really familiar with PackedScene, but your error comes from the fact that the pack() method returns an Error, which is just an ENUM. So, this…

PackedScend.new().pack(highlight).instance()

… ends up trying to call instance() against that returned ENUM value, which is just an INT …

jgodfrey | 2020-02-15 18:17

So that helped, thank you. I put the pack() method as a standalone and called the creation of the instance when the child is being created:

var highlighted = PackedScene.new()
highlighted.pack(highlight)
$Inv_GUI/inv_cont.get_child(c).add_child(highlighted.instance())

But I’ve run into a new problem now: The buttons are there, but now they don’t do anything. The two connections I made don’t seem to be doing anything anymore, why is that?

highlight.connect("pressed",pet,"_praise",["toy","doll"])
highlight.connect("pressed",self,"_clean_up",[highlight.get_instance_id()])

Dumuz | 2020-02-15 18:40

:bust_in_silhouette: Reply From: njamster

I’ve tried it without packing the scene and then instancing and it works fine, once. But it won’t add anymore because “the child already has a parent”

You cannot add the same instance of a node two times into the tree. If you want to insert two buttons, you need to create two separate instances for that:

for i in range(2):
    var btn = Button.new()
    add_child(btn.instance)

Imagine you’re directing a stage play. You can give an actor pretty precise instructions on what to do. Like where to stand, where to look at, what to wear, etc. But if you only have one actor and order him to fill out two completely separate roles at the same time that won’t work. For that you’ll always need two separate actors.

Your answer on my other question pretty much answered my follow-up on this one. Thank you again.

Dumuz | 2020-02-16 15:55