View Single Post
  #2  
Unread 03-07-2021, 08:26 AM
Garan's Avatar
Garan Garan is offline
The Undying
Interface Author - Click to view interfaces
 
Join Date: Oct 2010
Posts: 341
It seems there are two versions of Class.lua that were published. The first, probaby published while Lua was still in Beta, possibly by another plugin author, uses metatables to determine class types. The second, published by Turbine, uses an internal table to keep track of class types. I include the first version in my plugins and have never used the IsA() function so I never noticed it doesn't seem to work for user classes (the Class() function works fine otherwise). It seems that the IsA() from the second version might work correctly for Turbine defined classes as well as user classes.

The second version is included in any of the libraries that are found by entering "Turbine" in the "Search Downloads" field. That may help.
EDIT: Note, the version of Class.lua in these libraries has a dependancy on the Type.lua also found in these libraries.

Hmmm, after some testing with the second version (the one from the libraries available here), I got mixed results. My simple test code was:
Code:
abc=class(Turbine.UI.Window)
function abc:Constructor()
	Turbine.UI.Window.Constructor(self)
end
w1=Turbine.UI.Window()
w2=abc()
def=class(abc)
function def:Constructor()
	abc.Constructor(self)
end
w3=def()

Turbine.Shell.WriteLine("w1,window:"..tostring(w1:IsA(Turbine.UI.Window)))
Turbine.Shell.WriteLine("w1,control:"..tostring(w1:IsA(Turbine.UI.Control)))
Turbine.Shell.WriteLine("w2,window:"..tostring(w2:IsA(Turbine.UI.Window)))
Turbine.Shell.WriteLine("w2,control:"..tostring(w2:IsA(Turbine.UI.Control)))
Turbine.Shell.WriteLine("w2,abc:"..tostring(w2:IsA(abc)))
Turbine.Shell.WriteLine("w3,window:"..tostring(w3:IsA(Turbine.UI.Window)))
Turbine.Shell.WriteLine("w3,control:"..tostring(w3:IsA(Turbine.UI.Control)))
Turbine.Shell.WriteLine("w3,abc:"..tostring(w3:IsA(abc)))
Turbine.Shell.WriteLine("w3,def:"..tostring(w3:IsA(def)))

Turbine.Shell.WriteLine("abc,window:"..tostring(abc:IsA(Turbine.UI.Window)))
Turbine.Shell.WriteLine("def,window:"..tostring(def:IsA(Turbine.UI.Window)))
Turbine.Shell.WriteLine("def,abc:"..tostring(def:IsA(abc)))
The results were:
Code:
w1,window:true
w1,control:true
w2,window:false
w2,control:false
w2,abc:true
w3,window:false
w3,control:false
w3,abc:true
w3,def:true
abc,window:true
def,window:false
def,abc:true
so,
instances of system defined classes will show true for the class they instantiate and parent classes
instances of user defined classes will not show as instances of the system defined parent of the class they instantiate but will show any user defined class in the hierarchy
it works similarly for the classes themselves with a disconnect at the first user defined class in the hierarchy,
user classes directly derived from a system class will show as derived from that class (and it's parents) but any user class derived from a user class will only show true for the user defined parents in it's hierarchy.

Odd. But, as mentioned earlier, I don't use IsA() anyway so this is about as far as I will investigate, but it does look like there might be a bug in the Turbine implementation

Anyone else actually use the IsA() function that can shed more light?

EDIT2: it seems my version actually came from a plugin author, source currently unknown. I suspect it came from an author, not Turbine, due to the use of the variable "klass" which would imply a non-english source, swedish perhaps?

Last edited by Garan : 03-07-2021 at 12:56 PM.
Reply With Quote