Page 1 of 1
Understanding classes
Posted: Sat Nov 06, 2010 12:04 am
by foszor
Hi, I'm pretty new here but I'm very familiar with lua. I do have a question about classes, though. I know how to create and use them, but the variable "__HAS_SECS_COMPATIBLE_CLASSES__" is confusing. Do I need to use this in every file I construct a class in or just once in my game?
Re: Understanding classes
Posted: Sat Nov 06, 2010 12:07 am
by zac352
I recommend writing your own class system. Makes more sense to yourself. :d
Re: Understanding classes
Posted: Sat Nov 06, 2010 12:14 am
by foszor
I guess now I'm even more confused. I tried using the basic class system I had from other lua scripts and it threw an error. Can you provide me with a very simple, cut and dry way to create a new class?
Re: Understanding classes
Posted: Sat Nov 06, 2010 12:28 am
by zac352
foszor wrote:I guess now I'm even more confused. I tried using the basic class system I had from other lua scripts and it threw an error. Can you provide me with a very simple, cut and dry way to create a new class?
Code: Select all
function makeanewclass() --returns constructor
local n=newproxy(true)
local index={}
getmetatable(n).__index=index
getmetatable(n).__newindex=function(t,k,v)index[k]=v if string.sub(k,1,2)=="__" then getmetatable(n)[k]=v end end
getmetatable(n).__call=function() return newproxy(n) end --DO NOT TRUST THIS DIRTEH HACK, IT PROBABLY DOES NOT WORK.
return n
end
This is an example of a class script. I'm tired, so don't judge it.
Code: Select all
local myClass=makeanewclass()
myClass.myValue=4
local myObj=myClass()
myObj.myValue=3
print(myObj.value,myClass.value)--If this prints 4,4; which it probably will, this script has failed and you must refine the __call metamethod. :P
Re: Understanding classes
Posted: Sat Nov 06, 2010 9:33 am
by Robin
Don't listen to zac, for the love of cod.
foszor wrote:the variable "__HAS_SECS_COMPATIBLE_CLASSES__" is confusing. Do I need to use this in every file I construct a class in or just once in my game?
You don't have to use them at all. It's just a way so others can make class systems compatible to SECS, and your code can check whether it's compatible. I'd never use __HAS_SECS_COMPATIBLE_CLASSES__, don't worry about it.
Now, did you put the SECS code in a file called class.lua (or something like that) and did you require() that file?
Re: Understanding classes
Posted: Sat Nov 06, 2010 10:03 am
by bartbes
Yeah, I've never ever seen it used
. Anyway, its goal is to make it possible to use another class system, without changing code, simply by checking if it's compatible.
Re: Understanding classes
Posted: Mon Nov 08, 2010 11:43 pm
by foszor
Okay thank you guys, that helped me