Advertisement
LuaWeaver

Docs for wOOP 0.5

Nov 8th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. This will go over all the functions and ways to use wOOP. It's a tad different from other libraries.
  2. Any optional arguments will be shown by being wrapped in [ and ].
  3. To create a class, you can use something similar to this:
  4.  
  5. class "className" ["extends"]
  6. {
  7. --data
  8. }
  9.  
  10. This will create a class called "className" and if "extends" is provided, it'll extend that class (inherit all data). The table is what contains all the properties and methods. "extends" defaults to "base", the root class.
  11.  
  12. To create an object, you can just call the class name like a function (i.e. className()). With constructors, you can supply arguments when you call it (className(1, 2, 3)). This is *NOT* a table, it is a userdata generated using "newproxy". This allow people to use the __gc and __len metamethods.
  13.  
  14. To make a constructor, define the "__settings" table within the class data. Create the function "new" and make it return a table containing certain items. In example:
  15.  
  16. class "coordinates"
  17. {
  18. x=0,
  19. y=0,
  20. __settings=
  21. {
  22. new=function(x,y)
  23. return {x=x,y=y}
  24. end
  25. }
  26. }
  27.  
  28. Calling coordinates(1, 3) will give you a table (really a proxy to a table) containing {x=1,y=3}.
  29.  
  30. As for the __ at the beginning of that, that creates a private value. This is another advantage of using proxies: they can actually act as proxies! The __settings value and any other private values can be accessed by using one of the psuedomethods.
  31.  
  32. Wait, what's a psuedomethod you say? A psuedomethod is quite plain: it's a table with the __call metamethod set. Why in the world would I do this!?
  33.  
  34. Now, self can be used without having to use methods! You can just use a plain function:
  35.  
  36. class "coordinates"
  37. {
  38. _x=0,
  39. _y=0,
  40. __settings=
  41. {
  42. new=function(x,y)
  43. return {x=x,y=y}
  44. end
  45. },
  46. slide=function(x,y)
  47. self._x=self.x+x
  48. self._y=self.y+y
  49. end,
  50. coords=function()
  51. return {x=self._x,y=self._y}
  52. end
  53. }
  54.  
  55. The function is auto-converted to the table and auto-assigned the metatable: not to worry! Now you can just use the self value from any public psuedomethod!
  56.  
  57. As for the single _, that designates it as a read only value! Attempting to set it from anything outside a psuedomethod will give an error.
  58.  
  59. Another powerful feature of this is the "object metatable" (abbreviated to omt in the code). This is a metatable that applies to the objects (kinda obvious). To use it, just define "__omt" as a table containing all the contents! Woohoo, simplicity is good!
  60.  
  61. By the way, using getmetatable or setmetatable on any object will give an error. Sorry, but the metatables can be set with the object metatable! You shouldn't mess with the objects directly, modify the __omt. There is also a __mt for the class itself.
  62.  
  63. Oh, and an object cannot have any new values attached to it. The __newindex locks out any NEW value.
  64.  
  65. One last thing. The default psuedomethods and properties are listed here as well.
  66.  
  67. .isA("classname")
  68. A psuedomethod that checks if it is a class or extends one.
  69.  
  70. .className
  71. Set every time by the library, but it's global anyways.
  72.  
  73. .__omt
  74. The default omt contains nothing.
  75.  
  76. .__mt
  77. The default mt contains only __tostring=function(a) return "Class "..a.className end
  78.  
  79. .__settings
  80. The default constructor returns a blank table.
  81.  
  82. Thank you for using wOOP.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement