Help tracking down an error in spritebatch

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Imaculata
Party member
Posts: 102
Joined: Mon Aug 10, 2015 2:51 pm

Help tracking down an error in spritebatch

Post by Imaculata »

I'm relatively new to coding, and I've just started on a simple Zelda clone in LOVE. I used one of the code examples from this tutorial, to create this bit of code here:
function setupMap() --Called upon loading the game

--the level layout
--This is the system of inter connecting rooms and the prefab tile sets that each room uses.

levelWidth = 5 + love.math.random(0,15)
levelHeight = 5 + love.math.random(0,15)

--The tile map, is all the tiles that makes up the randomly regenerated map
--An average dungeon screen should be 16x12tiles, leaving room for the interface at the top

mapWidth = 16 * levelWidth --levelWidth is always 5 rooms or larger
mapHeight = 12 * levelHeight --levelHeight is always 5 rooms or larger
mapScrollSpeed = 0.1

--Here is where we load the map
map = {}
for x=1,mapWidth do
map[x] = {}
for y=1,mapHeight do
map[x][y] = 118
end
end
end

function setupTileset() --Called upon loading the game
--This is a modifed version of the original script from the tutorial. I've removed all the copy-paste stuff, and tried to make the game load all the tiles automatically, based on how big the tilesheet is.

tilesetImage:setFilter("nearest", "linear") -- this "linear filter" removes some artefacts if we were to scale the tiles
sheetSize = {math.floor(tilesetImage:getWidth() /64), math.floor(tilesetImage:getHeight() /64)}

--Add the blank tile in the null position
tileQuads[0] = love.graphics.newQuad(16 * tileSize, 0 * tileSize, tileSize, tileSize, tilesetImage:getWidth(), tilesetImage:getHeight())
--Cache all the tiles on the spritesheet
for y=1,sheetSize[2] do
for x=1,sheetSize[1] do
tileQuads[((y*sheetSize[1]) - sheetSize[1]) + x] = love.graphics.newQuad((x-1) * tileSize, (y-1) * tileSize, tileSize, tileSize, tilesetImage:getWidth(), tilesetImage:getHeight())
end
end

tilesetBatch = love.graphics.newSpriteBatch(tilesetImage, tilesDisplayWidth * tilesDisplayHeight)

updateTilesetBatch()
end

--Update tiles --Called when moving the map
function updateTilesetBatch()
tilesetBatch:bind()
tilesetBatch:clear()
for x=0, tilesDisplayWidth-1 do
for y=0, tilesDisplayHeight-1 do
if (map ~= nil and tileQuads ~= nil) then
tilesetBatch:add(tileQuads[map[x+math.floor(mapX)][y+math.floor(mapY)]], x*tileSize, y*tileSize) --<-Error occurs here
end
end
end
tilesetBatch:unbind()
end

-- central function for moving the map
function moveMap(dx, dy)
oldMapX = mapX
oldMapY = mapY
mapX = math.max(math.min(mapX + dx, mapWidth - tilesDisplayWidth), 1)
mapY = math.max(math.min(mapY + dy, mapHeight - tilesDisplayHeight), 1)

-- only update if we actually moved
if math.floor(mapX) ~= math.floor(oldMapX) or math.floor(mapY) ~= math.floor(oldMapY) then
updateTilesetBatch()
end
end
However, it occasionally spawns the following error upon starting the game, but not always.
ERROR

main.lua:323: attempt to index a nil value

Traceback

main.lua:323: in function 'updateTilesetBatch'
main.lua:292: in function 'setupTileset'
main.lua:37: in function 'load'
[C]: in function 'xpcall'
What I'm trying to do, is make the game create random dungeons. So I've automated the code for loading quads, and the size of the dungeon is different every time I load the game. This may have something to do with why the error only occurs sometimes. I've installed ZeroBraneStudio in order to try and get a bit more information out of the code, because I want to know exactly what the values are when it all goes wrong. But for some reason, ZeroBraneStudio doesn't tell me anything. No variables to inspect what so ever. Maybe I have the thing configured wrong or something, I don't know. It IS using the LOVE interpreter, but it still doesn't seem to work.

So I have two questions:

Does anyone know what could be causing the error?
Does anyone know of a good way to help track these sort of issues in the future?
User avatar
Imaculata
Party member
Posts: 102
Joined: Mon Aug 10, 2015 2:51 pm

Re: Help tracking down an error in spritebatch

Post by Imaculata »

Here's a screenshot of the two errors I've been getting, and the line of code they are referring to. I assume this means that the code is requesting an item from the "map" or "tileQuads" tables, that returns nill, or does not exist. Or, the entire table is nil at the moment that the code looks up the value.

But how do I solve this problem?

Image
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 222 guests