godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.1k stars 69 forks source link

Add `is_class_static` virtual instance method to `godot-cpp` #8852

Open limbonaut opened 7 months ago

limbonaut commented 7 months ago

Describe the project you are working on

Behavior tree framework: https://github.com/limbonaut/limboai

Describe the problem or limitation you are having in your project

I need to efficiently check if a task instance is of a certain class without comparing strings all the time. I need to do that not only in the framework code. It's a fairly useful thing.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

In the Godot Engine code, there is an instance method called is_class_ptr which can be used with Class::get_class_ptr_static() to make such checks. In the godot-cpp, we already have get_class_static() added by GDCLASS macro. Having also an instance method virtual bool is_class_static(StringName cn) would provide an efficient comparison method to the already existing functionality.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Ref<MyBaseclass> ref = get_my_class(); // returns Ref<MySubclass>;
if (ref->is_class_static(MySubclass::get_class_static()) {
  // ...
}

If this enhancement will not be used often, can it be worked around with a few lines of script?

Use is_class() instead, which is a String-based comparison.

Is there a reason why this should be core and not an add-on in the asset library?

It's as useful as is_class() but faster, and utilized already existing functionality in godot-cpp.

dsnopek commented 7 months ago

One of godot-cpp's design goals is to have the same API as internally in the engine (at least as much as possible), so, to add an is_class_static() method to godot-cpp, we'd need to first add it to the engine itself.

limbonaut commented 7 months ago

Couldn't agree more. An alternative to adding this method to the engine perhaps could be exposing/implementing methods like is_class_ptr?

dsnopek commented 7 months ago

Yeah, if we can add is_class_ptr() to godot-cpp, that could work.

We'd need to make sure that that would work even in the case where one GDExtension received an object which is of a class from another GDExtension, but where the first GDExtension didn't generate a class binding for it (because it wasn't aware of the 2nd extension).