Skip to content

4.0 RC 2: Object::cast_to<() returns nullpr #1043

Closed

Description

@sash-rc

4.0 RC 2 (Linux x86_64 gcc)

auto node1 = get_node_or_null("gui"); // valid
auto node2 = Object::cast_to<Control>(node1); // nullpr
auto node3 = get_node<Control>("gui"); // nullpr

Activity

  1. Withaust commented on Feb 15, 2023

    @Withaust

    Also broken by #1037

  2. added
    bug
    on Feb 15, 2023
  3. dsnopek commented on Feb 15, 2023

    @dsnopek
    Collaborator

    Can you share a minimal reproduction project?

    I attempted to reproduce the issue, but was unable to - here's my project: object-cast-to-test.zip

    The core of my project is a GuiNode class that looks like this:

    void GuiNode::_ready() {
        Control *gui = memnew(Control);
        gui->set_name("gui");
        add_child(gui);
    }
    
    void GuiNode::_bind_methods() {
        ClassDB::bind_method(D_METHOD("do_something"), &GuiNode::do_something);
    }
    
    void GuiNode::do_something() {
        auto node1 = get_node_or_null("gui");
        auto node2 = Object::cast_to<Control>(node1);
        auto node3 = get_node<Control>("gui");
    
        printf("node1 = %p\n", node1);
        printf("node2 = %p\n", node2);
        printf("node3 = %p\n", node3);
    }

    In the game (in the game/ directory), there's a simple GDScript that does:

    func _ready() -> void:
    	var n = GuiNode.new()
    	add_child(n)
    	n.do_something()

    In my testing, it's printing out a valid pointer for all three cases!

    I suspect there's another bug somewhere that affects certain ways of creating Godot objects, which was simply exposed by the change in PR #1037. So, we'll really need to see how your nodes are created in order to track down where the problem is.

  4. sash-rc commented on Feb 15, 2023

    @sash-rc
    Author

    @dsnopek
    Sorry, I don't use SCons for my projects. But to reproduce a case, in a stock demo project, in test/src/example.cpp
    append this code to void Example::_notification(int p_what) (after line 52)

    if  (p_what == NOTIFICATION_READY) {
      auto just_node = get_node_or_null("Label");
      auto label_node = get_node<Label>("Label");
      auto cast_node = cast_to<Label>(just_node);
      UtilityFunctions::print("NOTIFICATION_READY, just_node: ", just_node, " label_node: ", label_node, " cast_node: ", cast_node);
    }

    It prints

    NOTIFICATION_READY, just_node: Label:<Label#26608664999> label_node: <Object#null> cast_node: <Object#null>

    Built with "editor" target, if this matters.

  5. dsnopek commented on Feb 16, 2023

    @dsnopek
    Collaborator

    @sash-rc In your example, how is the "Label" node created?

  6. sash-rc commented on Feb 16, 2023

    @sash-rc
    Author

    @dsnopek

    how is the "Label" node created?

    It's a part of demo scene, a child of Example node. Stored in test/demo/main.tscn

  7. dsnopek commented on Feb 16, 2023

    @dsnopek
    Collaborator

    It's a part of demo scene, a child of Example node. Stored in test/demo/main.tscn

    Oh, ok, you're using the demo project in this repo! Sorry, I didn't pick up on that from your original comment. Thanks!

  8. codecat commented on Feb 17, 2023

    @codecat
    Contributor

    This caused some issues in TBLoader as well: codecat/godot-tbloader#68 and codecat/godot-tbloader#69

  9. RonYanDaik commented on Feb 19, 2023

    @RonYanDaik

    on windows fails to cast.

    Object* oub = Object::cast_to<Object>((Object*)selected_array[x]); //success
    Node* noub = Node::cast_to<Node>((Object*)selected_array[x]);	  //failure
    Node* noub2 = Node::cast_to<Node>(selected_array[x]);		//failure
    
    //- then ---------------------------------------------------------------------------------
    Node3D* nub = (Node3D*)(oub);					//success
    Node3D* nub2 = Node3D::cast_to<Node3D>(oub);			//failure
    
    //-- and to test if cast_to is working at all --------------------------------------------
    Node* tub = nub->find_child("UnitBase");				//success
    Node3D* ub = Object::cast_to<Node3D>(tub);				//success
    
    
  10. dsnopek commented on Feb 19, 2023

    @dsnopek
    Collaborator

    FYI, the change that is causing these issues is going to be reverted (Remi approved it but hasn't gotten around to merging it yet):

    #1046

    I do have a set of PR's that fix these regressions without reverting (since reverting will add some other bugs back :-)), but the changes are too big with 4.0's imminent release.

    In any case, thanks for the extra info on how to reproduce, but this issue should be fixed very soon once the revert PR is merged. :-)

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions