View difference between Paste ID: kiwbk2Ju and WDAegKfv
SHOW: | | - or go back to the newest paste.
1-
This will go over all the functions and ways to use wOOP. It's a tad different from other libraries.
1+
Docs for wOOP 0.6.0
2-
Any optional arguments will be shown by being wrapped in [ and ].
2+
3-
To create a class, you can use something similar to this:
3+
METHODS DO NOT USE A COLON.
4
THEY USE A .
5-
class "className" ["extends"]
5+
THE SELF VARIABLE IS 'IMPLIED'
6
7-
	--data
7+
Global functions
8-
}
8+
wOOPupdate(nil) -- For events. Updates all events.
9
class(STRING className, [STRING extends], TABLE data) -- Create a new class. className is the name of the class. extends is the optional classname it inherits from. data is all the contents.
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.
10+
11
Global classes
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.
12+
13
CLASS base(nil) -- The base class for all objects. Every object is an extension of this.
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:
14+
	Class props
15
		STRING className -- the classname
16-
class "coordinates"
16+
		TABLE __settings -- the settings of the class
17
		TABLE __omt -- the object metatable
18-
	x=0,
18+
		TABLE __mt -- the metatable
19-
	y=0,
19+
	Class methods
20-
	__settings=
20+
		isA(STRING className) -- checks if the class if or extends the className
21
22-
		new=function(x,y)
22+
CLASS event(FUNCTION condition) -- An event. condition is a function which is run every frame and if true will run the callback function.
23
	Class props
24
		BOOLEAN _connected -- Whether a callback is selected.
25
	Class methods
26-
}
26+
		connect(FUNCTION callback) -- Runs this function every time the condition is met.
27
		disconnect(nil) -- Remvoes the callback.
28-
Calling coordinates(1, 3) will give you a table (really a proxy to a table) containing {x=1,y=3}.
28+
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.
30+
Class structure
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!?
32+
class "exampleClass" "base" --base is automatically set if not specified
33
{
34-
Now, self can be used without having to use methods! You can just use a plain function:
34+
	--PROPS START HERE
35
	__x=0, --The __ makes this a private value
36-
class "coordinates"
36+
	__y=0,
37
38-
	_x=0,
38+
	_magnitude=0,
39-
	_y=0,
39+
40-
	__settings=
40+
	--METHODS START HERE
41
	slide=
42-
		new=function(x,y)
42+
	function(x,y)
43
		self.__x=self.__x+x --Private and read only values can be changed using methods
44
		self.__y=self.__y+y
45
		self._magnitude=math.sqrt(self.__x^2+self.__y^2) --distance formula (magnitude)
46-
	slide=function(x,y)
46+
47-
		self._x=self.x+x
47+
48-
		self._y=self.y+y
48+
	coords=
49
	function()
50-
	coords=function()
50+
		return self.__x,self.__y
51-
		return {x=self._x,y=self._y}
51+
52-
	end
52+
53-
}
53+
	--EVENTS START HERE
54
	origin =
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!
55+
	event function(oldSelf,newSelf) --oldSelf : THE OBJECT ONE FRAME AGO newSelf : THE CURRENT OBJECT
56
		if newSelf.x==0 and newSelf.y==0 then
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.
57+
			return true
58
		end
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!
59+
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.
61+
	--CLASS SETUP STARTS HERE
62
	__settings= --Settings
63-
Oh, and an object cannot have any new values attached to it. The __newindex locks out any NEW value.
63+
64
		new= --Constructor
65-
One last thing. The default psuedomethods and properties are listed here as well.
65+
		function(x,y)
66
			return {x=x,y=y}
67-
.isA("classname")
67+
68-
A psuedomethod that checks if it is a class or extends one.
68+
69
	__omt=
70-
.className
70+
71-
Set every time by the library, but it's global anyways.
71+
		__tostring=="example"
72
	}
73-
.__omt
73+
}