LUA Library Question

I’m in the middle of making an addon for TTT which implements in-game achievements, but I’ve hit a hard point in development. I want all achievements to use the same functions, such as Achievement:Give(), Achievement:GetByID(), etc etc. When making a new achievement, I use a separate file for each, setting the information for said achievement manually, then adding it to one big library of all achievements and their information stored into one big… table-thing. How would I go about doing this? I’ve tried using metatables, but they honestly confuse me a lot, so if someone can explain it in detail, that would be great. What I ultimately want is…

All trophies stored in one place:
[LUA]Table:
AchievementID:
AchievementInformation
AchievementID:
AchievementInformation
…[/LUA]

Example
[LUA]1:
1:
Name = “First Timer”
Desc = “blahbahlbahlb”
2:
Name = “asdfasdfasdf”
Desc = “asdfasdfasdfasdfasdfasdf”
…[/LUA]

I could access those achievements by ID, extracting the information from within with one function. Any ideas? This is what I’ve got so far, sorry if it’s wrong or incredibly dumb.

Library File
[LUA]Trophy = {};
Trophy_Meta = {__index = Trophy_Meta};

function Trophy:New(tab)
setmetatable(tab, Trophy.Meta);
return tab;
end

function Trophy:GetAll()
return self;
end

function Trophy:GetByID(id)
for k, v in pairs(self) do
if k == id then
return v;
end
end

return nil;

end[/LUA]

Example Achievement File
[LUA]local NewTrophy = {};
NewTrophy.TrophyID = 1;
NewTrophy.TrophyName = “First Timer”;
NewTrophy.TrophyDesc = “Join the server for the first time.”;
NewTrophy.TrophyMat = “vgui/trophies/first_timer”;
NewTrophy.MaxVal = nil;
NewTrophy = Trophy:New(NewTrophy);[/LUA]

Add me on steam, i can help you with lua.

EDIT:

behold:

[lua]
– Metatable
local LuaVector = {}
LuaVector.__index = LuaVector
LuaVector.__tostring = function(self)
return string.format(“Vector <%0.2f, %0.2f, %0.2f>”, self.x, self.y, self.z)
end

– Constructor
function LuaVector.new(x, y, z)
local t = {
x = x or 0,
y = y or 0,
z = z or 0
}
return setmetatable(t, Vector)
end

– Example methods
function LuaVector:__add(vec)
return Vector.new(self.x + vec.x, self.y + vec.y, self.z + vec.z)
end
function LuaVector:__sub(vec)
return Vector.new(self.x - vec.x, self.y - vec.y, self.z - vec.z)
end
function LuaVector:__unm(vec)
return Vector.new(-self.x, -self.y, -self.z)
end
function LuaVector:Length()
return math.sqrt(self.x^2 + self.y^2 + self.z^2)
end

– Example usage
local pos = LuaVector.new(1, 0, 1)
print(pos) – “Vector <1.00, 0.00, 1.00>”
print(math.Round(pos:Length(), 2)) – 1.41

local pos2 = pos + LuaVector.new(1, 4, 5)
print(pos2) – “Vector <2.00, 4.00, 6.00>”
print(math.Round(pos2:Length(), 2)) ) – 7.48

local pos3 = pos1 - pos2
print(pos3) – “Vector <-1.00, -4.00, -5.00>”
print(-pos3) – “Vector <1.00, 4.00, 5.00>”
[/lua]

This looks simple enough, I think I’ll be able to base my stuff off this. Thank you!