Once you have defined a class, the add to class action statement lets you add any object to the class. The object must already exist; it may have been previously defined, or it may have been created during runtime using the add statement. Note that the add statement, which defines an object during runtime, is different from the add to class statement described here, which adds an existing object to a class; for example:
add Small to class Size
Ancestry can be specified in the add to class statement; for example, the following code adds the object Monkey, which is a child of Animals, to the class JungleBeasts:
add Monkey in Animals to class JungleBeasts
An object can be a member of many classes simultaneously. Adding an object to one class has no effect on the object's membership in other classes.
If a response has been defined for the class, then an object that is defined and added to the class at runtime can stimulate the response when it is selected.
For example:
string variable KeyName integer variable Counter is 0
...
response to ...
copy "BlueKey" to KeyName
append Counter to KeyName
add key KeyName at position 50 50
add KeyName to class BlueClass
copy (Counter + 1) to Counter
...
KeyName is a variable to which the string literal "BlueKey" was assigned. The first time this response is performed, the add statement creates a new object whose name is BlueKey0. The second time it is performed, BlueKey1 is created, and so on.
An object can be created during runtime, and added dynamically to a class. In the following example, an object is created during runtime, at the coordinate positions entered from the keyboard.
# Define an initially empty class:
class Keys
...
response to line from keyboard
# Extract X and Y positions from keyboard input:
extract from input
take number X
take number Y
copy (Counter+1) to Counter
# Construct name of key:
copy "Point" to Name
append Counter to Name
# Add newly created key at specified position:
add key Name at position X Y
...
# Add constructed key name as member of Keys class:
add Name to class Keys
When you specify an object name in the add to class action statement, make sure that the name does not already exist within that class. If it does, you must first remove the object from the class and then re-add it. Otherwise, the addition has no effect and only the old object (with the same name) remains in the class.