Search code examples
luagideros

How to get an Array of Meshes


myMesh = {}
myMesh[0].x = 30

Nope... doesn't work.

myMesh = Mesh.new{}
myMesh[0].x = 30

Nope... no go.

An array of meshes should be possible, but I don't know how. Can someone help? Thanks

Edit: Thanks for your responses. Pardon me for this stupid mistake. I just realized, "What nonsense are you doing J?" I really wanted an array of vertices of the mesh, so that I can use loops to manipulate them. Sorry about that... although it didn't hurt to learn how to get an array of meshes.

So I figured it out, because I was sure I did this before, when I used gml.

ptx = {}; pty = {}; ptz = {} 
ptx[1] = myMesh.x; pty[1] = myMesh.y; ptz[1] = myMesh.z; 

Thanks for the help though. I also learned that lua doesn't use index 0

Edit: Wait. That doesn't work either does it?

Well for now this gives no error, so I'll see if it does what I want.

pMesh = {}
 for m=1, 6 do
    pMesh[m] = Mesh.new()
 end

pMesh[1].x = 0; pMesh[1].y = 0; pMesh[1].z = 0

Thanks guys. If you have any other suggestions, I'm all ears.


Solution

  • myMesh = {}
    myMesh[0].x = 30
    

    Will cause an error for indexing a nil value.

    myMesh = {} creates an empty Lua table.

    doing myMesh[0].x is not allowed because there is no myMesh[0]. First you have to insert a table element at index 0.

    myMesh = {}
    myMesh[0] = Mesh.new(true)
    myMesh[0].x = 30
    

    myMesh is a stupid name for an array of meshes as it suggests it's just a single mesh. Also in Lua we usually start with index 1 which will makes things a bit easier using Lua's standard table tools. I'm not sure if

    mesh = Mesh.new()
    mesh.x = 30
    

    is actually ok. Why would a mesh have an x coordinate? This is not listed in Mesh's properties in the manual.

    Usually you would create a Mesh with multiple points And if you want an array of multiple meshes you would simply put those meshes into a table unless there is a particular user data type for this.