local logo = {
sprite = love.graphics.newImage("resources/logo.png"),
x = window_width/4,
y = window_height/6+20,
width = 1,
height = 1,
draw = function () end
}
logo.width = logo.sprite:getWidth()
logo.height = logo.sprite:getHeight()
logo.draw = function ()
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(logo.sprite, logo.x, logo.y,0, 1, 1, logo.width/2, logo.height/2)
end
If possible, I want to cram in width,height, and draw function in the table so it's in the brackets. I think I'm missing something, I'm pretty sure you should be able to do this.
Although this doesn't bother me as well, as it works just fine.
You can't, because you want to reference the left hand side of the definitions -while- everything's being defined;
But, you can do it a bit differently, if you want to:
local sprite = love.graphics.newImage("resources/logo.png")
local width, height = sprite:getWidth(), sprite:getHeight()
local draw = function()
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(logo.sprite, logo.x, logo.y,0, 1, 1, logo.width/2, logo.height/2)
end
local logo = {
sprite = sprite,
x = window_width/4,
y = window_height/6+20,
width = width,
height = height,
draw = draw, -- last comma allowed, but not required, of course.
}
Me and my stuff True 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.
KayleMaster wrote: ↑Thu Apr 13, 2017 8:06 pm
Thanks for the reply, I'll guess I'll stick with my method, because yours creates 4 locals that are there just to be inserted in the table later.
Yep! Never said mine was better.
Me and my stuff True 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.
IMO I'd rather have a tiny bit uglier definition that only gets executed once/when it needs to, than to do pointless things in a function that gets called a hundred times per second, but that may only be me.
(Not that it would have a terrible impact on performance, but still)
Me and my stuff True 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.
logo_draw = (function ()
local sprite = love.graphics.newImage("resources/logo.png")
local width2, height2 = sprite:getWidth()/2, sprite:getHeight()/2
local x = window_width/4,
local y = window_height/6+20,
return function ()
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(sprite, x, y,0, 1, 1, width2, height2)
end
end)()
Keep on mind that this way you create new copy of the whole function every time closure is generated. That consumes a good bit of memory, and every such function is unique, an in equality check will always fail. Closures exist for convenience -a function can carry it's own variables without needing an object for that- but not for optimization.