Defining Methods
The most elementary method call in steve is a call to a method that takes no arguments. The definition of such a method is simple. Inside the definition of instanceName, we create a line:
+ to methodName:
The statements that follow this line will be part of the newly defined method until either the end of the object definition, or until the next method definition.
To define a method that takes arguments we will need the keyword, variable name and type of each argument. The keyword identifies the variable when it is being called, while the variable name
is how the variable will be referenced from within the method. Finally, the type is simply the type of variable that will be passed in. The layout of this information is , such that a method
which takes one variable could be defined by the following line:
keyword, variable_name, (type)
+ to set-velocity to-value theValue (float):
If the method takes two variables, we add another keyword/name/type triplet:
+ to set-rotation of-joint theJoint (Object) to-value theValue (float):
The code associated with the second method would then use the variables theJoint and theValue: "of-joint" and "to-value" are not actual
variables, but instead the keywords which indicate which variables follows.
The calling convention of these methods is simple. After the instance name and method name, we give a list of keywords and values to be passed in. The order of the keyword/value pairs does not
effect how the code is executed, though it may effect the readability of the code. The following lines call the set-rotation method which we defined above:
# the following lines are equivalent myObject set-rotation of-joint myJoint to-value 200. myObject set-rotation to-value 200 of-joint myJoint.
Methods may also have local variables associated with them. These variable definitions look just like class variable definitions, except that they follow after the method definition line, and not after the variable definition line. Method variables are automatically initialized to zero every time the method is called. Variable declarations in a method must precede all statements in the method.
For example, here is a simple method that uses local variables:
+ to find-closest-creature in creatureList (list):
item (object).
closestItem (object).
distance (float).
# we start with an unreasonably high "closestDistance" so that
# we are sure to find something closer.
closestDistance = 1000000.
foreach item in creatureList: {
distance = |(self get-location) - (item get-location)|.
if distance < closestDistance: {
closestItem = item.
closestDistance = distance.
}
}
return closestItem.
|
For developer use only |
|---|---|
|
When examining the internal classes included with the breve distribution, you might notice some methods defined using a minus sign instead of a plus sign: - to methodName: This syntax simply means that the method should be treated as a non-public method and that the method should not be documented. Though these methods function as all other methods, their use in user simulations is discouraged. |
