2DWorlds Forums
Pure Lua 5.2 Script (May work in 5.1, untested) - Printable Version

+- 2DWorlds Forums (http://2dworlds.buildism.net/forum)
+-- Forum: Off Topic (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=5)
+--- Forum: Programming (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=30)
+--- Thread: Pure Lua 5.2 Script (May work in 5.1, untested) (/showthread.php?tid=9925)

Pages: 1 2


Pure Lua 5.2 Script (May work in 5.1, untested) - BrandonFireflower - 09-08-2012

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.


RE: Pure Lua 5.2 Script (May work in 5.1, untested) - h8onme - 09-08-2012

How does sharing copied lua code benefit people


RE: Pure Lua 5.2 Script (May work in 5.1, untested) - BrandonFireflower - 09-08-2012

(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?


RE: Pure Lua 5.2 Script (May work in 5.1, untested) - Qwertygiy - 09-08-2012

(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.


RE: Pure Lua 5.2 Script (May work in 5.1, untested) - DysLabs - 09-08-2012

kewl

Oh no - I'm turning into noob007.


RE: Pure Lua 5.2 Script (May work in 5.1, untested) - BrandonFireflower - 09-08-2012

(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


RE: Pure Lua 5.2 Script (May work in 5.1, untested) - qwerty - 09-08-2012

(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?


RE: Pure Lua 5.2 Script (May work in 5.1, untested) - BrandonFireflower - 09-08-2012

(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


RE: Pure Lua 5.2 Script (May work in 5.1, untested) - Jacob__mybb_import1 - 09-08-2012

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.


RE: Pure Lua 5.2 Script (May work in 5.1, untested) - When - 09-08-2012

(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.