Godot: Idea: make math functions work on vectors

Created on 19 Mar 2017  路  6Comments  路  Source: godotengine/godot

It would be nice and efficient to be able to use math functions (clamp, max, sin etc) also on Vector2 and Vector3 types, like in GLSL.

So instead of writing this:

    var turn_max = Vector2(deg2rad(turn_max_deg.x), deg2rad(turn_max_deg.y))

We could write simply this:

    var turn_max = deg2rad(turn_max_deg)

Which is also faster to execute due to DRY-ness and the possibility for component-wise operations to be executed natively ;)

Another example:

    var turn_max = Vector2(deg2rad(turn_max_deg.x), deg2rad(turn_max_deg.y))
    _turn_cmd = Vector2(clamp(mouse_cmd.x, -turn_max.x, turn_max.x), \
                            clamp(mouse_cmd.y, -turn_max.y, turn_max.y))

Becomes:

    var turn_max = deg2rad(turn_max_deg)
    _turn_cmd = clamp(turn_cmd, -turn_max, turn_max)
archived feature proposal core

Most helpful comment

Generally I support this idea, but in the special case of GDScript it's not ideal.

GDScript doesn't have overloaded methods, this would need to be either treated like a special case or overloaded functions would have to be implemented which isn't planned AFAIK.

Making the functions accept Variant instead of float/double would be an easy fix but would make them slower.

All 6 comments

Generally I support this idea, but in the special case of GDScript it's not ideal.

GDScript doesn't have overloaded methods, this would need to be either treated like a special case or overloaded functions would have to be implemented which isn't planned AFAIK.

Making the functions accept Variant instead of float/double would be an easy fix but would make them slower.

I am writing helpers at the moment, which are slower anyways^^

@karroffel

GDScript doesn't have overloaded methods

Does it really not? I'm looking at the docs for the float, int, and bool built-in types, and they all overload their respective casting function. Are they just special cases, or...?

Those are indeed special cases, similar with constructors for build in types. Script defined methods can't be overloaded, also overloading makes less (I don't say no) sense in a dynamically typed language anyway.

@karroffel In the specific case of GDScript I don't think we even talk about "overloading" in the same sense of C++/C#, but something that behaves like it and solves the same use case, which involves accepting a Variant parameter as an implementation detail. If we can't do that to a scripting language we created especially for Godot then it's a bit of a waste^^"

Feature and improvement proposals for the Godot Engine are now being discussed and reviewed in a dedicated Godot Improvement Proposals (GIP) (godotengine/godot-proposals) issue tracker. The GIP tracker has a detailed issue template designed so that proposals include all the relevant information to start a productive discussion and help the community assess the validity of the proposal for the engine.

The main (godotengine/godot) tracker is now solely dedicated to bug reports and Pull Requests, enabling contributors to have a better focus on bug fixing work. Therefore, we are now closing all older feature proposals on the main issue tracker.

If you are interested in this feature proposal, please open a new proposal on the GIP tracker following the given issue template (after checking that it doesn't exist already). Be sure to reference this closed issue if it includes any relevant discussion (which you are also encouraged to summarize in the new proposal). Thanks in advance!

Was this page helpful?
0 / 5 - 0 ratings