Quantcast Help with Lua OO - WoWInterface
Thread Tools Display Modes
12-31-18, 07:01 PM   #1
mtp1032
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 20
Help with Lua OO

Here is an error I'm having trouble tracking down.

Message: Interface\AddOns\SandBox\libs\UnitTests.lua:38: attempt to call method 'is_a' (a string value)

Here's my class declaration followed by the _init(...) definition

Lua Code:
  1. Slot = {}
  2. Slot.__index = Slot
  3.  
  4. setmetatable(Slot, {
  5.     __index = Container,        -- makes the inheritance work
  6.      __call = function (cls, ...)
  7.     local self = setmetatable({}, cls)
  8.     self:_init(...)
  9.     return self
  10.   end,
  11. })
  12.  
  13. -- This is the _init function.
  14. function Slot:_init( bagNumber, slotIndex )
  15.            
  16.     Container._init(self)               -- call the base class constructor
  17.     self.is_a = "Slot"                   -- in the parent class, this is set to "Virtual Container"
  18. end
  19.  
  20. -- the is_a() method
  21. function Slot:is_a()
  22.    return self.is_a
  23. end

Now, here's the test code that elicits the error:

Lua Code:
  1. local s = Slot(1,1)
  2. DEFAULT_CHAT_FRAME:AddMessage( s:is_a() )

It fails with the message shown above. Both function and property work as expected in the base class.

Any thoughts?

Last edited by mtp1032 : 12-31-18 at 08:01 PM. Reason: Forgot to copy "Slot.__index = Slot"
  Reply With Quote
12-31-18, 07:59 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,134
Code:
function Slot:is_a()
adds the method to the table when it is "built". Later when you call Slot:_init()
Code:
local s = Slot(1,1)
Code:
self.is_a = "Slot"
overwrites the function(method) with the string causing
Code:
DEFAULT_CHAT_FRAME:AddMessage( s:is_a() )
to try and run it as a method when it's now a string.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 12-31-18 at 08:14 PM.
  Reply With Quote
12-31-18, 11:17 PM   #3
mtp1032
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 20
Well, thanks as always, Fizzlemizz but I'm not sure I understood your suggestion. For example, I rewrote the method and changed its name from this (the original):
Code:
function Slot:is_a()
    return self.is_a
end
to this (note the changed name):
Code:
function Slot:type()
    return self.is_a
end
and I still get the same error, i.e., interpreting 'type' as a string.

So, after all this, did I misunderstand your suggestion?

Cheers,

Last edited by mtp1032 : 01-01-19 at 07:21 AM. Reason: Clarification and misspelling
  Reply With Quote
01-01-19, 12:06 AM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,134
Your original code with a few print statements

Lua Code:
  1. Slot = {}
  2. print("Slot =", Slot)
  3. Slot.__index = Slot
  4.      
  5. setmetatable(Slot, {
  6.         __index = Container,        -- makes the inheritance work
  7.          __call = function (cls, ...)
  8.         local self = setmetatable({}, cls)
  9.         self:_init(...)
  10.         return self
  11.       end,
  12. })
  13.      
  14.     -- This is the _init function.
  15. function Slot:_init( bagNumber, slotIndex )
  16.                
  17.         --Container._init(self)               -- call the base class constructor
  18. print("self =", self)
  19.         self.is_a = "Slot"                   -- in the parent class, this is set to "Virtual Container"
  20. end
  21.      
  22.     -- the is_a() method
  23. function Slot:is_a()
  24.        return self.is_a
  25. end
  26.    
  27. local s = Slot(1,1)
  28. print("Finally s =", s)
  29. print("Value of is_a", s.is_a)

At each print (excluding the last) you will notice the the table is the same. That means that during the intitialisation of s (local s = Slot(1,1)) when the code gets too:
Code:
self.is_a = "Slot"
you're actually doing
Code:
Slot.is_a = "Slot"
In effect overwriting the original funtion declaration of:
Code:
function Slot:is_a()
       return self.is_a
end
and turning it into the string "Slot"

I think you're thinking that s should actually be a new table entry in the Slot table.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 01-01-19 at 12:51 AM.
  Reply With Quote
01-01-19, 07:32 AM   #5
mtp1032
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 20
Originally Posted by Fizzlemizz View Post
I think you're thinking that s should actually be a new table entry in the Slot table.
You're right! That's exactly what I am (was) thinking. I'll need to sketch the tables to grok this.

Thanks and cheers,
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Help with Lua OO

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off