First, we need some way to tell if the toy has been wound up. We'll create a variable called "wound" on the generic toy. We also need to know if it is currently walking around, so we also define a variable called "going" that will be set to 1 while the duck is walking, and 0 when it is not.
@add-variable $windup_toy,wound = 0 Object variable $windup_toy,wound added with value 0. @add-variable $windup_toy,going = 0 Object variable $windup_toy,going added with value 0.
Before we can add a command to the duck, we need to program the method behind the command. All ColdC commands are actually just syntax descriptions that allow a method to be triggered. We define this method as public, which means that a method on any other object may call this method. There are two other access levels: protected, which means that only methods on the current object and its descendants may call this method, and private, which means that only the methods defined on the object on which this method is defined may call it.
@program $windup_toy.wind_cmd +access=public
arg cmdstr, cmd, @args;
// Increment the wound value x 2
wound+=2;
// Tell the player e has wound the toy
sender().tell("You wind up the " + .name() + ".");
// Tell everyone else in the room the toy was wound
.location().announce(strfmt("%s winds up the %s.", sender().name(), .name()));
.
On the first line we defined the arguments that the wind method will be expecting. These arguments are passed in by the command parsing system which we will define a command for in the next step. The first argument (cmdstr) will contain the entire command as entered by the player. The second argument (cmd) will contain just the command name that was used to activate the method. The third argument (args) will be a list of any other arguments sent to the method. The args argument is preceeded by an '@' symbol to indicate that we want to use scatter-assignment, which means that the args variable will receive any further arguments passed as a list. This helps prevent errors if there are too many arguments sent for some reason.
Here we also see lines that begin with '//' -- these are comments. The code compiler will ignore these lines, but they are extremely useful for documenting your code and making it easier to read. Documentation is important, and performing this task in the code will help both you and anyone else who uses your code!
The wound variable refers to the wound variable we defined on $windup_toy (the Generic Wind-Up Toy). In a ColdC method, local variables (variables used only in the executing method) are defined with the 'var' keyword. If a method references a variable that has not been declared locally with the var keyword, then the method assumes the variable is a member variable of the current object.
The idea behind adding 2 to wound is that you can wind it a bunch of times to make it go longer.
Now let's add the command so we can activate the wind method:
@add-command "wind <this>" to $windup_toy.wind_cmd
Let's try it on our duck:
wind hduck You wind up the Heisenberg's Wind-Up Duck.
Everyone else sees:
Heisenberg winds up the Heisenberg's Wind-Up Duck.