Attempt to index a nil value (local 'self')

Hopefully you can help me. I am trying to create a class that calls a function in itself passing some parameters.

I am getting a Attempt to index a nil value (local ‘self’)

What am I doing wrong ?

Here’s the code and thanks.


-- set ships automtically on grid.
function BattleGridManager:addShips()
    print("Building my ships")    
    self:addShip(Ship(1,6))
end

function BattleGridManager:addShip(ship)
-- add ship to grid
    placed = false
    print(ship)
    -- continue until random setting is complete
    while(placed==false)
    do
        -- I have found that random does not return random numbers randomly.
        r = math.random(21)
        x=r
        r = math.random(21)
        y=r
        print("r",r,"x,y",x,y)
        placed = true
        direction = self:checkPosition(x,y,ship.getSize())
        
        if (direction == 1) then addShipDown(x,y,ship) end
        if (direction == 2) then addShipAcross(x,y,ship) end
        if (direction == 3) then placed = false end
    end 
end

@darkside2003 I think you need to read about classes more before trying to use them.

EDIT: When you post code, put 3 ~’s on a line before and after your code. Look at the Wiki link at the top of the forum for tutorials that will help you.

@darkside2003 - how are you calling this code from your setup function?

@ignatz, also if it will be as it needed to be, the class upper isn’t called :init

@TokOut, do you really think I don’t know that? :confused:

I want to see the code which calls the class.

@darkside2003 Here’s your code with additions I made so it at least runs. Maybe what I show here is enough for you to get your code running.

function setup()
    bg=BattleGridManager()
    bg:addShips()    
end

BattleGridManager = class()

function BattleGridManager:init()   
end

-- set ships automtically on grid.
function BattleGridManager:addShips()
    print("Building my ships")    
    self:addShip(vec2(1,6))
end

function BattleGridManager:addShip(ship)
-- add ship to grid
    placed = false
    print("ship",ship)
    -- continue until random setting is complete
    while(placed==false)
    do
        -- I have found that random does not return random numbers randomly.
        r = math.random(21)
        x=r
        r = math.random(21)
        y=r
        print("r",r,"x,y",x,y)
        placed = true
        direction = self:checkPosition(x,y,self.getSize())
        if (direction == 1) then self:addShipDown(x,y,ship) end
        if (direction == 2) then self:addShipAcross(x,y,ship) end
        if (direction == 3) then placed = false end
    end 
end

function BattleGridManager:checkPosition(x,y,z)  
    print("checkPosition") 
    return 1
end

function BattleGridManager:getSize()  
    print("getSize") 
end

function BattleGridManager:addShipDown(x,y,s)  
    print("addShipDown ",x,y,s) 
end

function BattleGridManager:addShipAcross(x,y,s)  
    print("addShipAcross ",x,y,s) 
end

@ignatz I thought you’ve overseen it. Also best developers/ teachers can oversee mistakes

Sorted it out.

Thanks everyone for your help