godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.16k stars 97 forks source link

Add a script editor feature for generating setter and getter functions. #1238

Open TheDuriel opened 4 years ago

TheDuriel commented 4 years ago

Describe the project you are working on: Anything really. I use Godot commercially and make heavy use of tool scripts and static typing for cohesion when working with a team.

Describe the problem or limitation you are having in your project: Typing setters and or getters for variables can become tedious quickly. Especially when you're creating something that requires a lot of them.

func set_property_name(new_value: <type>) -> void:
    pass

There are scenarios in which I need a dozen of these in the same file, and typically every Class I make will have at least a few setters. Either to make things read only at runtime (and protect it from my team members), or to trigger some kind of behavior. It's a lot of boilerplate to write, and even to copy and paste. As you need to change the name and type each time.

Describe the feature / enhancement and how it helps to overcome the problem or limitation: I propose a menu option to generate a setter or getter at demand, using the currently selected variable for its name and type.

So you'd select a variable via doubleclick, rightclick/navigate into edit/or hit the hotkey, and the Editor would automagically insert an empty setter or getter function at an appropriate location in your script.

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams: ^

If this enhancement will not be used often, can it be worked around with a few lines of script?: I could write an autohotkey script to do this. But there is no means of doing so within the Editor itself via the plugin system or similar.

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

Jummit commented 4 years ago

In the example you showed, you missed the actual setter. This is how it should generate imo:

func set_property_name(new_value: Type) -> void:
    property_name = new_value
    # Replace with function body.

I think this should be both a context menu when right clicking a property of a script, and a menu where you can toggle which properties to generate setters/getters for.

context menu

dialog

TheDuriel commented 4 years ago

I have a lot of setters that don't actually perform the set. But yes, setting the value should be the default.

TheDuriel commented 4 years ago

On this topic. The ability to generate an initializer for non exported members may also be useful. Most people tend not to use Godot classes this way... But its something that will be used a lot by advanced users. And will make it feel more like a modern IDE.

bojidar-bg commented 4 years ago

Note that if #844 is implemented, this would be less of an issue as GDScript setters and getters would no longer need to be separate methods.

(Also, note that for the read-only case you can probably reuse the setter.)

TheDuriel commented 4 years ago

Honestly I really don't like #844 and would stick with separate functions.

It would make this less of an issue for some people, but its not suitable syntax for the kind of setters I write.

(And yeah, I'm reusing my readonly setters. Though right now I'm staring at a file where I want to do in editor validation of values, while doing something else at runtime, and I can't reuse setters there.)

Calinou commented 3 years ago

Is this still relevant with the property syntax now implemented in the new GDScript? Now that setters and getters can be written inline, I think this feature loses much of its value.