Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[Instructions:
- Syntax:
- _G.class(className){
- Property=value,
- Property=value
- }
- To make a new object of that class type:
- ClassName "InheritedClassName"
- For people with some experience in other programming languages, everything is inherited, right down
- to the values the orginal class had (you can edit those).
- Want to learn about this script? Read it =P
- ]]
- _G["class"]=function(name)
- return function(arg)
- local s={[name]={}} -- Since this is normal Lua, you can't just make [name] a variable. (Without using getfenv())
- for i,v in pairs(arg) do -- Get all the values
- s[name][i]=v -- Set them
- end
- local Environment=getfenv(2) -- Get the current environment
- Environment[name]=s[name] -- Create the class
- setmetatable(Environment[name], {__index=getfenv(), __newindex=function(t, i, v) -- Set the metatable!
- error("Attempt to index global '"..i.."' (a nil value)", 2) -- This reduces typos (hopefully)
- end,
- __call=function(Object, Name) -- Inheritance; no need for an ugly table!
- getfenv(2)[Name]=Object
- end})
- setfenv(2, Environment) -- Set the new environment
- end
- end
- --Example!:
- _G.class("Character"){
- Courage="Arg",
- Power=5,
- Wisdom="10"
- }
- Character "Link"
- Link.Power=4
Advertisement
Add Comment
Please, Sign In to add comment