What does this enum do?

Godot Version

4.2.2

Question

I was following a tutorial on generating meshes from an array, and while the tutorial works I’m still trying to understand the code line by line. The one part I don’t really understand is the use of the enum ARRAY_MAX, I’ve looked at the docs and don’t really understand what I’m suppose to take away here.

Here’s the code.

@tool
extends MeshInstance3D


# Called when the node enters the scene tree for the first time.
func _ready():
	gen_mesh()


func gen_mesh():
	var a_mesh = ArrayMesh.new()
	var vertices = PackedVector3Array(
	[
		Vector3(1,0,1),
		Vector3(1,0,0),
		Vector3(0,0,0),
		Vector3(0,0,1),
	]
	)
	
	var indices = PackedInt32Array(
	
		[
			0,1,2,
			2,3,0
		]
	)
	
	var array = []
	array.resize(Mesh.ARRAY_MAX)
	array[Mesh.ARRAY_VERTEX] = vertices
	array[Mesh.ARRAY_INDEX] = indices
	a_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, array)
	mesh = a_mesh
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass

Also here’s the video that I was watching

1 Like

I was initially pretty confused as well, but after looking at the add_surface_from_arrays() method in the ArrayMesh class reference, I discovered its meaning.

ARRAY_MAX is the last element in the ArrayType enum and is basically utilized solely to retrieve the length of said enum. When the array is resized to Mesh.ARRAY_MAX, it is done to ensure that the array is capable of storing a sub-array for each unique ArrayType element.

From the add_surface_from_arrays() reference:

Some of these other sub-arrays include:

  • ARRAY_NORMAL: Vertex normals
  • ARRAY_COLOR: Vertex color
  • ARRAY_TEX_UV: The primary UV coordinates
  • …and so on.

In this Godot Docs reference you’ll also find code that looks pretty similar to what you found in the tutorial.

2 Likes

So when the script orders for the array to be resized it’s making sure that it will be able to fit all of the sub arrays?

2 Likes