Uncle
Warcraft Moderator
- Joined
- Aug 10, 2018
- Messages
- 7,609
I don't know what I'm doing, help me!
I'm trying to create Grid objects which contain a table of Tile objects that contain information about their x/y coordinates and an Image for visual purposes. I use the Grids table to store all of these Grid objects using [player id] as the first index and ["grid name"] as the second index, allowing Players to have as many Grid objects as they want.
The code seems to work fine during the creation process. It creates a Grid for Player 1 which contains a table of Tiles. These Tiles seem to be created properly since they print the correct information about their centerX and centerY coordinates and appear in-game with the correct Image settings.
However, when I loop over some of the Tiles of my Grid at the end of the Test() function I begin to get weird results.
I'm getting the same exact centerX and centerY values for all three Tiles I loop over. These are the values of the very last Tile created for the Grid so I assume the data is somehow being overwritten. What am I doing wrong here? Is it some kind of metatable issue? Is that newAutotable() function funky, I've been using it for a while now without issues.
Lua:
-- This creates a 2d, 3d, etc. array (table). It needs to be above the variable creation
function newAutotable(dim)
local MT = {};
for i=1, dim do
MT[i] = {__index = function(t, k)
if i < dim then
t[k] = setmetatable({}, MT[i+1])
return t[k];
end
end}
end
return setmetatable({}, MT[1]);
end
-- GRID SYSTEM --
Grids = newAutotable(2) -- Creates a 2d table using [player id]["grid name"]
Grid = { id, centerX, centerY, width, height, tile }
Tile = { grid, image, row, column, centerX, centerY, minX, maxX, minY, maxY }
function Grid:new(id, centerX, centerY, width, height)
local o = {}
setmetatable(o, self)
self.__index = self
self.id = id or 1
self.centerX = centerX or 128
self.centerY = centerX or 128
self.width = width or 5
self.height = height or 5
self.tile = {}
self:createGridTiles(centerX, centerY, width, height)
return o
end
function Grid:createGridTiles(startX, startY, width, height)
local i = 0
local img
local posX
local posY
startX = startX - 256
startY = startY - 256
startX = startX - (128 * (width / 2))
startY = startY - (128 * (height / 2))
for x = 1, width do
for y = 1, height do
i = i + 1
img = CreateImage("Gridplane.dds",128,128,0,0,0,0,1,1,1,1)
SetImageRenderAlways(img, true)
SetImageColor(img, 255, 255, 255, 255)
posX = startX + x * 128
posY = startY + y * 128
SetImagePosition(img, posX, posY, 0)
local t = Tile:new(self, img, x, y, posX - 64, posY - 64, posX - 128, posY - 128, posX + 128, posY + 128)
self.tile[i] = t
print(i)
print(t)
print(self.tile[i].centerX.." / "..self.tile[i].centerY)
end
end
end
function Tile:new(grid, image, row, column, centerX, centerY, minX, minY, maxX, maxY)
local o = {}
setmetatable(o, self)
self.__index = self
self.grid = grid
self.image = image
self.row = row
self.column = column
self.centerX = centerX
self.centerY = centerY
self.minX = minX
self.minY = minY
self.maxX = maxX
self.maxY = maxY
return o
end
function Test()
local g = Grid:new(1, 128, 128, 5, 5)
Grids[g.id]["Build"] = g
print("Loop started")
for i = 1, 3 do
-- Get grid owned by player by grid name, then get tile at index, then print tile info
local g = Grids[1]["Build"]
local t = g.tile[i]
print(i)
print(t)
print(t.centerX.." / "..t.centerY)
end
end
The code seems to work fine during the creation process. It creates a Grid for Player 1 which contains a table of Tiles. These Tiles seem to be created properly since they print the correct information about their centerX and centerY coordinates and appear in-game with the correct Image settings.
However, when I loop over some of the Tiles of my Grid at the end of the Test() function I begin to get weird results.
I'm getting the same exact centerX and centerY values for all three Tiles I loop over. These are the values of the very last Tile created for the Grid so I assume the data is somehow being overwritten. What am I doing wrong here? Is it some kind of metatable issue? Is that newAutotable() function funky, I've been using it for a while now without issues.
Last edited: