Understanding classes

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
foszor
Prole
Posts: 3
Joined: Sat Nov 06, 2010 12:00 am
Location: California, USA
Contact:

Understanding classes

Post 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?
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Understanding classes

Post by zac352 »

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

Re: Understanding classes

Post 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?
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Understanding classes

Post 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
Hello, I am not dead.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Understanding classes

Post 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?
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Understanding classes

Post by bartbes »

Yeah, I've never ever seen it used :P. Anyway, its goal is to make it possible to use another class system, without changing code, simply by checking if it's compatible.
User avatar
foszor
Prole
Posts: 3
Joined: Sat Nov 06, 2010 12:00 am
Location: California, USA
Contact:

Re: Understanding classes

Post by foszor »

Okay thank you guys, that helped me
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 4 guests