Skip to content

Calling connect within extension class ctor causes set_instance_bindings error #1312

Closed

Description

@Naros

Godot version

4.2

godot-cpp version

4.2

System information

Windows 11

Issue description

When calling the connect method from within a GDE class ctor, the following error is thrown:

ERROR: Condition "_instance_bindings != nullptr && _instance_bindings[0].binding != nullptr" is true.
   at: Object::set_instance_binding (core\object\object.cpp:1824)

Steps to reproduce

Create a simple GDE class and register signal listener callback in the ctor:

class MyClass : public LineEdit {
 ...
}

MyClass::MyClass() {
  connect("text_submitted", callable_mp(this, &MyClass::_on_text_submitted));
}
...

When the class is created, the error will be thrown.

Minimal reproduction project

N/A

Activity

  1. dsnopek commented on Nov 20, 2023

    @dsnopek
    Collaborator

    Thanks!

    I think this is a variation on issue #1039

    Basically, at present, you can't pass this into any Godot APIs during the constructor, because the object isn't finished being setup at that point.

  2. AThousandShips commented on Nov 20, 2023

    @AThousandShips
    Member

    Can you try using NOTIFICATION_POSTINITIALIZE instead as a workaround?

  3. Naros commented on Nov 22, 2023

    @Naros
    ContributorAuthor

    Can you try using NOTIFICATION_POSTINITIALIZE instead as a workaround?

    No, because unfortunately NOTIFICATION_POSTINITIALIZE is not fired like it is in GDScript, see #1269 ; however, right now I am using NOTIFICATION_ENTER_TREE or NOTIFICATION_READY at the moment.

  4. bearman92 commented on Nov 22, 2023

    @bearman92
    Contributor

    I found a workaround that works for me

    I added _init() method to Wrapped

    class Wrapped() {
    ...
    	virtual void _init() { } // <- here
    	void _notification(int p_what) {}
    ...
    }
    

    And call at end of the Wrapped::_postinitialize

    void Wrapped::_postinitialize() {
    	const StringName *extension_class = _get_extension_class_name();
    	if (extension_class) {
    		godot::internal::gdextension_interface_object_set_instance(_owner, reinterpret_cast<GDExtensionConstStringNamePtr>(extension_class), this);
    	}
    	godot::internal::gdextension_interface_object_set_instance_binding(_owner, godot::internal::token, this, _get_bindings_callbacks());
    	_init(); // <- here
    }
    

    And I just use _init instead of constructors.

  5. AThousandShips commented on Nov 22, 2023

    @AThousandShips
    Member

    Generally many signals are best to connect on entering the tree, partially due to threading, see:

  6. dsnopek commented on Dec 6, 2023

    @dsnopek
    Collaborator

    Can you try using NOTIFICATION_POSTINITIALIZE instead as a workaround?

    No, because unfortunately NOTIFICATION_POSTINITIALIZE is not fired like it is in GDScript, see #1269 ; however, right now I am using NOTIFICATION_ENTER_TREE or NOTIFICATION_READY at the moment.

    PR #1321 was recently merged to fix NOTIFICATION_POSTINITIALIZE. Perhaps that is now another way to work around this issue?

  7. Naros commented on Jan 12, 2024

    @Naros
    ContributorAuthor

    Thanks, based on the feedback, I am going to close this as it seems the agreed approach is to use a later phase during object construction than the constructor to perform signal connections.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions