|
Wyvern uses lots of Lists, Sets, Hashes, Arrays, and other data structures in its arguments. This tutorial will show you how to read and manipulate them. As an example, the wyvern.lib.GameMap interface has a getMonsterList() function that returns a Set of the monsters in the map. (Sadly, it's a poorly-named function - should have been called getMonsters()).
Quickie DefinitionsThere are four main kinds of data structures used in Wyvern:
Python has its own set of data structures (arrays, lists, maps, and so on) with its own specific language constructs for creating and manipulating them. You are more than welcome to use them for your own purposes. However, you'll need to use this code in this tutorial when you're manipulating Wyvern built-in data structures (since they're written in Java). Many of the Wyvern game classes return one of the four standard data structures, or take one as a parameter. For example, there's a method called wyvern.lib.GameMap.getObjectsAt(x, y)That returns a List of the objects at location (x, y). The list is just like any other list, and you can do all the normal operations to it that we cover below.
Accessing ValuesThere are four fundamental operations for every data structure:
It has the Javadoc API documentation for all the Java classes. The one you'll visit the most often is in the Java 2 Platform Packages (in the right frame), under the package java.util. It has the API definitions for all the data structures that Wyvern uses. For example, you can click on the List interface, scroll down to the Method Summary, and see all the methods on List. You have all of these methods available to you in your Wyvern coding - in fact, you can call all of the thousands of methods in the entire Java API. In fact, you actually have access to three complete API sets in Wyvern:
So your code in Wyvern is actually sitting on a truly gigantic bundle of code, all of which you can use freely for your coding. It helps to familiarize yourself with the tools you've got at your disposal! IteratorsYou access all the data structures above (except Arrays) using an Iterator. It goes through each item in the data structure and returns them to you one at a time. Here's the basic pattern:
This works identically for Lists and Sets, except that for Sets, the items could come in any order. For Maps, you can iterate over the keys or the values. To iterate over the keys, do this:
The best way to iterate over the values of a Map is to iterate over the keys, and pull out the value for each key, like so:
Deleting Items During IterationSometimes you want to delete items from a data structure as you're going through it. You can't just say list.remove(item), because you'll get something called a ConcurrentModificationException when you try to do the next call to it.next().Instead, the Iterator provides a remove() method for you. To remove "rhialto" from a list of names, you'd do this:
ArraysNote: for experienced Python programmers: when we say "Array" here, we mean a Java array, such as
String[ ] names = { "Joe", "Fred", "Bob" };
You don't use Iterators to go through a Java array. Here's the pattern:
It's more or less like an Iterator. Here's a working example that shows you how to use it. In this case, we've written an object that provides a command called "argtest". When you type "argtest" followed by some arguments, it prints out the arguments you typed. Note that we didn't actually parse the arguments - they come pre-parsed in the CommandEvent passed into Command.execute(). You can get them via event.getArgs(), which returns an Array of the arguments.
|