Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pure Lua 5.2 Script (May work in 5.1, untested)
#1
Hello, all! I'm new here. I decided it would benefit the community if I shared programming/scripting knowledge, and thus, this is now open source.

This is a simple OOP library I made, called weaverOOP. It's unfinished, but the basic code is here.

[lua]wO={}
wO.global={}
wO.global.clone=function(self) local cloneObj={} for i,v in pairs(self) do cloneObj[i]=v end return cloneObj end
wO.global.isA=function(self, arg)
if self.className==arg then
return true
elseif self.className~="base" then
if wO.classes[self.extends]:isA(arg) then
return true
end
else
return false
end
end
wO.global.className="base"

wO.classes={base=wO.global}

function wO.newClass(a, ...)
arg={...}
b=(arg[1] or {})
b.className=a
b.name=a.."Obj"
if #arg==2 then
b.mt={__index=wO.classes[arg[2]]}
b.extends=arg[2]
else
b.mt={__index=wO.classes["base"]}
b.extends="base"
end
b.mt.__tostring=function(a) return "Class "..a end
b.mt.__call=function(self) return wO.newObj(self.className) end
setmetatable(b, b.mt)
b.mt=nil
wO.classes[a]=b
end

function wO.newObj(a, ...)
arg={...}
local o=(arg[1] or {})
local mt={__index=wO.classes[a]}
mt.__tostring=function(a) return "Object "..o.name..", a "..o.className end
setmetatable(o, mt)
return o
end

setmetatable(_G,{__index=function(_,a) return (a=="newClass" and wO.newClass or (a=="newObj" and wO.newObj or (wO.classes[a] or nil))) end}) --Ugly piece of code.

--Example begins here.

newClass("class",{x=0,y=0})
local t=class()
print(class.x,class.y)
newClass("class2",{z=15},"class")
local t=class2()
print(t.x,t.y,t.z)
[/lua]

API for those to lazy to read my 46 line code:

wO.newClass(string className,table class,[string extends])
creates a class called "classNameHere" where it's simply that table. It'll inherit all the things from the class called extends. (With no extends argument, it uses the global class)

wO.newObj(string className,[table objectValues])
Creates an object of className, with the values of objectValues. (Defaults to {} if not supplied) (deprecated as of 0.1.2)

className([table objectValues])

Equivalent to wO.newObj("className",[table objectValues]). Make sure the metatable of _G has not been changed by another script, this will error it.

Global methods and properties:

obj:isA(string className)

Checks if object obj is or extends an object of className.

obj:clone()

Returns an exact copy of object obj.

obj.name

Specific name of an object, not of much use as of now.

obj.className

The class name of an object, used in global functions and various other items.


Attached Files
.lua   wO.lua (Size: 1.19 KB / Downloads: 14)
Reply
#2
How does sharing copied lua code benefit people
Reply
#3
(09-08-2012, 03:56 AM)h8onme Wrote: How does sharing copied lua code benefit people

Copied? I think not...

Also, looking at code will help new/intermediate scripters/programmers understand the concepts and syntax of the language.

What makes you think it's copied?
Reply
#4
(09-08-2012, 04:16 AM)BrandonFireflower Wrote: Also, looking at code will help new/intermediate scripters/programmers understand the concepts and syntax of the language.

That may be so, but that code is a heckuva lot more complicated than any I've ever used or seen used in Buildism, even by the creator. I may have seen some code that was that advanced in a few Roblox scripts, but we'd be talking 5,000 lines made by the finest, and definitely not the kind of thing your average builder would be using.
Reply
#5
kewl

Oh no - I'm turning into noob007.
[Image: 2eehsib.gif]
Reply
#6
(09-08-2012, 12:35 PM)Qwertygiy Wrote:
(09-08-2012, 04:16 AM)BrandonFireflower Wrote: Also, looking at code will help new/intermediate scripters/programmers understand the concepts and syntax of the language.

That may be so, but that code is a heckuva lot more complicated than any I've ever used or seen used in Buildism, even by the creator. I may have seen some code that was that advanced in a few Roblox scripts, but we'd be talking 5,000 lines made by the finest, and definitely not the kind of thing your average builder would be using.

The only complicated part is the metatables. o.e
If you've not seen any Lua code this complicated, you don't want to see what I used it for... Tongue
Reply
#7
(09-08-2012, 09:18 PM)BrandonFireflower Wrote: The only complicated part is the metatables. o.e
If you've not seen any Lua code this complicated, you don't want to see what I used it for... Tongue

Was it to crack the Pentagon's firewall?
Reply
#8
(09-08-2012, 09:49 PM)Qwerty Wrote: Was it to crack the Pentagon's firewall?

No, it was to create a 2D tile based scroller in Love2D, and a little game where the goal is to fall to the bottom of a tower.

Hehe, that scroller was kinda inefficient. Tongue
Reply
#9
I understand it, but there isn't very much in the way of advanced scripting on Buildism. I think I'm the only one that even knows what a metatable is. Tongue

In the beginning I was hoping that Buildism's community would be more like Love2D's (it was actually one of the things that inspired me to start working on it again) but that was not to be.
Reply
#10
(09-08-2012, 11:01 PM)Jacob_ Wrote: I understand it, but there isn't very much in the way of advanced scripting on Buildism. I think I'm the only one that even knows what a metatable is. Tongue

In the beginning I was hoping that Buildism's community would be more like Love2D's (it was actually one of the things that inspired me to start working on it again) but that was not to be.
It's because 95.5% of all your users are from ROBLOX.
[Image: KbMTk.png][Image: uC9A1.png]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)