Page 1 of 1
Instantiation issue?
Posted: Thu May 26, 2016 12:54 am
by shiftyp
Code: Select all
function TiledMap:findPath(startX, startY, endX, endY)
openlist = {}
closedlist = {}
startTile = self:getTile(startX, startY)
endTile = self:getTile(endX, endY)
table.insert(openlist, startTile)
print(startTile.x, startTile.y)
...
startTile is always the same as the last call to getTile. So in the example above, startTile is the same as endTile, but only after I call endTile = self:getTile. I can't figure it out. Probably something small I'm missing, I've been staring at it too long.
Code: Select all
function TiledMap:getTile(tileX, tileY)
layer = self.data.layers[1]
tileid = layer.data[tileY * layer.width + tileX]
tile = Tile:new(tileX, tileY, tileid, self.data.tilesets[1].tiles[1].properties)
print("tile = ", tile.x, tile.y)
return tile
end
.....
function Tile:new(tileX, tileY, gid, properties)
new_inst ={}
setmetatable(new_inst, Tile_mt)
self.id = gid
self.x = tileY
self.y = tileX
print("Tile new", self.x, self.y)
self.properties = properties
return new_inst
end
Re: Instantiation issue?
Posted: Fri May 27, 2016 1:00 am
by shiftyp
Code: Select all
test = {}
test.__index = test
function test:new(x)
local newTest = {}
setmetatable(newTest, test)
self.x = x
return newTest
end
t1 = test:new(1)
t2 = test:new(2)
print(t1, t2)
print(t1.x, t2.x)
The output of the print call is:
The addresses of the tables are different, but the x value is the same. Why would this be?
EDIT:
Got it. it needs to be newTest.x = x instead of self.x
I'm stupid.
Re: Instantiation issue?
Posted: Fri May 27, 2016 9:33 am
by marco.lizza
shiftyp wrote:Got it. it needs to be newTest.x = x instead of self.x
I'm stupid.
It should also be
Code: Select all
function test.new(x)
local self = {}
setmetatable(self, test)
self.x = x
return self
end
in my opinion. There's no point in using the
specifier in the allocator.
Re: Instantiation issue?
Posted: Fri Jun 10, 2016 12:22 am
by rmcode
You don't need to use a metatable approach ... you could just go for straight closure based OOP:
Code: Select all
local ClassName = {};
function ClassName.new()
local self = {};
local baz = 'Foo is Löve, All is Löve' -- Private attribute
self.bazooka = true -- Public attribute
-- Private function
local function bar()
-- Do stuff
end
-- Public function
function self:foo()
-- Do stuff
end
return self;
end
return ClassName;
Example for inheritance:
Code: Select all
local ParentClass = {};
function ParentClass.new()
local self = {};
return self;
end
return ParentClass;
Code: Select all
local ChildClass = {};
function ChildClass.new()
local self = ParentClass.new(); -- Use the table from the parent class.
return self;
end
return ChildClass;
It's how I code all my Lua projects. I like the simplicity and the clean code it gives you.
marco.lizza wrote:
in my opinion. There's no point in using the
specifier in the allocator.
It depends. I use it to differentiate between class and object functions.
Re: Instantiation issue?
Posted: Fri Jun 10, 2016 6:47 am
by zorg
Just to seem helpful, gonna link
this post in here as well, as to not duplicate what has once already been written down.