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
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
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
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)
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
From my understanding, that's what metatables are for. Also, I testedWell, 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.
and it had already inherited all the traits of theCode: Select all
gun_1 = gun.create( false, 100, 100, 100, 100, 100 ), gun_2 = gun.create(),
portion. I thought that's what metatables were for?Code: Select all
gun = { automatic = true, clip_size = 32, x = 0, y = 0, w = 100, h = 100 }
portion of your arbitrary, since you insert it in to gun anyway?Code: Select all
return newGun
My bad, you are definitely right on this.davisdude wrote: From my understanding, that's what metatables are for. Also, I testedand it had already inherited all the traits of theCode: Select all
gun_1 = gun.create( false, 100, 100, 100, 100, 100 ), gun_2 = gun.create(),
portion.Code: Select all
gun = { automatic = true, clip_size = 32, x = 0, y = 0, w = 100, h = 100 }
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
Indeed it is. But it won't make any real difference unless you are dealing with a large number of items when iterating.davisdude wrote: I didn't know pairs was slower...
Code: Select all
local t = {'a', 'b', true, 4}
for k,v in ipairs(t) do
print(k,v)
end
Code: Select all
local t = {
a = 1,
b = 2,
c = 3
}
for k,v in pairs(t) do
print(k,v)
end
Well, not exactly. In general, anytime one inits a new objects, he is likely to store this object in a variable.davisdude wrote: Finally, isn't theportion of your arbitrary, since you insert it in to gun anyway?Code: Select all
return newGun
Code: Select all
local myGun = gun.create(...)
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.
Users browsing this forum: No registered users and 1 guest