Disappearing Table Field

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
eclectocrat
Prole
Posts: 2
Joined: Wed Jan 19, 2011 2:51 am

Disappearing Table Field

Post by eclectocrat »

Hi,

I am just starting with LOVE on OS X 10.6 and have come upon an unusual problem that has stumped me. I will post the code in it's entirety because it is so short and simple.

Code: Select all

---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
Tile =
{
	new = function (w, h)
		local ret = {}
		for k,v in pairs(Map) do
			ret[k] = v
		end
		ret.w = w
		ret.h = h
		return ret
	end,

	w = 0,
	h = 0,

	draw = function (self, x, y)
		love.graphics.rectangle("fill", x, y, self.w, self.h)
	end,
}
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
Map =
{
	new = function (w, h, tilew, tileh)
		local ret = {}
		for k,v in pairs(Map) do
			ret[k] = v
		end
		
		ret.tiles = {}
		for i = 1,w do
			local array = {}
			for j = 1,h do table.insert(array, nil) end
			table.insert(ret.tiles, array)
		end
		ret.w = w
		ret.h = h
		ret.tilew = tilew
		ret.tileh = tileh
		return ret
	end,
	
	tile = function (self, x, y, t)
		t = t or self.tiles[x][y]
		self.tiles[x][y] = t
		return t
	end,
	
	draw = function (self, sx, sy)
		if self.tiles then
			sx = sx or 0
			sy = sy or 0
			love.graphics.setColor(0, 128, 128, 255)
			for x = 1,self.w do
				for y = 1,self.h do
                                        -- self.tile is nil HERE
					local t = self.tiles[x][y]
					if t then t:draw(
						sx + (x - 1) * self.tilew, 
						sy + (y -1 ) * self.tileh)
					end
				end
			end
		else
			-- ALWAYS END UP HERE...
			love.graphics.print(
				"WTF? self.tiles = " .. tostring(self.tiles), 200, 200)
		end
	end,
}
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------

-- GLOBAL OBJECTS
world 	= nil
	
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
function love.load()
	local font = love.graphics.newFont(12)
	love.graphics.setFont(font)
	love.graphics.setColor(0,0,0,255)
	love.graphics.setBackgroundColor(255,255,255)
	
	math.randomseed(os.time())
	
	world = Map.new(20, 20, 16, 16)
	local tile = Tile.new(16, 16)
	
	for x = 1,world.w do
		for y = 1,world.h do
			if math.random(10) > 6 then
				world:tile(x, y, tile)
			end
		end
	end
end
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
function love.draw()
	-- THIS ALWAYS PRINTS A VALID TABLE ADDRESS
	love.graphics.print("PRE: world.tiles = " .. tostring(world.tiles),
		200, 100)
		
	world:draw()
	
	-- THIS ALWAYS PRINTS A VALID TABLE ADDRESS TOO
	love.graphics.print("POST: world.tiles = " .. tostring(world.tiles),
		200, 300)
end
So only in the world.draw function is world.tiles equal to nil. Whats weirder is that world.w and world.h are not nil, and self is not nil. Please, can anyone reproduce this bug? Am I missing something painfully obvious?

Thanks for any insight.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Disappearing Table Field

Post by BlackBulletIV »

Well I've spent a bit of time with your code and here's what I can see. Somehow, in your Map.draw method, someone how you're getting into some sort of recursion. I printed the value of world.tiles out the console before calling draw, and inside of draw and after draw. This is what I got:

Code: Select all

1 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - nil
2 - table: 0x1462c80
2 - table: 0x1462c80
2 - table: 0x1462c80
3 - table: 0x1462c80
The number 1 is before, 2 is in inside, and 3 is after. After I get print out 3, I quit the program (using "love.event.push('q')"). This obviously shows some recursion is occurring, and sometimes the table is a real table, other times it's not. I suspect the problem is here:

Code: Select all

if t then t:draw(
   sx + (x - 1) * self.tilew, 
   sy + (y -1 ) * self.tileh)
end
I think that somehow, t:draw() is mapping to the world object, but the proper self is not being provided. I've got no idea how this is possible (I'd have to look a lot harder I suspect), but this is the only call to a method named draw inside of the Map.draw method. Hopefully this information should be able to help you work out the problem.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Disappearing Table Field

Post by Robin »

I can't tell right now, but one thing I can see is that you're doing

Code: Select all

      local ret = {}
      for k,v in pairs(Map) do
         ret[k] = v
      end
rather than

Code: Select all

      local ret = setmetatable({}, {__index = Map}) -- the second table could be defined elsewhere
-- you could even have Map.__index = Map somewhere and use setmetatable({}, Map) here.
Help us help you: attach a .love.
eclectocrat
Prole
Posts: 2
Joined: Wed Jan 19, 2011 2:51 am

Re: Disappearing Table Field

Post by eclectocrat »

Aha! BlackBulletIV, thanks! I was mistakenly copying the Map prototype instead of the Tile prototype when making new tiles, thats where the recursion came in. Thanks a lot for your help!
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Disappearing Table Field

Post by BlackBulletIV »

There we go! I've run into many problems like this already; so obvious when you find them, but utterly confusing at first glance. No worries mate. ;)
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests