Having trouble drawing meshes from functions

Hello,

I am new to Codea (and coding in general) and have found this site to be extremely useful. I’ve used this site to learn how to draw meshes and how to call functions, but I’m a bit stuck when it comes to combining the two techniques. I’m sure it’s something really simple that I am doing incorrectly.

Problem: I can draw meshes just fine when I include them in “Main”, but I cannot get my meshes to draw when I try to call it as a function.

Thank you all for your help.

function setup()

triangle = meshTest()

--triangle = mesh()
--triangle.vertices = {vec2(0,0),vec2(100,0),vec2(0,100)}

end


function draw()

    background(40, 40, 50)
    strokeWidth(5)
    triangle:draw()
    
end

-- The code below is on a different tab  -- 

meshTest = class()

function meshTest:init()
    
self.mesh = mesh()
self.vertices = {vec2(0,0),vec2(100,0),vec2(0,100)}

end

function meshTest:draw()

self.mesh:draw()

end

You’re 90% there.


--# Main

function setup()
    triangle = MeshTest()
end

function draw()
    background(40, 40, 50)
    strokeWidth(5)
    triangle:draw()
end


--# MeshTest
MeshTest = class()

function MeshTest:init()

self.mesh = mesh() 
self.mesh.vertices = {vec2(0,0),vec2(100,0),vec2(0,100)}

end

function MeshTest:draw()

self.mesh:draw()

end

The biggest thing was that in meshTest you were using self.vertices instead of self.mesh.vertices. Remember that you’d defined the class as containing a mesh called mesh rather than being a mesh itself.

I capitalized the name of the class. Nothing magic about that, just handy for keepin it distinct from variable names.

Ah, I see! Thank you very much. Now if I can only figure out a way to replace the hair I’ve pulled out over the past two days…