| 1 | # XMOO Readme file. |
| 2 | |
| 3 | #Creating a new class |
| 4 | In this example, we will create a torch. |
| 5 | |
| 6 | Start with the line: |
| 7 | classes.obj_torch = { } -- This creates an empty class |
| 8 | |
| 9 | What you call the class does not matter, it is not visible to users. |
| 10 | |
| 11 | Now we must define some things, such as its name, and what it looks like, its properties. |
| 12 | |
| 13 | classes.obj_torch._properties = { |
| 14 | name = "torch", |
| 15 | longname = "An electric torch", |
| 16 | desc = "A small portable electric torch, to help you see when it is dark" |
| 17 | } |
| 18 | |
| 19 | Simple, eh? :) |
| 20 | |
| 21 | Now, we want the user to be able to do things with the torch, let's say that 'twisting' it, switches it on and off. |
| 22 | So we must define the method 'twist': |
| 23 | |
| 24 | function classes.obj_torch:twist(info) |
| 25 | |
| 26 | end |
| 27 | |
| 28 | info is a variable we get, that contains information useful to use, such as who twisted it (info.person), and what room we are in (info.room), etc. |
| 29 | |
| 30 | Now we must also keep track of whether the torch is lit or unlit. We will store this as a property. In a method, 'self' always refers to the curent object. |
| 31 | |
| 32 | function classes.obj_torch:twist(info) |
| 33 | if self._properties.state == "lit" then self._properties.state == "unlit" -- if it is lit, make it unlit |
| 34 | else self._properties.state == "lit" end -- Otherwise, make it lit |
| 35 | end |
| 36 | |
| 37 | Very good, but we must show everyone in the room what is going on: |
| 38 | |
| 39 | function classes.obj_torch:twist(info) |
| 40 | if self._properties.state == "lit" then |
| 41 | self._properties.state == "unlit" -- if it is lit, make it unlit |
| 42 | self._properties.desc = "A small portable electric torch, to help you see when it is dark. It is not lit." |
| 43 | info.person:_say("/me turns off the torch") |
| 44 | else |
| 45 | self._properties.state == "lit" -- Otherwise, make it lit |
| 46 | self._properties.desc = "A small portable electric torch, to help you see when it is dark. It is lit." |
| 47 | info.person:_say("/me lights the torch") |
| 48 | end |
| 49 | end |
| 50 | |
| 51 | As you can see, the colon ':' is used to call methods, or functions of another object. Remember that people are objects too! |
| 52 | The default 'look' method uses the 'desc' property, so we have changed it, to show whether the torch is lit or not. |
| 53 | |
| 54 | It's done! |
| 55 | |
| 56 | More to come, including creating new rooms :) |