View Single Post
11-24-18, 02:06 PM   #5
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Lua Code:
  1. local allFrames = {}
  2. local framesConstructor = {}
  3. function framesConstructor:create(name, width, height, constructor, updater)--method for frame creation - simulates OOP
  4.     self.frame = CreateFrame("Frame", name .. "BackgroundFrame", mainFrame)
  5.     self.name = name
  6.     self.height = height
  7.     self.width = width
  8.     self.constructor = constructor
  9.     self.updater = updater
  10.     return self
  11. end
  12.  
  13. function framesConstructor:create2(name, width, height, constructor, updater)--method for frame creation - simulates OOP
  14.     local tbl = {}
  15.     tbl.frame = CreateFrame("Frame", name .. "BackgroundFrame", mainFrame)
  16.     tbl.name = name
  17.     tbl.height = height
  18.     tbl.width = width
  19.     tbl.constructor = constructor
  20.     tbl.updater = updater
  21.     return tbl
  22. end
  23.  
  24. tinsert(allFrames, framesConstructor:create("runicpower", 120, 16, "setupRpower", "updateRpower"))
  25. tinsert(allFrames, framesConstructor:create("runes", 120, 12, "setupRunes", "updateRunes"))
  26. tinsert(allFrames, framesConstructor:create("diseases", 64, 64, "setupDis", "updateDis"))
  27.  
  28. tinsert(allFrames, framesConstructor:create2("runicpower", 120, 16, "setupRpower", "updateRpower"))
  29. tinsert(allFrames, framesConstructor:create2("runes", 120, 12, "setupRunes", "updateRunes"))
  30. tinsert(allFrames, framesConstructor:create2("diseases", 64, 64, "setupDis", "updateDis"))
  31.  
  32. for i=1, #allFrames do
  33.     print(allFrames[i].constructor)
  34. end

The first 3 tinserts replicate what you are doing and in the print, they all end up containing the information of the last call to create() because "return self" means return framesConstructor (always returning the same table, not a new instance). The last 3 create/return a new table each call containing the passed information.

If you wanted an inherited "instance" of framesConstructor for each entry:

Lua Code:
  1. tinsert(allFrames, Mixin({}, framesConstructor))
  2. allFrames[#allFrames]:create("runicpower", 120, 16, "setupRpower", "updateRpower")
  3.  
  4. tinsert(allFrames, Mixin({}, framesConstructor))
  5. allFrames[#allFrames]:create("runes", 120, 12, "setupRunes", "updateRunes")
  6.  
  7. tinsert(allFrames, Mixin({}, framesConstructor))
  8. allFrames[#allFrames]:create("diseases", 64, 64, "setupDis", "updateDis")
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-24-18 at 03:55 PM.
  Reply With Quote