Want to learn more about how syntax in Arraymesh works

Godot Version


Hello everyone,

I was going over some older code I wrote for my game to get the vertex values from a mesh. I remember I followed a tutorial for it but I didn’t fully understand the syntax.

Here’s my code.

func get_mesh_vertices():
mesh_data = surface_tool.commit_to_arrays()
vertices = mesh_data[ArrayMesh.ARRAY_VERTEX]
normals = mesh_data[ArrayMesh.ARRAY_NORMAL]

So this works perfectly, I get the Vertex coordinates and vertex normals in those two variables. What I don’t understand is how this syntax works.

mesh_data seems to be a large array from understanding. So what is “mesh_data[ArrayMesh.ARRAY_VERTEX]” actually doing? I don’t think I have seen something written inside the [ ] part of an array doing something before.

So what does ArrayMesh.ARRAY_VERTEX do?

And why does it happen inside of square brackets?

How am I able to get different information out by putting in a different value in the [ ]?

Is this a common thing?

Where can I learn more about this type of syntax?

I did check the documentation and while I learned more about the SurfaceMesh tool and how the ArrayMesh API kind of works, I couldn’t find this square bracket syntax anywhere. Maybe this is a common array function I am unaware of.

Thank you so much!

mesh_data is an array and you are accessing the different indices of that array with []

ARRAY_INDEX, ARRAY_NORMAL,… are just entries in the enum Mesh.ArrayType


vertices = mesh_data[ArrayMesh.ARRAY_VERTEX]
normals = mesh_data[ArrayMesh.ARRAY_NORMAL]


vertices = mesh_data[0]
normals = mesh_data[1]
1 Like

Oh that makes sense! It’s just an array with another array as certain entries. I think I just didn’t truly get ENUMs so that threw me off at first. mesh_data is just an array of arrays lol. Thanks for the explanation!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.