Disappearing Table Field
Posted: Wed Jan 19, 2011 3:00 am
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.
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.
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
Thanks for any insight.