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 sum(Array) or Array.sum() to GDScript #3776

Open
me2beats opened this issue Jan 9, 2022 · 10 comments
Open

Add sum(Array) or Array.sum() to GDScript #3776

me2beats opened this issue Jan 9, 2022 · 10 comments

Comments

@me2beats
Copy link

me2beats commented Jan 9, 2022

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 has start 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?

func sum(arr:Array):
	var result = 0
	for i in arr:
		result+=i
	return result

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

@Mickeon
Copy link

Mickeon commented Jan 9, 2022

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:

var my_array := [1, 2, 3, 4, 5]
print(my_array.sum()) # prints "15"

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

@Calinou
Copy link
Member

Calinou commented Jan 9, 2022

This can be achieved using Array.reduce() in master.

Is performance of summing arrays an actual bottleneck in a project of yours? Don't optimize before measuring the bottlenecks 🙂

@Xrayez
Copy link
Contributor

Xrayez commented Jan 9, 2022

plugins can't add built-in functions

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 sum() and alike in Goost.

@ghost
Copy link

ghost commented Jan 9, 2022

plugins can, add new functions and variabless globally, i think you just gotta autoload them in the project

@Xrayez
Copy link
Contributor

Xrayez commented Jan 9, 2022

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

@me2beats me2beats changed the title Add sum(Array) to GDScript Add sum(Array) or Array.sum() to GDScript Jan 9, 2022
@me2beats me2beats changed the title Add sum(Array) or Array.sum() to GDScript Add sum(Array) or Array.sum() to GDScript Jan 9, 2022
@me2beats
Copy link
Author

me2beats commented Jan 9, 2022

This can be achieved using [Array.reduce()]

Python and some other languages have both reduce() and sum()

@mojoyup
Copy link

mojoyup commented Apr 30, 2022

Assuming we are using a single array, and the array elements are numerical values, you could simply perform a loop of the array:

for l in finalArray.size(): if l < finalArray.size(): #ADD ARRAY VALUES sum += finalArray[l] print(sum)

Would this not work?
PS. sorry if I cannot add a code "block" yet. Still learning. I just clicked on the add code button.

@Proggle
Copy link

Proggle commented Aug 31, 2022

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.

@emlai
Copy link

emlai commented Nov 1, 2023

I would like a sum method as well, but maybe a good middle ground is to make the reduce syntax nicer, for example by allowing passing + as a function to it:

For example Swift allows numbers.reduce(0, +) and Ruby allows numbers.reduce(:+) for summing an array.

@carsonetb
Copy link

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 for l in finalArray.size(): if l < finalArray.size(): #ADD ARRAY VALUES sum += finalArray[l] is kind of messy.

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

8 participants