godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.13k stars 93 forks source link

GDScript: add `@read_only` annotation for custom node #10174

Open DexterFstone opened 3 months ago

DexterFstone commented 3 months ago

Describe the project you are working on

Projects and Add-ons that Use Custom Classes

Describe the problem or limitation you are having in your project

When creating a custom class:

  1. Cannot be used as scene root: It has a script, so it cannot be connected to.
  2. Clicking on a method, property, or signal opens the script, not the documentation: For example, I create a class called CustomCharacterBody2D and publish it. Someone wants to use it but cannot access its APIs directly because it is not the root. Or maybe they want to see what this method does when they click on it, so they go directly to the script instead of the documentation.

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

Adding a @read_only Keyword to Custom Classes in Godot (Of course, I am not sure about the name @read_only) In Godot, the @read_only keyword can be added to the top line of a custom class script to achieve several beneficial behaviors:

Prevent Direct Node Editing: When a node with a @read_only script is added to a scene, the script button in the node's scene window is hidden. This prevents direct editing of the script's code, promoting a more structured and maintainable approach.

Enable Script Attachment: Despite being read-only, nodes with @read_only scripts can still have other scripts attached to them. This allows for extending the functionality of the read-only script using separate scripts, promoting code modularity and reusability.

Method and Signal Documentation: Clicking on methods, signals, or properties of a @read_only script will directly open the script's documentation instead of the script code. This enhances the user experience by providing immediate access to relevant documentation.

Clean Node Display: In the "Add Node" window, the script name is hidden from the node name for @read_only scripts. This aligns with the behavior of internal Godot nodes, creating a consistent and intuitive node naming convention.

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

@read_only
@class_name CustomCharacterBody2D
extends CharacterBody2D

image

image

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

no

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

there is currently no built-in API in Godot to directly achieve the desired behavior of making custom classes read-only with the specified features. Implementing these features would require modifications to the Godot engine's core code.

RedMser commented 3 months ago

It sounds like you want to be able to create an extension class in GDScript (a Script that acts like it is a new class). Seems like a duplicate of #7950 to me

DexterFstone commented 2 months ago

It sounds like you want to be able to create an extension class in GDScript (a Script that acts like it is a new class). Seems like a duplicate of #7950 to me

here are more details about my proposal Artboard 1

HolonProduction commented 2 months ago

Having a GDScript behave the same as an native class would be a bit tricky, but I think for the two issues you pointed out there might be simpler solution:

dalexeev commented 2 months ago

2. Clicking on a method, property, or signal opens the script, not the documentation: For example, I create a class called CustomCharacterBody2D and publish it. Someone wants to use it but cannot access its APIs directly because it is not the root. Or maybe they want to see what this method does when they click on it, so they go directly to the script instead of the documentation.

I think we should separate the Open Documentation and Go to Definition shortcuts. For example, Ctrl+Click and Ctrl+Shift+Click (or Alt+Click). Both options would be available for GDScript. For native symbols, we can support documentation only or implement opening the sources on GitHub in the browser (I'm not sure how difficult it is to implement).