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

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

Open
TheDuriel opened this issue Jul 22, 2020 · 6 comments
Open

Comments

@TheDuriel
Copy link

TheDuriel commented Jul 22, 2020

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
Copy link

Jummit commented Jul 22, 2020

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
Copy link
Author

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

@TheDuriel
Copy link
Author

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
Copy link

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
Copy link
Author

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.)

@fire fire changed the title Script editor feature for generating setter and getter functions. Add a script editor feature for generating setter and getter functions. Jul 22, 2020
@Calinou
Copy link
Member

Calinou commented Feb 17, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants