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
Before you make a thread asking for help, read this.
Post Reply
Trogg
Prole
Posts: 2
Joined: Sun Jun 08, 2014 9:30 pm

Main.lua not recognizing a function. [SOLVED]

Post by Trogg »

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"

function love.load()
	window = love.window.setMode(640, 480)
	
	player.load()
	map.load()
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 = {}

function map.load()
	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.
User avatar
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.

Post by substitute541 »

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 = {}

function map.load()
   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.

Post by Trogg »

Ah, thank you so much, I knew it was a silly mistake.
User avatar
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.

Post by substitute541 »

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.
Post Reply

Who is online

Users browsing this forum: No registered users and 61 guests