## Main.lua not recognizing a function. [SOLVED]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Trogg
Prole
Posts: 2
Joined: Sun Jun 08, 2014 9:30 pm

### Main.lua not recognizing a function. [SOLVED]

I'm having a problem where a certain function in my main.lua is simply not being recognized, or something of the sort.

The error that I'm getting is:
main.lua:18: attempt to call field 'draw' (a nil value)

from what I understand, that means that for some reason main.lua thinks that a function called map.draw() doesn't exist, right?

Here's my code.

main.lua

Code: Select all

require "map"
require "player"

window = love.window.setMode(640, 480)

end

function love.update(dt)
player.update(dt)
--map.update(dt)
end

function love.draw()
player.draw()
map.draw()
end
map.lua

Code: Select all

map = {}

map = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
}

map.mapWidth = #map[1]
map.mapHeight = #map
map.tileWidth = 32
map.tileHeight = 32
end

function drawMap()
for y = 1, map.mapHeight do
for x = 1, map.mapwidth do
if map[y][x] == 1 then
love.graphics.setColor(255, 255, 0)
love.graphics.rectangle("fill", (x-1)*map.tileWidth, (y-1)*map.tileHeight, map.tileWidth, map.tileHeight)
end
end
end
end

function map.draw()
drawMap()
end
What really confuses me is that my player.lua works just fine, loads and draws correctly. And if I put map.lua in a separate project and run it on it's own, it works. It's just the linking between main and map that's the problem. I'm completely stumped, I feel that I'm missing something really obvious or I've just made a stupid mistake, hopefully somebody can help me out here.
Last edited by Trogg on Mon Jun 09, 2014 4:33 am, edited 1 time in total.

substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

### Re: Main.lua not recognizing a function.

Aside from having a little typo in your draw loop, you are redefining the 'map' table from a table consisting of the .load and .draw functions, to the actual map data and parameters.

Code: Select all

map = {}

map = {  -- This line is your problem.
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
....

Currently designing themes for WordPress.

Sometimes lurks around the forum.

Trogg
Prole
Posts: 2
Joined: Sun Jun 08, 2014 9:30 pm

### Re: Main.lua not recognizing a function.

Ah, thank you so much, I knew it was a silly mistake.

substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

### Re: Main.lua not recognizing a function.

Trogg wrote:Ah, thank you so much, I knew it was a silly mistake.
No problem!

Also, welcome to the forums!
Currently designing themes for WordPress.

Sometimes lurks around the forum.

### Who is online

Users browsing this forum: No registered users and 9 guests