Can I cram all of this into a table?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Can I cram all of this into a table?

Post by KayleMaster »

Code: Select all

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.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Can I cram all of this into a table?

Post by zorg »

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:

Code: Select all

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 :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.
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Re: Can I cram all of this into a table?

Post by KayleMaster »

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.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Can I cram all of this into a table?

Post by zorg »

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. :awesome:
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.
User avatar
Drak
Prole
Posts: 2
Joined: Mon Apr 17, 2017 12:59 am

Re: Can I cram all of this into a table?

Post by Drak »

You could change your draw functions to something like:

Code: Select all

		draw = function ()
			logo.width = logo.width or logo.sprite:getWidth();
			logo.height = logo.height or logo.sprite:getHeight();
			
			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)
But as noted, you can't call "getWidth()" when initializing a table. AFAIK.
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Re: Can I cram all of this into a table?

Post by KayleMaster »

Oh shit, that's very clever. Still getting used to all the tricks in Lua.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Can I cram all of this into a table?

Post by zorg »

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. :3
(Not that it would have a terrible impact on performance, but still)
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.
MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: Can I cram all of this into a table?

Post by MasterLee »

When the variables sprite,x,y,width,height are not used anywhere else why not use an closure?

Code: Select all

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)()
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Re: Can I cram all of this into a table?

Post by KayleMaster »

Cus I want OOP

Code: Select all

local inputBox = {
		sprite = love.image.newImageData( 1, 1 ),
		x = 0,--width/4,
		y = 0,--height/6+100,
		width = 1,
		height = 1,
		input_string = ""
	}
	function inputBox:new (o)
		o = o or {}
		setmetatable(o, self)
    	self.__index = self
      return o
    end
	function inputBox:draw() 
		love.graphics.setColor(255, 255, 255, 255) --maybe redundant
		love.graphics.draw(self.sprite,self.x,self.y, 0, 1, 1, self.width/2, self.height/2)
		love.graphics.printf(input_string, window_width/4-self.width/2+20, window_height/6+100-6, 400, "left" )
	end
	function inputBox:init()
		self.width = self.sprite:getWidth()
		self.height = self.sprite:getHeight()
	end
usernameBox = inputBox:new{sprite = love.graphics.newImage("resources/username_box.png"),x = width/4, y = height/6+100}
usernameBox:init()
passwordBox = inputBox:new{sprite = love.graphics.newImage("resources/password_box.png"),x =width/4,  y = height/6+160}
passwordBox:init()
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Can I cram all of this into a table?

Post by raidho36 »

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.
Post Reply

Who is online

Users browsing this forum: Jimanzium and 32 guests