## Understanding classes

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
foszor
Prole
Posts: 3
Joined: Sat Nov 06, 2010 12:00 am
Location: California, USA
Contact:

### Understanding classes

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?
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Contact:

### Re: Understanding classes

I recommend writing your own class system. Makes more sense to yourself. :d
foszor
Prole
Posts: 3
Joined: Sat Nov 06, 2010 12:00 am
Location: California, USA
Contact:

### Re: Understanding classes

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?
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Contact:

### Re: Understanding classes

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
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Understanding classes

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?
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: Understanding classes

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.
foszor
Prole
Posts: 3
Joined: Sat Nov 06, 2010 12:00 am
Location: California, USA
Contact:

### Re: Understanding classes

Okay thank you guys, that helped me

### Who is online

Users browsing this forum: No registered users and 23 guests