fork download
  1. Trophies = {} -- Global function, so you can access it from other files
  2. Trophies.__trophies = {}
  3.  
  4. function Trophies.GetAll() -- Returns all trophies
  5. return Trophies.__trophies
  6. end
  7.  
  8. function Trophies.Add(T) -- Adds a trophy, 2 different trophies with same ID cannot exist, newer one overwrites older one
  9. Trophies.__trophies[T.ID] = T
  10. end
  11.  
  12. function Trophies.GetByID(ID) -- Returns trophy by ID
  13. return Trophies.__trophies[ID]
  14. end
  15.  
  16. function Trophies.IsTrophy(T) -- Checks if a table is trophy
  17. if getmetatable(T) == TrophySkeleton then return true end
  18. return false
  19. end
  20.  
  21. TrophySkeleton = {} -- "Base class" of every new trophy
  22. TrophySkeleton.__index = TrophySkeleton
  23. setmetatable(TrophySkeleton, { __call = function(_, ...) return TrophySkeleton.New(...) end }) -- Allows you to use TrophySkeleton(A,B) instead of TrophySkeleton.New(A,B)
  24. TrophySkeleton.__TrophyLastID = 0 -- Automatic ID managment
  25.  
  26. function TrophySkeleton.New(nID, name, desc)
  27. if (nID == nil) then
  28. TrophySkeleton.__TrophyLastID = TrophySkeleton.__TrophyLastID + 1
  29. while (Trophies.GetByID(TrophySkeleton.__TrophyLastID) ~= nil) do
  30. TrophySkeleton.__TrophyLastID = TrophySkeleton.__TrophyLastID + 1
  31. end
  32. end
  33. local T = setmetatable({
  34.  
  35. ID = nID or TrophySkeleton.__TrophyLastID,
  36. Name = name or "",
  37. Desc = desc or "",
  38. Test = function(this)
  39. return ("ID: " .. tostring(this.ID) .. ", Name: " .. this.Name .. ", Desc: " .. this.Desc)
  40. end
  41.  
  42. -- Add custom fields and methods here, like in this example, to call methods you need to do TROPHY:METHOD() or TROPHY.METHOD(TROPHY)
  43.  
  44. }, TrophySkeleton) -- Creates new
  45. Trophies.Add(T)
  46. return T
  47. end
  48.  
  49. local A = TrophySkeleton.New(nil, "A", "Test One")
  50. print(A.ID, A.Name, A.Desc)
  51.  
  52. local B = TrophySkeleton(2, "B", "Test Two")
  53. print(B.ID, B.Name, B.Desc)
  54.  
  55. local C = TrophySkeleton.New(nil, "C", "Test Three")
  56. print(C.ID, C.Name, C.Desc)
  57.  
  58. print()
  59.  
  60. local TROPHY_2 = Trophies.GetByID(2)
  61. print(TROPHY_2.ID, TROPHY_2.Name, TROPHY_2.Desc)
  62. print(Trophies.GetByID(1):Test())
  63.  
  64. print()
  65.  
  66. print("A is trophy: " .. tostring(Trophies.IsTrophy(A)))
  67. print("{} is trophy: " .. tostring(Trophies.IsTrophy({})))
Success #stdin #stdout 0.01s 2540KB
stdin
Standard input is empty
stdout
1	A	Test One
2	B	Test Two
3	C	Test Three

2	B	Test Two
ID: 1, Name: A, Desc: Test One

A is trophy: true
{} is trophy: false