Bug with 'self' (SOLVED)

I’m experiencing this weird bug. Here’s some part of the code:

Block = class()
function Block:draw()
translate(self.x,self.y,self.z)
self.mesh:draw()
end

It tells me: attempt to index a nil value (local ‘self’)
Any help would be appriceated

Do you have a function Block:init() that sets self.x, self.y, and self.z to some values.

Yes, here is some of it:

function Block:init(x,y,z)
self.x = x
self.y = y
self.z = z
self.mesh = mesh()
end

Here’s something that uses what you’ve shown so far.

function setup()
    m1=Block(100,200,0)
end

function draw()
    background(40, 40, 50)
    m1:draw() 
end

Block = class(x,y,z)

function Block:init(x,y,z)
    self.x = x
    self.y = y
    self.z = z
    self.mesh = mesh()
    self.mesh.vertices={vec2(100,100),vec2(200,100),vec2(100,200)}
    self.mesh:setColors(255,255,255)
end

function Block:draw()
    translate(self.x,self.y,self.z)
    self.mesh:draw()
end

There is still the same error:
Attempt to index nil value ‘self’ :frowning:

the error is in this line:

translate(self.x,self.y,self.z)

The code I show above works fine for me with no errors. I can’t say why you get errors without seeing your code.

Here’s the whole code:
Block:

Block = class(x,y,z)

function Block:init(x,y,z)
    -- you can accept and set parameters here
    self.x = x
    self.y = y
    self.z = z
    local vertices = {
    vec3(-0.5, -0.5,  0.5), -- Left  bottom front
    vec3( 0.5, -0.5,  0.5), -- Right bottom front
    vec3( 0.5,  0.5,  0.5), -- Right top    front
    vec3(-0.5,  0.5,  0.5), -- Left  top    front
    vec3(-0.5, -0.5, -0.5), -- Left  bottom back
    vec3( 0.5, -0.5, -0.5), -- Right bottom back
    vec3( 0.5,  0.5, -0.5), -- Right top    back
    vec3(-0.5,  0.5, -0.5), -- Left  top    back
    }
    
    
    -- now construct a cube out of the vertices above
    local cubeverts = {
    -- Front
    vertices[1], vertices[2], vertices[3],
    vertices[1], vertices[3], vertices[4],
    -- Right
    vertices[2], vertices[6], vertices[7],
    vertices[2], vertices[7], vertices[3],
    -- Back
    vertices[6], vertices[5], vertices[8],
    vertices[6], vertices[8], vertices[7],
    -- Left
    vertices[5], vertices[1], vertices[4],
    vertices[5], vertices[4], vertices[8],
    -- Top
    vertices[4], vertices[3], vertices[7],
    vertices[4], vertices[7], vertices[8],
    -- Bottom
    vertices[5], vertices[6], vertices[2],
    vertices[5], vertices[2], vertices[1],
    }
    local texvertices = { vec2(0.03,0.24),
    vec2(0.97,0.24),
    vec2(0.03,0.69),
    vec2(0.97,0.69) }
    
    -- apply the texture coordinates to each triangle
    local cubetexCoords = {
    -- Front
    texvertices[1], texvertices[2], texvertices[4],
    texvertices[1], texvertices[4], texvertices[3],
    -- Right
    texvertices[1], texvertices[2], texvertices[4],
    texvertices[1], texvertices[4], texvertices[3],
    -- Back
    texvertices[1], texvertices[2], texvertices[4],
    texvertices[1], texvertices[4], texvertices[3],
    -- Left
    texvertices[1], texvertices[2], texvertices[4],
    texvertices[1], texvertices[4], texvertices[3],
    -- Top
    texvertices[1], texvertices[2], texvertices[4],
    texvertices[1], texvertices[4], texvertices[3],
    -- Bottom
    texvertices[1], texvertices[2], texvertices[4],
    texvertices[1], texvertices[4], texvertices[3],
    }
    
    -- now we make our block type
    self.Mh = mesh()
    self.Mh.vertices = cubeverts
    self.Mh.texCoords = cubetexCoords
    self.Mh.texture = "Project:Block Grass"
    self.Mh:setColors(255,255,255,255)
    
end
function Block:draw()
    -- Codea does not automatically call this method
    pushMatrix()
    resetMatrix()
    translate(self.x,self.y,self.z)
    self.Mh:draw()
    popMatrix()
end

function Block:touched(touch)
    -- Codea does not automatically call this method
end

Main:


-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    playerX = 0
playerY = 0
playerZ = 0
    angleX = 0
    angleY = 0
RotationX = 0
RotationY = 0
RotationZ = 0
    blocks = {}
    for j = 0,32 do
    for i = 1 , 32 do
        blocks[i] = Block(i,-1,j)
        blocks[i]:init(i,-1,j)
        blocks[i].Mh.texture = "Project:Block Grass"
    end
        end
end
-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(0, 255, 251, 255)
    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    resetMatrix()
    RotationX = math.cos(math.rad(angleX)) * 500
    camera(playerX,playerY+5,playerZ,RotationX,RotationY,RotationZ,0,1,0)
    perspective(90,WIDTH/HEIGHT)
    for i = 1,32 do
        blocks[i].draw()
    end
    resetMatrix()
end


It is unfinished, which is why you will see some unused variables

It has to be blocks[i]:draw() with a colon, not a dot, to access self.

Also, in setup, you don’t call blocks:init. That is done automatically when you declare a new instance of Block.

Thanks. It wasn’t a bug…

Apparently I didn’t get back to the discussion soon enough. Glad to see you got your answer.

Well, I assumed when you said bug, you meant bug in your own code, not in Codea. :slight_smile: