__ and Functions

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

__ and Functions

Post by davisdude »

I was wondering if anybody had any good explanations on __ and how it works. I was never quite sure. I looked on PIL and it yearned no conclusive results. So I thought I would ask the trust LOVE community! :awesome:
And while I have you, how would you make a named function?
For example:

Code: Select all

thing = create( 0, 0, 32, 32 ) --x, y, w, h of thing
thing:move( 'w', 'a', 's', 'd' )-- Keys to control thing
What would the function look like?
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
chezrom
Citizen
Posts: 59
Joined: Tue May 28, 2013 11:03 pm
Location: France

Re: __ and Functions

Post by chezrom »

the lua reference manual explains all the __ functions : meta-tables
the explanations here are clear and worth the effort to read.
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: __ and Functions

Post by ejmr »

This may also be helpful, although I also agree that reading the official manual is worth it.

http://nova-fusion.com/2011/06/30/lua-m ... -tutorial/
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: __ and Functions

Post by davisdude »

I looked at both these and have a much better understanding of metatables! Thanks :awesome:
What about the function part?
EDIT
No need to fear! After a bit of experimentation, I figured it out on my own! Here's my example:

Code: Select all

function love.load() 
	guns = {
		gun_1 = gun.create( false, 100, 100, 100, 100, 100 ), 
		gun_2 = gun.create(), 
	}
end

function love.draw() 
	for a, g in pairs( guns ) do
		g:draw()
	end
end

function love.update( dt ) 
end
gun = { automatic = true, clip_size = 32, x = 0, y = 0, w = 100, h = 100 }
gun.__index = gun

function gun.create( automatic, clip_size, x, y, w, h )
	local t = {}
	setmetatable( t, gun )
	
	t.automatic = automatic
	t.clip_size = clip_size
	t.x, t.y, t.w, t.h = x, y, w, h
	return t
end

function gun:draw()	
	love.graphics.rectangle( 'fill', self.x, self.y, self.w, self.h )
end
Please let me know if there's a way I could make this more efficient! :awesome:
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: __ and Functions

Post by Roland_Yonaba »

Well, considering the attributes of guns instances, maybe some of them can have default values.
If so, you can take advantage of it so that you won't need to pass them as args everytime you are calling gun.create.

Code: Select all

function gun.create(valueA, valueB, optValueC, optValueD)
  local newGun = setmetatable({}, gun)
  newGun.attrA = valueA
  newGun.attrB = valueB
  newGun.attrC = valueA or (DefaultValueD)
  newGun.attrD = optValueD or (DefaultValueD)
  ...
  return newGun
end

aGun = gun.create(1,2,3,4)
anotherGun gun.create(1,2)
Second, I can see that all your guns instances are kept inside a table named guns, but you are sorting them using strings ("gun_1", "gun_2"). Is it really needed to do so ? You can simply store them with integer keys, which will let you iterate though the collection using ipairs (known to be a lot faster than pairs). In that case, the factory function can include this and automatically register each single new instance of gun you are creating.

Code: Select all

local guns = {}
...
function gun.create(...)
    local newGun = setmetatable({}, gun)
    ....
    guns[#guns+1] = newGun
    return newGun
end

...

-- Iterating through the collection of guns
for i, gun in ipairs(guns)
    ...
end
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: __ and Functions

Post by davisdude »

Well, considering the attributes of guns instances, maybe some of them can have default values.
If so, you can take advantage of it so that you won't need to pass them as args everytime you are calling gun.create.
From my understanding, that's what metatables are for. Also, I tested

Code: Select all

      gun_1 = gun.create( false, 100, 100, 100, 100, 100 ), 
      gun_2 = gun.create(), 
and it had already inherited all the traits of the

Code: Select all

gun = { automatic = true, clip_size = 32, x = 0, y = 0, w = 100, h = 100 }
portion. I thought that's what metatables were for?
I didn't know pairs was slower, but I did do the name simply because I was just wondering how you would do it.
Finally, isn't the

Code: Select all

return newGun
portion of your arbitrary, since you insert it in to gun anyway?
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: __ and Functions

Post by Roland_Yonaba »

davisdude wrote: From my understanding, that's what metatables are for. Also, I tested

Code: Select all

      gun_1 = gun.create( false, 100, 100, 100, 100, 100 ), 
      gun_2 = gun.create(), 
and it had already inherited all the traits of the

Code: Select all

gun = { automatic = true, clip_size = 32, x = 0, y = 0, w = 100, h = 100 }
portion.
My bad, you are definitely right on this.
I did not not noticed that default values were already defined in the gun table prototype. Just take into account that those properties are not available on instantiation, but right at the moment you will need them. Practically it doesn't raise any issue, but it is good to know that to fully understand what metatables does.
For instance, you can try the following after creating a new gun object.

Code: Select all

local gun_A = gun.create()
-- Print all of its properties and values
for k,v in pairs(gun_A) do print(k,v) end

-- Check for a property
print(gun_A.automatic) --> true
-- Print all of its properties and values again
for k,v in pairs(gun_A) do print(k,v) end]

-- and so on
davisdude wrote: I didn't know pairs was slower...
Indeed it is. But it won't make any real difference unless you are dealing with a large number of items when iterating.
The thing is, ipairs iterates on the assumption that the collection you are cycling through contains elements sorted numerically with integers, while pairs does not follow any particular order. Basically, it is a common behavior for Lua users to use ipairs for array-like tables (pairs will also work here).

Code: Select all

local t = {'a', 'b', true, 4}
for k,v in ipairs(t) do 
  print(k,v)
end
And pairs for map-like tables, where entries are not likely to be indexed with keys (ipairs will just fail on such tables).

Code: Select all

local t = {
  a = 1,
  b = 2,
  c = 3
}
for k,v in pairs(t) do 
  print(k,v)
end
If you want to dig deeper, see TableTutorial.
davisdude wrote: Finally, isn't the

Code: Select all

return newGun
portion of your arbitrary, since you insert it in to gun anyway?
Well, not exactly. In general, anytime one inits a new objects, he is likely to store this object in a variable.

Code: Select all

local myGun = gun.create(...)
That is the reason for what the factory function should return something. But right before that, it also internally registers this new instance inside a collection, which is very convenient too if one needs to cycle through all those instances later on.
On the other hand, this one would have been terrific:

Code: Select all

gun.create(....) -- Create a new gun object, which will be stored last in the collection
local myNewGun = #guns -- Get the last gun created.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: __ and Functions

Post by davisdude »

Okay, I understand now! Thanks! :awesome:
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests