Skip to content

Broken GDExtention's resource properties #1042

Closed

Description

@Withaust

Godot version

4.0.rc2

System information

Windows 10, Forward Clustered

Issue description

After RC2 in GDExtention properties that are resources (for example, PackedScenes) get reset to <empty> no matter what you do (even if you assign manually from .tscn file)
image

Steps to reproduce

Have an exposed resource property with GDExtention and try assigning any resource to it.

Minimal reproduction project

Activity

  1. added
    bug
    on Feb 15, 2023
  2. akien-mga commented on Feb 15, 2023

    @akien-mga
    Member

    How are you using GDExtension exactly?

  3. added this to the 4.0 milestone on Feb 15, 2023
  4. Withaust commented on Feb 15, 2023

    @Withaust
    Author

    In .hpp file:

    private:
    
        godot::Ref<godot::PackedScene> player_scene;
    
    public:
    
        void set_player_scene(godot::Ref<godot::PackedScene> p_player_scene) { 
            player_scene = p_player_scene; 
        } 
    
        godot::Ref<godot::PackedScene> get_player_scene() { 
            return player_scene; 
        }

    In .cpp file:

    void PlayerSpawner::_bind_methods() {
        godot::ClassDB::bind_method(godot::D_METHOD("get_player_scene"), &PlayerSpawner::get_player_scene); 
        godot::ClassDB::bind_method(godot::D_METHOD("set_player_scene"), &PlayerSpawner::set_player_scene); 
        ADD_PROPERTY(godot::PropertyInfo(godot::Variant::OBJECT, "player_scene",
        PROPERTY_HINT_RESOURCE_TYPE, "PackedScene"), "set_player_scene", "get_player_scene");
    }

    In RC1 this code worked perfectly fine

  5. akien-mga commented on Feb 15, 2023

    @akien-mga
    Member

    That looks like godot-cpp specifically, not just the lower level GDExtension. I'll move it to the godot-cpp repo.

  6. Withaust commented on Feb 15, 2023

    @Withaust
    Author

    After manually reverting lines changed by #1037, related to cast_to everything started working again
    image

  7. changed the title [-]Godot RC2 broke GDExtention's resource properties[/-] [+]Broken GDExtention's resource properties[/+] on Feb 15, 2023
  8. dsnopek commented on Feb 15, 2023

    @dsnopek
    Collaborator

    I am able to reproduce this issue! Here's a minimal test project that I created: resource-property-test.zip

    I'm still trying to figure out what exactly is happening...

  9. dsnopek commented on Feb 15, 2023

    @dsnopek
    Collaborator

    I'm starting to understand what's going on here:

    • The Variant containing the PackedScene is being passed from the Godot side to the godot-cpp side
    • When Ref<T>::Ref(const Variant &p_variant) gets it, it calls Variant::operator Object *() to get the godot::Object (the godot-cpp version of the object - I'll put godot:: in front to try and make that distinction)
    • Inside Variant::operator Object*() there is this line at the end:
      return reinterpret_cast<Object *>(internal::gde_interface->object_get_instance_binding(obj, internal::token, &Object::___binding_callbacks));
      
    • Via a whole bunch of indirection, this leads to a godot::Object being constructed (because the &Object::__binding_callbacks are being passed in), rather than a godot::PackedScene
    • So, the dynamic_cast<godot::PackedScene>() ultimately fails, because the object isn't a godot::PackedScene (even though the _owner property points to a PackedScene on the Godot side)

    Before PR #1037, it was doing a reinterpret_cast<T>() (instead of dynamic_cast<T>()) which would cast the godot::Object to a godot::PackedScene, even though it wasn't actually a godot::PackedScene object. I think this would have made calling any of godot::PackedScene's methods unsafe?

    So, I think the solution is figuring out how to have Variant::operator Object*() (on the godot-cpp side - this is getting confusing :-)) actually create a godot::PackedScene rather than a godot::Object? I'm not yet sure how to do that, though.

    I'd love some review of my reasoning above from someone who is more familiar with how GDExtension is intended to work. :-)

  10. dsnopek commented on Feb 16, 2023

    @dsnopek
    Collaborator

    I think I've got a way to fix this issue but it's kind of involved (requires a change on the Godot engine side, and a new generated file in godot-cpp) and so will probably require a bunch of review and back-and-forth. With the 4.0 stable release coming soon, let's just revert PR #1037 for now, and I can finish a proper fix after 4.0 :-)

    Here's a PR #1046 to do the revert

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

    bugregression

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions