Search code examples
lua2d-gameslove2d

Why is 'self' returning an error in my Love2D Lua code?


I'm very new to Lua and Love2D and was just tinkering with some code I found in an online tutorial. But I keep coming across an Error: Map.lua:32: attempt to index local 'self' (a number value).

Here's my main.lua code:

require 'Map'

local speed = 1000
local score = 0

map = Map:create()

function love.load()
sprite = love.graphics.newImage("Game Boy Advance - LEGO Island 2 The Bricksters Revenge - Pepper.png")
x = love.graphics.getWidth() / 2
y = love.graphics.getHeight() / 2
end

function love.update(dt)
score = score + 1
success = "no"
end

function love.keypressed(key)
    success = "yes"
    if key == "right" then
        x = x + speed
    end
    if key == "left" then
        x = x - speed
    end
    if key == "escape" then
        love.event.quit()
    end
end

function love.draw()
--love.graphics.clear(108,140,0,255)
--love.graphics.draw(sprite, x, y)
map = Map:create() -- returning nil
--love.graphics.print(map[2])

map:render()
end

And the Map.lua code:

require 'Util'

Map = {}
Map.__index = Map

TILE_FLOOR = 1
TILE_EMPTY = 12

function Map:create()
    local this = {
        spritesheet = love.graphics.newImage("Amiga Amiga CD32 - Jail Break - Blocks & Backgrounds.png"),
        mapHeight = 30,
        mapWidth = 28,
        tileheight = 12,
        tilewidth = 12,
        tiles = {}
    }

    this.tileSprites = generateQuads(this.spritesheet, 12, 12)

    setmetatable(this, self)

    for y = 1, this.mapHeight do
        for x = 1, this.mapWidth do
            this:setTile(x, y, TILE_FLOOR)
        end
    end
    return this
end

function Map:getTile(x, y)
    return self.tiles[(y - 1) * self.mapWidth + x]
end

function Map:setTile(x, y, tile)
    self.tiles[(y - 1) * self.mapWidth + x] = tile
end

function Map:render()
    for y = 1, self.mapHeight do
        for x = 1, self.mapWidth do
            love.graphics.draw(self.spritesheet, self.tileSprites[self.getTile(x,y)], (x-1) * self.tilewidth, (y-1) * self.tileheight)
        end
    end
end

And the Util.lua code:




function generateQuads(sheet, tilewidth, tileheight)
    local sheetheight = sheet:getHeight() / tileheight
    local sheetwidth = sheet:getWidth() / tilewidth

    local counter  = 1
    local quads = {}

    for y = 0, sheetheight - 1 do
        for x = 0, sheetwidth - 1 do 
            quads[counter] = love.graphics.newQuad(x * tilewidth, y  * tileheight, tilewidth, tileheight, sheet:getDimensions())
            counter = counter + 1
        end
    end

    return quads
end

Total beginner here - the problem might be simple but I'm stumped :( Thanks and have a great day! :)


Solution

  • In Map:render you incorrectly call your method Map:getTile, which requires self as first argument, using just a single dot and thus passing x as self, y as x and nil as y:

    love.graphics.draw(self.spritesheet, self.tileSprites[self.getTile(x,y)], (x-1) * self.tilewidth, (y-1) * self.tileheight)`
    

    See difference between . and :.

    which triggers the error. You must call the method self:getTile using a colon (:) to pass self as first parameter: self:getTile(x, y):

    love.graphics.draw(self.spritesheet, self.tileSprites[self:getTile(x,y)], (x-1) * self.tilewidth, (y-1) * self.tileheight)`