|
| 04 Dec 2011 08:27 AM |
Cuz i googled for how to maike organized code for Lua and read som stoopid WoW Lua scriptin thingy and made class creatin thingy similiar to wut it had.
I dont know if it works xD
U do
CreateClass( [Table containing static functions and variables. If u try accessin something from a instance of this class and it doesnt exist, it checks if its in this static table], [Function that returns an instance of class. You can add a :destroy method for it, else a default destructor is used when u call :destroy on it. (the default empties out the table)] )
--[[ Create a class. static is a table containing static variables. If something isnt in the class instance, static will be checked for the variable. USAGE: local Car=CreateClass( { AddWheels=function(self,amount) self.Wheels=self.Wheels+amount end }, function(wheels,name) return { Wheels=(wheels and wheels or 4), Name=(name and name or "Anonymoos car") } end )
local car=Car:new(8,"Fat truck")
print(car.Wheels) >8 car:AddWheels(2) print(car.Wheels) >10 print(car.Name) >Fat truck ]] function CreateClass(static,instancer) static["__index"]=static if static.destroy==nil then static.destroy=function(self) setmetatable(self,nil) for i,v in pairs(self) do self[i]=nil end end end static.new=function(self,...) --create new instance local new=instancer(unpack(args)) --set metatable to static setmetatable(new,static) end end |
|
|
| Report Abuse |
|
|
kaboom1
|
  |
| Joined: 13 Nov 2008 |
| Total Posts: 363 |
|
|
| 04 Dec 2011 08:45 AM |
| *Goes off to write better one because your system doesn't have what I like about OOP* |
|
|
| Report Abuse |
|
|
|
| 04 Dec 2011 08:45 AM |
| I was thinking of making a class have a :inherit(otherclass) function but i thought it would require more than 5 lines cuz of metatable usage. |
|
|
| Report Abuse |
|
|
kaboom1
|
  |
| Joined: 13 Nov 2008 |
| Total Posts: 363 |
|
|
| 04 Dec 2011 08:48 AM |
| That's not why, I wunt the use of Interfaces ): |
|
|
| Report Abuse |
|
|
|
| 04 Dec 2011 08:57 AM |
Watsa interfaice?
Use the same static table to create multiple class types? o.e And put all functions in the static table? |
|
|
| Report Abuse |
|
|
kaboom1
|
  |
| Joined: 13 Nov 2008 |
| Total Posts: 363 |
|
|
| 04 Dec 2011 09:01 AM |
Interface=Java thingeh
It makes it error if all items in the interface are not included in the class that uses it. |
|
|
| Report Abuse |
|
|
nightname
|
  |
| Joined: 10 Jun 2008 |
| Total Posts: 8960 |
|
|
| 04 Dec 2011 09:22 AM |
| This has been done before, many times too. |
|
|
| Report Abuse |
|
|
NilPirate
|
  |
| Joined: 31 Jul 2010 |
| Total Posts: 3077 |
|
|
| 04 Dec 2011 09:23 AM |
Might not work with Roblox, and certainly not mine, but;
local mt_class = {}
function mt_class:extends(parent) self.super = parent setmetatable(mt_class, {__index = parent}) parent.__members__ = parent.__members__ or {} return self end
local function define(class, members) class.__members__ = class.__members__ or {} for k, v in pairs(members) do class.__members__[k] = v end function class:new(...) local newvalue = {} for k, v in pairs(class.__members__) do newvalue[k] = v end setmetatable(newvalue, {__index = class}) if newvalue.__init then newvalue:__init(...) end return newvalue end end
function class(name) local newclass = {} _G[name] = newclass return setmetatable(newclass, {__index = mt_class, __call = define}) end |
|
|
| Report Abuse |
|
|
pighead10
|
  |
| Joined: 03 May 2009 |
| Total Posts: 10341 |
|
|
| 04 Dec 2011 11:38 AM |
"Interface=Java thingeh
It makes it error if all items in the interface are not included in the class that uses it."
I never got the point of interfaces |
|
|
| Report Abuse |
|
|
|
| 04 Dec 2011 11:43 AM |
Who needs interfaces or extending stuff in roblox...
I was just thinking of using this to create and destroy gui windows and stuff. For anything else its too complex to bother to use it. |
|
|
| Report Abuse |
|
|
KingBoo
|
  |
| Joined: 16 Jul 2007 |
| Total Posts: 8495 |
|
|
| 04 Dec 2011 12:44 PM |
| Next step: Overload operators |
|
|
| Report Abuse |
|
|
|
| 04 Dec 2011 01:41 PM |
| I dont want that its confuzzeling. |
|
|
| Report Abuse |
|
|
TheMyrco
|
  |
| Joined: 13 Aug 2011 |
| Total Posts: 15105 |
|
|
| 04 Dec 2011 01:43 PM |
?v=G8-ocjinDc8
~Myrco; Music lover, nederlands/dutch and a scripter |
|
|
| Report Abuse |
|
|
|
| 06 Dec 2011 10:35 AM |
This has been done before in ROBLOX. Although, I think ROBLOX might've removed this feature, since I can't make it work...
~ Cows moo, ducks quack, I post. ~ |
|
|
| Report Abuse |
|
|
|
| 06 Dec 2011 02:11 PM |
@candymaniac Thats for JavaScript. Do you think ROBLOX has that future?
Aparently, not. |
|
|
| Report Abuse |
|
|
pighead10
|
  |
| Joined: 03 May 2009 |
| Total Posts: 10341 |
|
| |
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 06 Dec 2011 07:00 PM |
My preferred version of classes in Lua:
function class(name) local cls = {} getfenv(0)[name] = cls return function(ctor, static) if ctor then getfenv(0)['Make'..name] = function(...) local inst = {} ctor(inst) return inst end end if static then static(cls) end end end
Usage:
class'MyClass'(function(this) local mPrivateVar = 3 this.publicVar = 4 function this:method() ... end end, function(def) def.publicStaticVar = 2.171 function def:staticMethod()
end end)
Much more elegant than a more heavy-weight class system in most cases. For instance, you can see this technique used heavily in my recent Christmas Katana gear. |
|
|
| Report Abuse |
|
|
|
| 07 Dec 2011 08:58 AM |
Well, I don't know anything of Programming but LUA... since my knowledge is very limited...
~ Cows moo, ducks quack, I post. ~ |
|
|
| Report Abuse |
|
|
|
| 07 Dec 2011 02:57 PM |
@Candymaniac
Lua is not a acronym.....Sorry...Have to point that out..
NOW I WANNA MAKE MY OWN CLASS CRETING THINGY FOR Lua!!!!
*Scripts something random*
*Fail code*
*Get's bored*
*RAGEQuits later* |
|
|
| Report Abuse |
|
|