Material Box

Material Box

WEB Design & Material Images

Godot - Getting the Average Value of an Array

Godot

This code example shows how to get the average value of an array in GDScript.


Use `reduce()` to get the sum of an array.
`reduce()` calls the specified Callable for each element in the array and accumulates the result.
Divide the obtained sum by the number of elements in the array obtained with `size()`.

var array = [5, 6, 7]

var sum = array.reduce(func(acc, num): return acc + num)
var average = sum / array.size()

print(average)
# 6

Godot

TitleGodot - Getting the Average Value of an Array

CategoryGodot

Created

Update

AuthorYousuke.U