godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
86.99k stars 19.51k forks source link

Cannot inherit from PhysicsBody3D or CollisionObject3D #71834

Open EMBYRDEV opened 1 year ago

EMBYRDEV commented 1 year ago

Godot version

4.0.beta14.mono

System information

Windows 10

Issue description

When in the editor you cannot create a script that inherits from PhysicsBody3D using the new script dialogue.

GDScript files that are manually edited to extend PhysicsBody3D do not immediatly produce any errors but cannot be instantiated.

C# files that extend PhysicsBody3D will not compile due to a missing contructor.

Steps to reproduce

Searching for PhysicsBody3D shows no results.

image

Searching for CharacterBody3D allows you to inherit from that class however shows both PhysicsBody3D and CollisionShape3D as disabled.

image

Minimal reproduction project

N/A

EMBYRDEV commented 1 year ago

After some further testing, any attempt to override CollisionObject3D or PhysicsBody3D in C# results in the following compile error.

[PROJ_PATH]\PlayerBody3D.cs(3,22): error CS1729: 'CollisionObject3D' does not contain a constructor that takes 0 arguments 
rburing commented 1 year ago

A CollisionObject3D is always an area or a body:

https://github.com/godotengine/godot/blob/5f3dee5988391deffcc0de1039dd50e05e266913/scene/3d/collision_object_3d.cpp#L76-L80

Similarly a PhysicsBody3D always creates a body in one of the pre-existing modes:

https://github.com/godotengine/godot/blob/999bb91dc670882742bb28c698297f110ca3fd0b/scene/3d/physics_body_3d.cpp#L56-L59

So there is little room to extend either in a meaningful way via script. On the other hand it might be neat to have a single script that you could attach either to an area or a body node, though I don't see any obvious use case. In any case, this issue is about scripts extending abstract classes.

raulsntos commented 1 year ago
Anvoker commented 2 months ago

It's arguable.

I encountered an issue where I cannot inherit from CollisionObject2D in C# because it will tell me Cannot access internal constructor 'CollisionObject2D()' here. Note that CollisionObject2D is not abstract.

I'm not sure what is the appropriate way to organize them.

As for the issue itself -- I'll admit I don't have a super solid usecase for inheriting from these classes yet. But I can illustrate a more general usecase maybe.

Godot uses inheritance as a way for your scripts to get access to methods and data from the parent node. If I have something in mind for a script that would work with either CharacterBody2D or RigidBody2D or StaticBody2D I cannot write a single script that inherits from CollisionObject2D or PhysicsBody2D and then be able to attach the script to multiple node types, which I think would be a desirable feature.