Getting a error with my draw code.

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
FoundationIDE
Prole
Posts: 6
Joined: Wed Jun 05, 2013 6:51 pm

Getting a error with my draw code.

Post by FoundationIDE »

Code: Select all

BlockSize = 128
Map_Width = 8
Map_Height = 6

sample_map = {
	{1,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},
}

LoadedMatrix = {}

function DrawBlock(GridPosX,GridPosY, block)
	love.graphics.draw(block, GridPosX * BlockSize, GridPosY * BlockSize)
end

function DrawRow(row, map_matrix)
	for CurrentColumn = 0, Map_Width, 1 do
		BlockData = map_matrix[row][CurrentColumn]
		DrawBlock(CurrentColumn, row, love.graphics.newImage("assets/" .. BlockData .. ".png"))
	end
end

function DrawMap()
	LoadedMatrix = sample_map
	for CurrentRow = 0, Map_Height, 1 do
		DrawRow(CurrentRow, LoadedMatrix)
	end
end
i get the following error: draw.lua:30: attempt to index field '?' (a nil value)

if i change line 30 to the following:

Code: Select all

BlockData = map_matrix[1][1]
it works fine with it just drawing the first value of the first row.

can somebody tell me what i'm doing wrong here?
User avatar
chezrom
Citizen
Posts: 59
Joined: Tue May 28, 2013 11:03 pm
Location: France

Re: Getting a error with my draw code.

Post by chezrom »

In lua, by default, the first index of index elements in table is 1 not 0, so the row of your matrix go from 1 to 6, the column from 1 to 8
You try to access element 0 that does not exist

Some advice :
  • You can use #myTable to have the number of indexed elements in table.
  • please in future, give the code that match the line number
  • each time you want to draw a tile you load the image :huh: ? load all your image once, and reuse the loaded image.
FoundationIDE
Prole
Posts: 6
Joined: Wed Jun 05, 2013 6:51 pm

Re: Getting a error with my draw code.

Post by FoundationIDE »

chezrom wrote:In lua, by default, the first index of index elements in table is 1 not 0, so the row of your matrix go from 1 to 6, the column from 1 to 8
You try to access element 0 that does not exist

Some advice :
  • You can use #myTable to have the number of indexed elements in table.
  • please in future, give the code that match the line number
  • each time you want to draw a tile you load the image :huh: ? load all your image once, and reuse the loaded image.
Thanks. I had the engine load all the images temporarily while I was making the drawing code. Its fixed now.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Getting a error with my draw code.

Post by davisdude »

Also, note that you put a comma at the end of the tile table.
sample_map = {
{1,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},
}
That may also be causing the problem, or it may have just been a mis-type. :crazy:
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Getting a error with my draw code.

Post by Robin »

davisdude wrote:Also, note that you put a comma at the end of the tile table.
No, that's okay. Commas are allowed after the final item, to make it easier to generate Lua code.
Help us help you: attach a .love.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Getting a error with my draw code.

Post by davisdude »

I never realized that! Thanks for that piece of information. :awesome:
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Getting a error with my draw code.

Post by Jasoco »

Robin wrote:
davisdude wrote:Also, note that you put a comma at the end of the tile table.
No, that's okay. Commas are allowed after the final item, to make it easier to generate Lua code.
You're darn right it does. You don't know how many times I pull my hair out when working with other languages that scream bloody murder if I leave an extra comma at the end. Especially when changing settings in Sublime Text's horribly outdated GUI-less preferences file structure. (Seriously, it's 2013. Give me a goddamned GUI!)
Post Reply

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 2 guests