Instantiation issue?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
shiftyp
Prole
Posts: 2
Joined: Thu May 26, 2016 12:49 am

Instantiation issue?

Post 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
shiftyp
Prole
Posts: 2
Joined: Thu May 26, 2016 12:49 am

Re: Instantiation issue?

Post 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:

Code: Select all

tables at different addresses
2     2
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.
marco.lizza
Citizen
Posts: 52
Joined: Wed Dec 23, 2015 4:03 pm

Re: Instantiation issue?

Post 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

Code: Select all

:
specifier in the allocator.
User avatar
rmcode
Party member
Posts: 454
Joined: Tue Jul 15, 2014 12:04 pm
Location: Germany
Contact:

Re: Instantiation issue?

Post 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

Code: Select all

:
specifier in the allocator.
It depends. I use it to differentiate between class and object functions.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Instantiation issue?

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 1 guest