0 votes

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)
in Projects by (263 points)

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 ..

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()])

1 Answer

0 votes

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.

by (10,057 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories