Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime classes cannot be cast_to from non runtime classes #1419

Open
cridenour opened this issue Mar 24, 2024 · 2 comments
Open

Runtime classes cannot be cast_to from non runtime classes #1419

cridenour opened this issue Mar 24, 2024 · 2 comments
Labels
bug This has been identified as a bug

Comments

@cridenour
Copy link

Godot version

4.3.dev (dad6c774b0)

godot-cpp version

4.3.dev (e55b792)

System information

Windows 10

Issue description

Noticed this when creating an EditorGizmo for a new node type - a node that is gameplay focused and ideally should be tagged runtime.

bool BuildAnchorGizmoPlugin::_has_gizmo(Node3D *for_node_3d) const {
  return Object::cast_to<BuildAnchor>(for_node_3d) != nullptr;
}

would always return false until I changed the registration.

Steps to reproduce

  1. Create and register a runtime class: e.g.
    ClassDB::register_runtime_class<BuildAnchor>();

  2. Create and register a non-runtime class: e.g.
    ClassDB::register_class<BuildAnchorEditorPlugin>();

  3. In second class, attempt to cast a given node:
    BuildAnchor *anchor = Object::cast_to<BuildAnchor>(gizmo->get_node_3d());

anchor will always be nullptr.

Minimal reproduction project

Can provide if necessary.

@AThousandShips AThousandShips added the bug This has been identified as a bug label Mar 24, 2024
@pupil1337
Copy link
Contributor

pupil1337 commented Apr 27, 2024

If node A is generated in the scene, godot-cpp will new a object, and godot will also new a object, their cache each other's addresses. When godot-cpp call get_node<T> will first execute get_node_internal(), which returns the address p of the node in godot; Then execute cast_to<T>(p), will find p pointed's godot-cpp object, and then dynamimc_cast to type T. So we can find godot-cpp object through the get_node() method.

However, since runtime-class in editor never create an instance in godot-cpp, so it returns nullptr.
This issue is caused by the runtime class feature of Godot, need godot fix it.It is best not reference runtime-class nodes during the editor.

@dsnopek
Copy link
Contributor

dsnopek commented Apr 29, 2024

However, since runtime-class in editor never create an instance in godot-cpp, so it returns nullptr.

This is correct!

The whole idea with runtime classes, is that the editor won't create a real instance of them, instead it creates a placeholder. The placeholder object's type will be the closest native parent, so if your BuildAnchor descends from Node3D, then the placeholder instance will be a Node3D, and you can still interact with it as a Node3D. But since no BuildAnchor instance was actually ever created, you can't cast to BuildAnchor.

This is by design.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This has been identified as a bug
Projects
None yet
Development

No branches or pull requests

4 participants