-
-
Notifications
You must be signed in to change notification settings - Fork 97
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 sum(Array)
or Array.sum()
to GDScript
#3776
Comments
I believe this may be better off as a Array function, instead of being available in the global scope, so that it may be called like this:
...And/or a function specifically available for all Packed*Array, except PackedStringArray and PackedByteArray, so that it's not prone to error (if the Array is not all int/floats) and opens the door for more interesting behaviour (getting the average Vector2 of a PackedVector2Array) |
This can be achieved using Is performance of summing arrays an actual bottleneck in a project of yours? Don't optimize before measuring the bottlenecks 🙂 |
This alone deserves a proposal in and of itself really (see godotengine/godot#15639), especially if this could be implemented via GDExtension to alleviate performance issues. 🙂 The only issue is that those kind of functions won't be built-in, unless Godot somehow manages to integrate extensions to be officially bundled as part of official release (which likely won't happen due to minimalistic approach to everything in Godot). However, if Godot adds support for this natively, we could add |
plugins can, add new functions and variabless globally, i think you just gotta autoload them in the project |
The keyword is built-in here. It means that you could register GDScript functions as if they are part of the language. But as I said, the issue is still there: you always have to download an external addon for every single project you create, even prototypes. #831 could be implemented to help this, but as far as I know, the consensus is that global plugins will be restricted to editor only, which is unfortunate. I just wanted to clarify this, lets keep this on topic and continue discussion in #831 if you like. 🙂 |
sum(Array)
to GDScriptsum(Array)
or Array.sum() to GDScript
sum(Array)
or Array.sum() to GDScriptsum(Array)
or Array.sum()
to GDScript
Python and some other languages have both |
Assuming we are using a single array, and the array elements are numerical values, you could simply perform a loop of the array:
Would this not work? |
Definitely a necessary tool. Yeah, technically you can replicate an array sum function, with a loop, but this is a very fundamental array operation - having to loop on this is like having to do all multiplication in Godot by putting addition in a loop. I'd add a mean() function to the list as well, while we're at it. |
I would like a For example Swift allows |
I would like this too. Is it at all hard to add? I don't think there's anything bad about adding a sum function and it would be helpful and doing something like |
Describe the project you are working on
A game (not related to the proposal tho)
Describe the problem or limitation you are having in your project
There is no function to get sum of an Array.
In Python, I simply use
sum(Array)
Also the built-in implementation would be faster than iterating in a gdscript loop (it would be noticeable on large arrays).
Describe the feature / enhancement and how it helps to overcome the problem or limitation
Adding
sum(Array)
function would solve the problem.Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
sum(Array)
Assuming Array is an array of ints or array of floats.
Python
sum()
also hasstart
argument (this is just a value that will be added to the sum() result), but not sure if it is needed.Another option is
Array.sum()
#3776 (comment)
If this enhancement will not be used often, can it be worked around with a few lines of script?
but built-in one would be faster (and maybe some other advantages)
Is there a reason why this should be core and not an add-on in the asset library?
plugins can't add built-in functions
The text was updated successfully, but these errors were encountered: