Sat, 27 Mar 2010 17:43:08 +0000
Initial commit (importing from old SVN repo which got lost)
# XMOO Readme file. #Creating a new class In this example, we will create a torch. Start with the line: classes.obj_torch = { } -- This creates an empty object What you call the class does not matter, it is not visible to users. Now we must define some things, such as its name, and what it looks like, its properties. classes.obj_torch._properties = { name = "torch", longname = "An electric torch", desc = "A small portable electric torch, to help you see when it is dark" } Simple, eh? :) 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. So we must define the method 'twist': function classes.obj_torch:twist(info) end 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. 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. function classes.obj_torch:twist(info) if self._properties.state == "lit" then self._properties.state == "unlit" -- if it is lit, make it unlit else self._properties.state == "lit" end -- Otherwise, make it lit end Very good, but we must show everyone in the room what is going on: function classes.obj_torch:twist(info) if self._properties.state == "lit" then self._properties.state == "unlit" -- if it is lit, make it unlit self._properties.desc = "A small portable electric torch, to help you see when it is dark. It is not lit." info.person:_say("/me turns off the torch") else self._properties.state == "lit" -- Otherwise, make it lit self._properties.desc = "A small portable electric torch, to help you see when it is dark. It is lit." info.person:_say("/me lights the torch") end end As you can see, the colon ':' is used to call methods, or functions of another object. Remember that people are objects too! The default 'look' method uses the 'desc' property, so we have changed it, to show whether the torch is lit or not. It's done! More to come, including creating new rooms :)