godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.12k stars 78 forks source link

Move math functions to it's own class outside of `@GlobalScope` #10645

Open AdriaandeJongh opened 3 weeks ago

AdriaandeJongh commented 3 weeks ago

Describe the project you are working on

Rift Riff and soon a sequel to Hidden Folks.

Describe the problem or limitation you are having in your project

@GlobalScope is currently a fairly random collection of functions. It's useful to always have access to those functions but it pollutes autocomplete every time you write a few characters in every script every time. On top of that, if you're new to Godot and don't know where the math functions are listed, @GlobalScope is not the first place anyone would look.

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

80% of @GlobalScope are math functions. Just like namespaces in C#, it would help GDScript a lot if the math functions can all be found under and accessed through the same class (Math) or possibly even under type-defining classes (Mathf and Mathi). This would make the documentation clearer on where and what all the math functions are. It would also greatly reduce the autocomplete options, making that a more pleasant experience as well.

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

floor(x: Variant)
min(...)
ease(x: float, curve: float)

... becomes ...

Math.floor(x: Variant)
Math.min(a: Variant, b: Variant)
Math.ease(x: float, curve: float)

... or alternatively ...

Mathi.floor(x: int)
Mathf.floor(x: float)

Mathi.min(a: int, b: int)
Mathf.min(a: float, b: float)

Math.ease(x: float, curve: float)

This way, if you're writing code and have a variable named min_dist, and you start writing 'min', the math function doesn't even show up in autocomplete and thus there is less of a mental load (as small as it may be) to have to find the thing you're looking for. But perhaps I don't need to explain why fewer options in autocomplete are beneficial for users.

Likewise, if I'm looking for any math operation but aren't sure which one, starting to type Math. and seeing the autocomplete options is a much better experience in-editor; it allows me to stay in the editor, too.

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

n/a

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

n/a

AThousandShips commented 3 weeks ago

The nature of these methods is that they're used very often, so adding an additional step to get to them makes them harder to use and makes the massive compatibility breakage even less justified IMO

The fact that they are in their own class in C# I'd say isn't really relevant here IMO, C# is very different and just putting a lot of methods in global scope there is a problem for a more general purpose language, but GDScript is a special purpose language

Mickeon commented 3 weeks ago

There's certainly an argument to be made about things not belonging in the global scope, but I feel like most avid GDScript would disagree on the math functions being some of them. They are way too commonly used. If more advanced Math functions were added, there could be a Math class, but I don't believe this to be currently necessary, as the existing Variant types already do a good job at "enclosing those", so to speak.