Page 1 of 1

drawq question

Posted: Mon Mar 19, 2012 5:25 am
by dabbertorres
Did a forum and google search, couldn't find an answer to my question. So....

I'm pretty new to Lua and LOVE, I'm getting the hang of it. I was loosely following the tutorials on the wiki, and I've ran into a problem when trying to execute.
I create a .love (quadruple checked I did it right), and I get the following:

"main.lua:68: Incorrect parameter type: expected user data."

I realized I most of put a variable in my drawq function wrong (it referenced to that point). I've tried a couple different things, used the function like it is used here:
https://love2d.org/wiki/love.graphics.newQuad (down at the bottom)
and here:
https://love2d.org/wiki/love.graphics.drawq
Neither worked.

Here is the function its in:

Code: Select all

function draw_map()
	for y=0, mapHeight do
		for x=0, mapWidth do
			love.graphics.drawq(terrainSetImage,
			terrainQuads[map[y+1][x+1]],
			x*terrainSize, y*terrainSize, 0, 1, 1, 0, 0)
		end
	end
end
Also tried this:

Code: Select all

function draw_map()
	for y=0, mapHeight do
		for x=0, mapWidth do
			love.graphics.drawq(terrainSetImage,
			terrainQuads[map[y+1][x+1]],
			x*terrainSize, y*terrainSize)
		end
	end
end
Got anything for me?
Thanks

Re: drawq question

Posted: Mon Mar 19, 2012 7:57 am
by Jasoco
The only way that code wouldn't work is if you didn't create either the image or the quads correctly. What is the code you use for creating the image and quads?

Also, make sure map[x+1][y+1] actually equals a value that is legal.

A tip I like to use for debugging when I get an error I can't figure out and know the line number, I like to use the console (Terminal on OS X and Linux) and use the print() function to print out data that could be useful.

In this case I would place a print(map[y+1][x+1]) before the love.graphics.quad line and see if the value printed out is a valid index to your quad table.

Also, it occurs to me that you might be trying to access a map entry that doesn't exist as evident by your use of for x = 0 to width and then adding 1 to the x and y's instead of using for x = 1, width and just accessing map[x][y]. I am BETTING that it's trying to access the first tile outside the map's upper X bounds and is failing. If you either change it to what I said, or add -1 to the width and height parts in the for loops, it may fix it.

More code please! Post your image and quad code as well as your map grid generation code.

Re: drawq question

Posted: Mon Mar 19, 2012 8:35 am
by dabbertorres
Ok, and thanks for the suggestions. I made a change in the above snippets that should work around that...
And oh ya, console would be a good idea...
And I'm going to post all that I have so far, it's a bit... Well it looks like someone just starting out. Haha.

Code: Select all

function love.load()
	love.graphics.setBackgroundColor(0, 0, 0)
	mapWidth = 20
	mapHeight = 12

	terrainSetImage = love.graphics.newImage("terrain_atlas.png")
	terrainSize = 16

	terrainQuads = {}

	-- sky
	terrainQuads[0] = love.graphics.newQuad(2*terrainSize, 2*terrainSize,
	terrainSize, terrainSize, terrainSetImage:getWidth(),
	terrainSetImage:getHeight())
	-- blank dirt
	terrainQuads[1] = love.graphics.newQuad(0*terrainSize, 0*terrainSize,
	terrainSize, terrainSize, terrainSetImage:getWidth(),
	terrainSetImage:getHeight())
	-- top grass
	terrainQuads[2] = love.graphics.newQuad(1*terrainSize, 0*terrainSize,
	terrainSize, terrainSize, terrainSetImage:getWidth(),
	terrainSetImage:getHeight())
	-- left grass
	terrainQuads[3] = love.graphics.newQuad(2*terrainSize, 0*terrainSize,
	terrainSize, terrainSize, terrainSetImage:getWidth(),
	terrainSetImage:getHeight())
	-- right grass
	terrainQuads[4] = love.graphics.newQuad(0*terrainSize, 1*terrainSize,
	terrainSize, terrainSize, terrainSetImage:getWidth(),
	terrainSetImage:getHeight())
	-- bottom grass
	terrainQuads[5] = love.graphics.newQuad(1*terrainSize, 1*terrainSize,
	terrainSize, terrainSize, terrainSetImage:getWidth(),
	terrainSetImage:getHeight())
	-- top left grass
	terrainQuads[6] = love.graphics.newQuad(2*terrainSize, 1*terrainSize,
	terrainSize, terrainSize, terrainSetImage:getWidth(),
	terrainSetImage:getHeight())
	-- top right grass
	terrainQuads[7] = love.graphics.newQuad(0*terrainSize, 2*terrainSize,
	terrainSize, terrainSize, terrainSetImage:getWidth(),
	terrainSetImage:getHeight())
	-- top right left grass
	terrainQuads[8] = love.graphics.newQuad(1*terrainSize, 2*terrainSize,
	terrainSize, terrainSize, terrainSetImage:getWidth(),
	terrainSetImage:getHeight())

	-- map table
	map = {
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 1, 7, 0, 0, 0},
		{2, 2, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 2, 1, 1, 1, 2, 2, 2},
		{1, 1, 1, 7, 0, 0, 0, 0, 0, 0, 0, 6, 1, 1, 1, 1, 1, 1, 1, 1},
		{1, 1, 1, 1, 7, 1, 1, 1, 1, 1, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1},
		{1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
		{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
		{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
	}
end

function draw_map()
	for y=1, mapHeight do
		for x=1, mapWidth do
			love.graphics.drawq(terrainSetImage,
			terrainQuads[map[y][x]],
			(x-1)*terrainSize, (y-1)*terrainSize, 0, 1, 1, 0, 0)
		end
	end
end

function love.keypressed(key)
	if key == "escape" then	-- if user wants to exit
		love.event.push("q")	-- then exit!
	end
end

function love.draw()
	draw_map()
end
It's a basic just drawing a map, I'm just trying to get used to drawing out "stationary" items before moving on.

Re: drawq question

Posted: Mon Mar 19, 2012 8:39 am
by dabbertorres
And actually I just tested your fix with the x+1 and y+1 stuff and it worked! Thanks for the help! So it was the logic, not the function that got me.... Hehe.

Re: drawq question

Posted: Mon Mar 19, 2012 7:11 pm
by Lafolie
Table indices begin at 1 in Lua.

Re: drawq question

Posted: Tue Mar 20, 2012 3:57 am
by Jasoco
Yeah. Unless you manually start at 0 or even lower (Negatives!) and isn't a different type of index like a string name always assume it begins at 1.

i.e.

Code: Select all

table = {
  {
    "Value",
    {
      "More stuff",
      "Even more"
  }
}

Code: Select all

table[1] = <a table>
table[1][1] = "Value"
table[1][2] = <a table>
table[1][2][1] = "More stuff"
table[1][2][2] = "Even more
Keep it in mind when creating the grid to remember what number you started the first tile at (Whether it's 0, 0 or 1, 1) and begin the loop there from then on.

Re: drawq question

Posted: Tue Mar 20, 2012 5:10 am
by dabbertorres
Ya, I realized that was keeping it from working well. Thanks. I got it working quite well now! I'm used to 0 as the index (I'm coming from C++, Python, etc), so 1 as the index took a bit of getting used to.