The Tile System does not work

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.
Leap of Time
Prole
Posts: 23
Joined: Fri Sep 11, 2015 9:20 pm

The Tile System does not work

Post by Leap of Time »

--Solved--

I Debugged for like the whole FUCKING day and I can´t get it to FUCKING work. I don´t understand the error and I made a nil check and only if none of the values are nil it executes it, but it still says, that there is a FUCKING nil in the line.

This is the crash reprot:
"
Error

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

Traceback

main.lua:145: in function `drawMap`
main.lua:91: in function `draw`
[C]: in function `xpcall`
"

Here is my source code:

Code: Select all

--the main.lua
--LÖVE
lib = require("lib.mainLib")
keyListener = require ("lib.keyListener")
require("maps.map1")

function love.load()
	WIDTH = 1240
	HEIGHT = WIDTH / 16 * 10
	love.window.setMode(WIDTH, HEIGHT)
	
	--love.window.setFullscreen(true, "desktop")

	playerRightNoKnife = love.graphics.newImage("resources/entitys/Spieler (Gehen ohne Klingen, transparent).png")
	playerLeftNoKnife = love.graphics.newImage("resources/entitys/Spieler (Andere Richtung gehen ohne Klingen, transparent).png")
	--player = lib.animationArray(6, 190, 19 - 1, 50, 80, -54, playerLeftNoKnife)
	player = lib.animationArray(6, 40, 19, 50, 70, 54, playerRightNoKnife)
	playerP = playerRightNoKnife
	
	oneTime = true
	
	px = 1
	py = 1
	
	mapMotX = 0
	mapMotY = 0
	mapSX = 0
	mapSY = 0
	
	timer = 0
	iterator = 0
	max = 3
	moving = false
	direction = "left"
	
	onetime = true
	secondtime = false
	jumping = false
	
	--if abcde == nil then love.event.quit() end
	
	tileIndexer = {}
	for i = 1, 1000 do
		tileIndexer[i] = {}
		for o = 1, 1000 do
			tileIndexer[i*o] = {}
			tileIndexer[i+o] = {}
			tileIndexer[i-o] = {}
			tileIndexer[i/o] = {}
		end
	end
	
	loadTiles()
	loadMap1()
end


function love.update(dt)
	if tileIndexer[math.floor((love.window.getHeight() / 2) + tileSize * 2)][math.floor(love.window.getWidth() / 2 + mapX - 10 - tileSize * 1)] ~= 0 and direction == "left" and moving then mapMotX = 0 ; moving = false ; iterator = 0 end
	if not jumping and tileIndexer[math.floor(love.window.getHeight() / 2 + tileSize * 4 + 1)][math.floor(love.window.getWidth() / 2 + mapX - 10)] == 0 then mapMotY = 10 end
	mapSX = mapSX + mapMotX * dt
	mapSY = mapSY + mapMotY * dt
	mapX = math.floor(mapSX)
	mapY = 1
	
	
	px = love.window.getWidth() / 2 - 15
	py = love.window.getHeight() / 2 + 5
	
	if secondtime and onetime then
		mapSX = 0
		onetime = false
	end
	secondtime = true
	
	if direction == "right" and moving then playerP = playerRightNoKnife player = lib.animationArray(6, 40, 20, 50, 70, 54, playerRightNoKnife) end
	if direction == "left" and moving then playerP = playerLeftNoKnife player = lib.animationArray(6, 190, 20 - 1, 50, 80, -54, playerLeftNoKnife) end
	
	if moving then
		timer = timer + dt
		if timer > 0.21 then
			timer = 0
			iterator = iterator + 1
			if iterator > max then
				iterator = 0
			end
		end
	end
end

function love.draw()
	drawMap()
	love.graphics.draw(playerP, player[iterator], px, py)
	--the block under the player tileIndexer[math.floor(love.window.getHeight() / 2 + tileSize * 4 + 1)][math.floor(love.window.getWidth() / 2 + mapX - 10)]  x = 768, 608 y = 464
	--the block left in the middle of the player tileIndexer[math.floor((love.window.getHeight() / 2) + tileSize * 2)][math.floor(love.window.getWidth() / 2 + mapX - 10 - tileSize * 1)]
	--(love.window.getHeight() / 2) + tileSize * 2)
	love.graphics.print(math.floor((love.window.getHeight() / 2) + tileSize * 3), 200, 100)
	love.graphics.print(math.floor(love.window.getWidth() / 2 + mapX), 300, 100)
	love.graphics.print(mapSY, 400, 100)
	
end

function love.keypressed(key)
	if key == "escape" then lib.save() end
	if key == "d" then moving = true ;  direction = "right" ; mapMotX = 14 end
	if key == "a" and tileIndexer[math.floor((love.window.getHeight() / 2) + tileSize * 2)][math.floor(love.window.getWidth() / 2 + mapX - 10 - tileSize * 1)] == 0 then moving = true ;  direction = "left" ; mapMotX = -14 end
end

function love.keyreleased(key)
	if key == "d" then moving = false ;  direction = "right" ; iterator = 0 ; mapMotX = 0 end
	if key == "a" then moving = false ;  direction = "left" ; iterator = 0 ; mapMotX = 0 end
	if key == "d" and love.keyboard.isDown("a") and tileIndexer[math.floor((love.window.getHeight() / 2) + tileSize * 2)][math.floor(love.window.getWidth() / 2 + mapX - 10 - tileSize * 1)] == 0 then moving = true ; direction = "left" ; mapMotX = -14 end
	if key == "a" and love.keyboard.isDown("d") then moving = true ; direction = "right" ; mapMotX = 14 end
end

function loadTiles()
	tileSize = 16
	tile = {}
	tileIndex = {}
	tileI = {}
	tile[0] = love.graphics.newImage("resources/tiles/Tile0.png")
	tileIndex[0] = 0
	for i=1,4 do
		tile[i] = love.graphics.newImage( "resources/tiles/Tile"..i..".png" )
		tile[i]:setFilter("nearest", "linear")
		tileIndex[i] = i
	end
end

function loadMap1()
	renderHeight = 100
	renderWidth = 79
	mapX = 0
	mapY = 0
	map = requestMap1()
end

function drawMap()
	for y = 1, #map do
		if y + mapY < renderHeight + mapY then
			for x = 1, #map[y] do
				--love.graphics.print(x + mapX, x * tileSize - tileSize / 2, y * tileSize - tileSize)
				if y + mapY ~= nil then love.graphics.print(y + mapY, x * tileSize - tileSize, y * tileSize - tileSize) end
				--if x + mapX < renderWidth + mapX and y + mapY < renderHeight + mapY then if tile[map[y + mapY][x + mapX]] == nil then else love.graphics.draw(tile[map[y + mapY][x + mapX]], x * tileSize - tileSize, y * tileSize - tileSize, 0, tileSize / tile[map[y + mapY][x + mapX]]:getWidth(), tileSize / tile[map[y + mapY][x + mapX]]:getHeight()) end end
				tileI[y + mapY] = {}
				if map[y + mapY][x + mapX] ~= nil and tileI[y + mapY][x + mapX] ~= nil and y + mapY ~= nil and tileIndex[map[y + mapY][x + mapX]] ~= nil then tileI[y + mapY][x + mapX] = tileIndex[map[y + mapY][x + mapX]] end -- line 145 the error seems to be here.
				playerTileDetect(y, x, mapY, mapX)
			end
		end
	end
end

function playerTileDetect(y, x, mapY, mapX)
	for i = 1, tileSize do
		tileIndexer[y * tileSize - tileSize + i][x * tileSize - tileSize + i] = tileI[y + mapY][x + mapX]
		for o = 1, tileSize do
			tileIndexer[y * tileSize - tileSize + i][x * tileSize - tileSize + o] = tileI[y + mapY][x + mapX]
		end
	end
	--love.graphics.print(x * tileSize - tileSize + 1, x * tileSize - tileSize, y * tileSize - tileSize)
	--love.graphics.print(x * tileSize - tileSize + 1, 100 * x + x, 200 * y + x)
	--love.graphics.print(tileIndexer[y * tileSize - tileSize + 1][x * tileSize - tileSize + 1], x * tileSize - tileSize, y * tileSize - tileSize)
	--tx = tileIndexer[math.floor(love.window.getHeight() / 2 + 5)][math.floor(love.window.getWidth() / 2 - 15)]
end

Last edited by Leap of Time on Sun Sep 13, 2015 6:41 pm, edited 2 times in total.
User avatar
ArchAngel075
Party member
Posts: 319
Joined: Mon Jun 24, 2013 5:16 am

Re: HELP ME

Post by ArchAngel075 »

Source code cannot be used because other required files are missing, though

add this to just before line 145 (the if statement) :

Code: Select all

                print(map[y + mapY],#map)
                print(map[y + mapY][x + mapX],#map)
                print(tileI[y + mapY][x + mapX],#tileI)
                print(tileIndex[map[y + mapY][x + mapX]],#tileIndex)
if any of those prints is nil, check which one it is (by using the order)
its also possible the program will error on one of those with a nil index

Also you can use for integer containing arrays :

Code: Select all

if map[y + mapY][x + mapX] and tileI[y + mapY][x + mapX] and ..... then
instead of,

Code: Select all

if map[y + mapY][x + mapX] ~= nil and tileI[y + mapY][x + mapX] ~= nil and ..... then
the check returns false if the table has nothing at that index.
the check returns true if an item exists at that index, though if the value is a false value you would need to explicitly check against nil.
Last edited by ArchAngel075 on Sun Sep 13, 2015 5:19 pm, edited 1 time in total.
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: HELP ME

Post by arampl »

ArchAngel075 wrote:Source code cannot be used because other required files are missing, though

add this to just before line 145 (the if statement) :

Code: Select all

                print(map[y + mapY],#map)
                print(map[y + mapY][x + mapX],#map)
                print(tileI[y + mapY][x + mapX],#tileI)
                print(tileIndex[map[y + mapY][x + mapX]],#tileIndex)
if any of those prints is nil, check which one it is (by using the order)
I bet second print will show nil.
Leap of Time, you can attach .love file so we can help you better, and if you don't want to share your data(images, sounds, etc) you can replace them by some stub files (simple colored recangles for tiles for example)
Leap of Time
Prole
Posts: 23
Joined: Fri Sep 11, 2015 9:20 pm

Re: HELP ME

Post by Leap of Time »

how do I attach things?
User avatar
ArchAngel075
Party member
Posts: 319
Joined: Mon Jun 24, 2013 5:16 am

Re: HELP ME

Post by ArchAngel075 »

create a .love, or zip (not rar those are evil) file.
At bottom of a forum post (one being created or edited) select upload attachment tab - browse to your file and upload, it is automatically attached to end if you dont place inline - otherwise use place inline to specify where in a post it is attained.
Last edited by ArchAngel075 on Sun Sep 13, 2015 5:24 pm, edited 1 time in total.
Leap of Time
Prole
Posts: 23
Joined: Fri Sep 11, 2015 9:20 pm

Re: HELP ME

Post by Leap of Time »

and the prints do nothing. (I think its because they are made for a console, and I don´t know how to open a .love with a console (I am using Windows!).
Leap of Time
Prole
Posts: 23
Joined: Fri Sep 11, 2015 9:20 pm

Re: HELP ME

Post by Leap of Time »

The File
main.love
(50.49 KiB) Downloaded 246 times
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: HELP ME

Post by arampl »

Leap of Time wrote:and the prints do nothing. (I think its because they are made for a console, and I don´t know how to open a .love with a console (I am using Windows!).
Use conf.lua for that. Set t.console = true. See here: Config Files
User avatar
ArchAngel075
Party member
Posts: 319
Joined: Mon Jun 24, 2013 5:16 am

Re: HELP ME

Post by ArchAngel075 »

jimmied a premade conf file from my past projects, (other nonsense too since i installed love 0.10.0 ...)

turns out assert(tileI[y + mapY][x + mapX]) is a fail.
assert(tileI[y + mapY]) is fine

further tests show the array tileI[y + mapY] contains nothing (size 0)

Note this asserts once per frame, so it may be populated sometime after frame [1]

EDIT

nope it is not populated at all ever.
Leap of Time
Prole
Posts: 23
Joined: Fri Sep 11, 2015 9:20 pm

Re: HELP ME

Post by Leap of Time »

Sooo, what do I have to do
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 3 guests