|
Here we'll show you how to create objects that give players access to new commands. ContentsCommandsTo make a new command, have your python class implement the wyvern.lib.Command interface. This interface requires that you add 3 new methods to your class:
Here's a simple example, which we'll put in a file called wiz/rhialto/python/command_test.py:
To test it, I did this:
I.e. after typing "hello", it told me "Hello, World!" We made it a wyvern.lib.classes.DynamicObject, since that's a useful class for objects that can be picked up and dropped. It could have also been a wyvern.kernel.maps.MapObject. How does it work?Note: we frequently use the word "agent" instead of "player", because the agent could be a monster. Monsters can move, cast spells, and do pretty much any command that a player has access to. When you type in a command, the game looks in four places to see if it can find a handler for the command:
So you have four chances to get your Command invoked. The easiest way is to just make it work in inventory or on the ground, since you don't have to do anything special for that. For map-wide commands, you'll want to register with the map, and for commands that follow the player around, you'll want to register them with the player. More on this later. knowsCommand We used If this doesn't make sense to you, don't worry - just use String.equals() for comparing strings. createEventThis is Wyvern's most powerful extension feature, although we didn't use it here. We'll talk more about it in the Hooks and Events manual. The basic idea is that you can add properties to the event so that other people can know about it and modify it if they want. In our case, there isn't much to add. For some commands, like "give", for example, the GiveCommand (a built-in Command) sets the giver, the recipient, and the gift, among other things. execute This is the part that actually executes your command. The
event that gets passed in is an instance of
CommandEvent provides these and many other very useful methods. The event is your friend - it's your scratchpad for putting and getting all sorts of useful data about the event and the context in which it's being executed. Registering CommandsWe mentioned that you can "register" your Command, so it gets called for everyone in the map, or for a player in any map. Map-Wide CommandsFor maps, you register a command with a "room", which is a rectangle in the map. It could be the entire map, if you want, in which case you'd call map.getBounds() to get the bounding rectangle. Here's a simple extension to our example that registers the "honk" command with the game map when you drop it. It's in a file called wiz/rhialto/python/maptest.py:
Now our object knows two commands: "honk" and "hello", and both of them tell the person who typed it "Hello, World!". However:
All we added were the 2 methods at the end: setMap() and remove(). setMap() is called when the object is added to the map (by dropping it, or if it was in the map file when the map was loaded). We call the superclass (important!) to put it in the map, and then we register ourselves to handle "honk" in the entire bounds of the map. On remove(), we remember which map we were in, call the superclass, then unregister. Notice something odd: we didn't add "honk" to the list of commands we understand in our knowsCommand method. Why didn't we? Well, we didn't really have to, because we've already proclaimed that we know the command when we registered with the map. In this case it's optional. knowsCommand is only necessary for getting your Command invoked automatically if it's in the agent's inventory, or sitting on the ground beneath the agent. You can just return 0 from this method if you're going to register your command yourself. (Don't return 1, or you'll get called for every single command, even ones you don't care about!) Player CommandsSetting up a command for a player is pretty similar - you just get a reference to the player and call registerCommand() (no rectangle this time) on the player. Need example here. Advanced FeaturesWe'll cover the more advanced functions in a later tutorial, perhaps, but this should get you started making new game commands.
|