godotengine / godot-proposals

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

Ability to chain function calls for `setter` types #6790

Open tavurth opened 1 year ago

tavurth commented 1 year ago

Describe the project you are working on

Any project

Describe the problem or limitation you are having in your project

I quite often find myself doing the following:

        $Enabler/CollisionShape2D.set_shape(switch_shape)
        $Enabler/CollisionShape2D.set_position(Vector2.ZERO)

However in my own scripts I would have set up these setter functions to always return self at the end.

This could simplify the script above as follows:

        $Enabler/CollisionShape2D\
            .set_shape(switch_shape)\
            .set_position(Vector2.ZERO)

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

I don't have to have bulky text lookups in my code.

I don't have to @onready var assign something if it's only used a few times in the code.

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

func set_shape(new_shape: Shape2D) -> Variant:
   ...
   return self

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

This would be GDScript core

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

I don't see any way to do this with an addon, I just wanted to start this discussion.

AThousandShips commented 1 year ago

I don't really see how this makes things any better, and to me it significantly reduces readability, and a workaround you're missing is to make a local var in your script with the object you want and access it removing the lengthy $node access

AThousandShips commented 1 year ago

This would also cause confusion for functions that are named like setters but aren't setters, like ones in servers, which don't have associated properties, and functions that take multiple arguments

It'd be relatively straightforward to make the generated setter functionality in GDScript do this, but for general functions that happen to look like a property setter you'd have to do this manually, which would be a massive undertaking

And if it's not done uniformly it'd be very confusing and cause people to report it as bugs

tavurth commented 1 year ago

Perhaps then supporting some system to override basic gdscript types would help with this.

In that way we could release this as a gdextension or gdscript addon

AThousandShips commented 1 year ago

Override what basic types and in what way?

tavurth commented 1 year ago

Override what basic types and in what way?

Override all _setters for example.

Currently I can do this in GDscript, but at the same time it will require me to disable that warning on anyone who installs that plugin.

Perhaps there would be an easier way to override every setter, instead of re-writing them with super in GDScript